From eab84734b23a24de649cc504bf900a69a1a7a02b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Kr=C3=BCger?= Date: Wed, 23 Jan 2013 10:46:09 +0100 Subject: [PATCH] Fixed bug in create constructor action & implemented create enum value action. --- .../ICSharpCode.NRefactory.CSharp.csproj | 17 ++-- .../CreateConstructorDeclarationAction.cs | 2 +- .../CodeActions/CreateEnumValue.cs | 86 +++++++++++++++++++ .../CreateConstructorDeclarationTests.cs | 26 ++++++ .../CodeActions/CreateEnumValueTests.cs | 83 ++++++++++++++++++ .../CodeActions/TestRefactoringContext.cs | 6 +- .../ICSharpCode.NRefactory.Tests.csproj | 22 ++++- 7 files changed, 231 insertions(+), 11 deletions(-) create mode 100644 ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateEnumValue.cs create mode 100644 ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateEnumValueTests.cs diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj index b652ea2058..cd4adad728 100644 --- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj +++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj @@ -40,13 +40,12 @@ TRACE;FULL_AST;NET_4_0 - PdbOnly - false + none True - Full - true + full + True True @@ -56,9 +55,10 @@ v4.5 - Full - true + full + True True + v4.5 True @@ -67,9 +67,9 @@ v4.5 - PdbOnly - false + none True + v4.5 @@ -509,6 +509,7 @@ + diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs index b4629051fe..8fa3fceb38 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs @@ -34,7 +34,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring { public IEnumerable GetActions(RefactoringContext context) { - var createExpression = context.GetNode(); + var createExpression = context.GetNode() as ObjectCreateExpression; if (createExpression == null) yield break; diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateEnumValue.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateEnumValue.cs new file mode 100644 index 0000000000..fa6333c284 --- /dev/null +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateEnumValue.cs @@ -0,0 +1,86 @@ +// +// CreateEnumValue.cs +// +// Author: +// Mike Krüger +// +// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using ICSharpCode.NRefactory.PatternMatching; +using System.Linq; +using ICSharpCode.NRefactory.TypeSystem; +using System.Threading; +using System.Collections.Generic; +using ICSharpCode.NRefactory.CSharp.Resolver; +using ICSharpCode.NRefactory.Semantics; + +namespace ICSharpCode.NRefactory.CSharp.Refactoring +{ + [ContextAction("Create enum value", Description = "Creates an enum value for a undefined enum value.")] + public class CreateEnumValue : ICodeActionProvider + { + internal static bool IsInvocationTarget(AstNode node) + { + var invoke = node.Parent as InvocationExpression; + return invoke != null && invoke.Target == node; + } + + internal static Expression GetCreatePropertyOrFieldNode(RefactoringContext context) + { + return context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression || n is NamedExpression) as Expression; + } + + public IEnumerable GetActions(RefactoringContext context) + { + var expr = GetCreatePropertyOrFieldNode(context); + if (expr == null) + yield break; + if (!(expr is MemberReferenceExpression)) + yield break; + var propertyName = CreatePropertyAction.GetPropertyName(expr); + if (propertyName == null) + yield break; + if (IsInvocationTarget(expr)) + yield break; + var statement = expr.GetParent(); + if (statement == null) + yield break; + if (!(context.Resolve(expr).IsError)) + yield break; + var guessedType = CreateFieldAction.GuessType(context, expr); + if (guessedType == null || guessedType.Kind != TypeKind.Enum) + yield break; + var state = context.GetResolverStateBefore(expr); + if (state.CurrentMember == null || state.CurrentTypeDefinition == null) + yield break; + + yield return new CodeAction(context.TranslateString("Create enum value"), script => { + var decl = new EnumMemberDeclaration { + Name = propertyName + }; + script.InsertWithCursor(context.TranslateString("Create enum value"), guessedType.GetDefinition (), decl); + }); + + } + + } +} + diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationTests.cs index dffc01e08b..71d3e794ad 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationTests.cs @@ -152,6 +152,32 @@ class TestClass $new System.NotImplementedException (0, ""Hello"", new TestClass ()); } } +"); + } + + [Test] + public void TestBug9664 () + { + TestWrongContext ( + @"enum Foo +{ + Bar, +} + +class Baz +{ + public Baz (Foo foo) + { + } +} + +class Program +{ + public void Main () + { + var b = new Baz (Foo.$Something); + } +} "); } } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateEnumValueTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateEnumValueTests.cs new file mode 100644 index 0000000000..b91f1f58b1 --- /dev/null +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateEnumValueTests.cs @@ -0,0 +1,83 @@ +// +// CreateEnumValueTests.cs +// +// Author: +// Mike Krüger +// +// Copyright (c) 2013 Xamarin Inc. (http://xamarin.com) +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. +using System; +using NUnit.Framework; +using ICSharpCode.NRefactory.CSharp.Refactoring; + +namespace ICSharpCode.NRefactory.CSharp.CodeActions +{ + [TestFixture] + public class CreateEnumValueTests: ContextActionTestBase + { + [Test] + public void TestSimpleEnum() + { + Test( + @"enum Foo +{ + Bar +} + +class Baz +{ + public Baz (Foo foo) + { + } +} + +class Program +{ + public void Main () + { + var b = new Baz (Foo.$Something); + } +} +",@"enum Foo +{ + Something, + Bar +} + +class Baz +{ + public Baz (Foo foo) + { + } +} + +class Program +{ + public void Main () + { + var b = new Baz (Foo.Something); + } +} +"); + } + + } +} + diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs index 89e9ba55ea..308cabd459 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs @@ -121,7 +121,11 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions var startOffset = GetCurrentOffset (insertType.LBraceToken.EndLocation); foreach (var node in nodes.Reverse ()) { var output = OutputNode (1, node, true); - InsertText (startOffset, output.Text); + if (parentType.Kind == TypeKind.Enum) { + InsertText (startOffset, output.Text +","); + } else { + InsertText (startOffset, output.Text); + } output.RegisterTrackedSegments (this, startOffset); } var tcs = new TaskCompletionSource (); diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj index 7eba852b19..7d1936dbee 100644 --- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj +++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj @@ -59,6 +59,26 @@ v4.5 false + + none + + + none + + + v4.5 + + + none + v4.5 + + + v4.5 + + + none + v4.5 + @@ -368,6 +388,7 @@ + @@ -392,7 +413,6 @@ -