diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddAnotherAccessorAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddAnotherAccessorAction.cs index fc57d1f95c..c5acc8aa60 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddAnotherAccessorAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/AddAnotherAccessorAction.cs @@ -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); }); } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddAnotherAccessorTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddAnotherAccessorTests.cs index 79339bf408..5f0369cbce 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddAnotherAccessorTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/AddAnotherAccessorTests.cs @@ -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 "}", result); } - [Test()] + [Test] public void TestAddGet () { string result = RunContextAction ( @@ -94,5 +94,17 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions " }" + Environment.NewLine + "}", result); } + + [Test()] + public void TestAutoProperty () + { + Test (@"class TestClass +{ + string $Test { get; } +}", @"class TestClass +{ + string Test { get; set; } +}"); + } } }