Browse Source

Fixed bug in create constructor action & implemented create enum value

action.
pull/32/merge
Mike Krüger 13 years ago
parent
commit
eab84734b2
  1. 17
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 2
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs
  3. 86
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateEnumValue.cs
  4. 26
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationTests.cs
  5. 83
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateEnumValueTests.cs
  6. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs
  7. 22
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

17
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -40,13 +40,12 @@
<DefineConstants>TRACE;FULL_AST;NET_4_0</DefineConstants> <DefineConstants>TRACE;FULL_AST;NET_4_0</DefineConstants>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>PdbOnly</DebugType> <DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugType>Full</DebugType> <DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>True</DebugSymbols>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'net_4_5_Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'net_4_5_Debug' ">
@ -56,9 +55,10 @@
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_5_Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_5_Debug|AnyCPU' ">
<DebugType>Full</DebugType> <DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols> <DebugSymbols>True</DebugSymbols>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'net_4_5_Release' "> <PropertyGroup Condition=" '$(Configuration)' == 'net_4_5_Release' ">
<Optimize>True</Optimize> <Optimize>True</Optimize>
@ -67,9 +67,9 @@
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_5_Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_5_Release|AnyCPU' ">
<DebugType>PdbOnly</DebugType> <DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks> <AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="System" /> <Reference Include="System" />
@ -509,6 +509,7 @@
<Compile Include="Refactoring\LocalReferenceFinder.cs" /> <Compile Include="Refactoring\LocalReferenceFinder.cs" />
<Compile Include="Refactoring\CodeActions\SortUsingsAction.cs" /> <Compile Include="Refactoring\CodeActions\SortUsingsAction.cs" />
<Compile Include="Formatter\CSharpIndentEngine.cs" /> <Compile Include="Formatter\CSharpIndentEngine.cs" />
<Compile Include="Refactoring\CodeActions\CreateEnumValue.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj"> <ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

2
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs

@ -34,7 +34,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{ {
public IEnumerable<CodeAction> GetActions(RefactoringContext context) public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{ {
var createExpression = context.GetNode<ObjectCreateExpression>(); var createExpression = context.GetNode<Expression>() as ObjectCreateExpression;
if (createExpression == null) if (createExpression == null)
yield break; yield break;

86
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateEnumValue.cs

@ -0,0 +1,86 @@
//
// CreateEnumValue.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// 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<CodeAction> 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<Statement>();
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);
});
}
}
}

26
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationTests.cs

@ -152,6 +152,32 @@ class TestClass
$new System.NotImplementedException (0, ""Hello"", new TestClass ()); $new System.NotImplementedException (0, ""Hello"", new TestClass ());
} }
} }
");
}
[Test]
public void TestBug9664 ()
{
TestWrongContext<CreateConstructorDeclarationAction> (
@"enum Foo
{
Bar,
}
class Baz
{
public Baz (Foo foo)
{
}
}
class Program
{
public void Main ()
{
var b = new Baz (Foo.$Something);
}
}
"); ");
} }
} }

83
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateEnumValueTests.cs

@ -0,0 +1,83 @@
//
// CreateEnumValueTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// 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<CreateEnumValue>(
@"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);
}
}
");
}
}
}

6
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/TestRefactoringContext.cs

@ -121,7 +121,11 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
var startOffset = GetCurrentOffset (insertType.LBraceToken.EndLocation); var startOffset = GetCurrentOffset (insertType.LBraceToken.EndLocation);
foreach (var node in nodes.Reverse ()) { foreach (var node in nodes.Reverse ()) {
var output = OutputNode (1, node, true); 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); output.RegisterTrackedSegments (this, startOffset);
} }
var tcs = new TaskCompletionSource<object> (); var tcs = new TaskCompletionSource<object> ();

22
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -59,6 +59,26 @@
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> <TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<DebugSymbols>false</DebugSymbols> <DebugSymbols>false</DebugSymbols>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|x86' ">
<DebugType>none</DebugType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_5_Debug|AnyCPU' ">
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_5_Release|AnyCPU' ">
<DebugType>none</DebugType>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_5_Debug|x86' ">
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'net_4_5_Release|x86' ">
<DebugType>none</DebugType>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
</PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="Microsoft.CSharp" /> <Reference Include="Microsoft.CSharp" />
<Reference Include="System" /> <Reference Include="System" />
@ -368,6 +388,7 @@
<Compile Include="CSharp\CodeActions\CreateEventInvocatorTests.cs" /> <Compile Include="CSharp\CodeActions\CreateEventInvocatorTests.cs" />
<Compile Include="IndentationTests\IndentationTests.cs" /> <Compile Include="IndentationTests\IndentationTests.cs" />
<Compile Include="CSharp\CodeCompletion\ImportCompletionTests.cs" /> <Compile Include="CSharp\CodeCompletion\ImportCompletionTests.cs" />
<Compile Include="CSharp\CodeActions\CreateEnumValueTests.cs" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\..\cecil\Mono.Cecil.csproj"> <ProjectReference Include="..\..\cecil\Mono.Cecil.csproj">
@ -392,7 +413,6 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="CSharp\" /> <Folder Include="CSharp\" />
<Folder Include="CSharp\CodeActions\AddUsing" />
<Folder Include="CSharp\Parser\" /> <Folder Include="CSharp\Parser\" />
<Folder Include="CSharp\CodeIssues\" /> <Folder Include="CSharp\CodeIssues\" />
<Folder Include="CSharp\CodeActions\" /> <Folder Include="CSharp\CodeActions\" />

Loading…
Cancel
Save