Browse Source

AddAnotherAccessor action now works on auto properties.

pull/32/merge
Mike Krüger 13 years ago
parent
commit
27913d73f1
  1. 16
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddAnotherAccessorAction.cs
  2. 16
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddAnotherAccessorTests.cs

16
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddAnotherAccessorAction.cs

@ -46,21 +46,25 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -46,21 +46,25 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
yield break;
}
yield return new CodeAction (pdecl.Setter.IsNull ? context.TranslateString("Add setter") : context.TranslateString("Add getter"), script => {
var accessorStatement = BuildAccessorStatement(context, pdecl);
Statement accessorStatement = null;
Accessor accessor = new Accessor () {
Body = new BlockStatement { accessorStatement }
};
var accessor = new Accessor ();
if (!pdecl.Getter.IsNull && !pdecl.Getter.Body.IsNull || !pdecl.Setter.IsNull && !pdecl.Setter.Body.IsNull) {
accessorStatement = BuildAccessorStatement(context, pdecl);
accessor.Body = new BlockStatement { accessorStatement };
}
accessor.Role = pdecl.Setter.IsNull ? PropertyDeclaration.SetterRole : PropertyDeclaration.GetterRole;
if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) {
script.InsertBefore(pdecl.RBraceToken, accessor);
script.InsertAfter(pdecl.Getter, accessor);
} else if (pdecl.Getter.IsNull && !pdecl.Setter.IsNull) {
script.InsertBefore(pdecl.Setter, accessor);
} else {
script.InsertBefore(pdecl.Getter, accessor);
}
script.Select(accessorStatement);
if (accessorStatement != null)
script.Select(accessorStatement);
script.FormatText(pdecl);
});
}

16
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddAnotherAccessorTests.cs

@ -33,7 +33,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -33,7 +33,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[TestFixture]
public class AddAnotherAccessorTests : ContextActionTestBase
{
[Test()]
[Test]
public void TestAddSet ()
{
string result = RunContextAction (
@ -64,7 +64,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -64,7 +64,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
"}", result);
}
[Test()]
[Test]
public void TestAddGet ()
{
string result = RunContextAction (
@ -94,5 +94,17 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -94,5 +94,17 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
" }" + Environment.NewLine +
"}", result);
}
[Test()]
public void TestAutoProperty ()
{
Test<AddAnotherAccessorAction> (@"class TestClass
{
string $Test { get; }
}", @"class TestClass
{
string Test { get; set; }
}");
}
}
}

Loading…
Cancel
Save