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
yield break; yield break;
} }
yield return new CodeAction (pdecl.Setter.IsNull ? context.TranslateString("Add setter") : context.TranslateString("Add getter"), script => { yield return new CodeAction (pdecl.Setter.IsNull ? context.TranslateString("Add setter") : context.TranslateString("Add getter"), script => {
var accessorStatement = BuildAccessorStatement(context, pdecl); Statement accessorStatement = null;
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 accessor = new Accessor () {
Body = new BlockStatement { accessorStatement }
};
accessor.Role = pdecl.Setter.IsNull ? PropertyDeclaration.SetterRole : PropertyDeclaration.GetterRole; accessor.Role = pdecl.Setter.IsNull ? PropertyDeclaration.SetterRole : PropertyDeclaration.GetterRole;
if (pdecl.Setter.IsNull && !pdecl.Getter.IsNull) { 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) { } else if (pdecl.Getter.IsNull && !pdecl.Setter.IsNull) {
script.InsertBefore(pdecl.Setter, accessor); script.InsertBefore(pdecl.Setter, accessor);
} else { } else {
script.InsertBefore(pdecl.Getter, accessor); script.InsertBefore(pdecl.Getter, accessor);
} }
script.Select(accessorStatement); if (accessorStatement != null)
script.Select(accessorStatement);
script.FormatText(pdecl); script.FormatText(pdecl);
}); });
} }

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

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

Loading…
Cancel
Save