Browse Source

Added create constructor declaration action/fixed bugs in other

creation actions.
newNRvisualizers
Mike Krüger 14 years ago
parent
commit
d4f9318a6f
  1. 1
      ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
  2. 63
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs
  3. 15
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateIndexerAction.cs
  4. 12
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateMethodDeclarationAction.cs
  5. 3
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs
  6. 3
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertForeachToForTests.cs
  7. 158
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationActionTests.cs
  8. 9
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs
  9. 14
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateIndexerTests.cs
  10. 3
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs
  11. 14
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateMethodDeclarationTests.cs
  12. 3
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs
  13. 6
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveBackingStoreTests.cs
  14. 1
      ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

1
ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj

@ -354,6 +354,7 @@ @@ -354,6 +354,7 @@
<Compile Include="Refactoring\CodeActions\InlineLocalVariableAction.cs" />
<Compile Include="Refactoring\CodeActions\DeclareLocalVariableAction.cs" />
<Compile Include="Refactoring\CodeIssues\InconsistentNamingIssue\WordParser.cs" />
<Compile Include="Refactoring\CodeActions\CreateConstructorDeclarationAction.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ICSharpCode.NRefactory\ICSharpCode.NRefactory.csproj">

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

@ -0,0 +1,63 @@ @@ -0,0 +1,63 @@
//
// CreateClassDeclarationAction.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin <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.Collections.Generic;
using ICSharpCode.NRefactory.CSharp.Resolver;
namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
[ContextAction("Create constructor", Description = "Creates a constructor declaration out of an object creation.")]
public class CreateConstructorDeclarationAction : ICodeActionProvider
{
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var createExpression = context.GetNode<ObjectCreateExpression>();
if (createExpression == null) {
yield break;
}
var resolveResult = context.Resolve(createExpression) as CSharpInvocationResolveResult;
if (resolveResult == null || !resolveResult.IsError || resolveResult.Member.DeclaringTypeDefinition == null || resolveResult.Member.DeclaringTypeDefinition.IsSealed || resolveResult.Member.DeclaringTypeDefinition.Region.IsEmpty) {
yield break;
}
yield return new CodeAction(context.TranslateString("Create constructor"), script => {
var decl = new ConstructorDeclaration() {
Name = resolveResult.Member.DeclaringTypeDefinition.Name,
Modifiers = Modifiers.Public,
Body = new BlockStatement() {
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
}
};
foreach (var parameter in CreateMethodDeclarationAction.GenerateParameters (context, createExpression.Arguments)) {
decl.Parameters.Add(parameter);
}
script.InsertWithCursor(context.TranslateString("Create constructor"), decl, resolveResult.Member.DeclaringTypeDefinition);
});
}
}
}

15
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateIndexerAction.cs

@ -37,13 +37,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -37,13 +37,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var indexer = context.GetNode<IndexerExpression>();
if (indexer == null) {
if (indexer == null)
yield break;
}
if (!(context.Resolve(indexer).IsError)) {
if (!(context.Resolve(indexer).IsError))
yield break;
}
var state = context.GetResolverStateBefore(indexer);
var guessedType = CreateFieldAction.GuessAstType(context, indexer);
@ -55,10 +52,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -55,10 +52,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
bool isStatic;
if (createInOtherType) {
if (targetResolveResult.Type.GetDefinition() == null || targetResolveResult.Type.GetDefinition().Region.IsEmpty)
yield break;
isStatic = targetResolveResult is TypeResolveResult;
if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface) {
if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface)
yield break;
}
} else {
isStatic = indexer.Target is IdentifierExpression && state.CurrentMember.IsStatic;
}
@ -80,9 +78,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -80,9 +78,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
foreach (var parameter in CreateMethodDeclarationAction.GenerateParameters (context, indexer.Arguments)) {
decl.Parameters.Add(parameter);
}
if (isStatic) {
if (isStatic)
decl.Modifiers |= Modifiers.Static;
}
if (createInOtherType) {
if (targetResolveResult.Type.Kind == TypeKind.Interface) {

12
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateMethodDeclarationAction.cs

@ -105,14 +105,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -105,14 +105,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public IEnumerable<CodeAction> GetActionsFromInvocation(RefactoringContext context, InvocationExpression invocation)
{
if (!(context.Resolve(invocation.Target).IsError)) {
if (!(context.Resolve(invocation.Target).IsError))
yield break;
}
var methodName = GetMethodName(invocation);
if (methodName == null) {
if (methodName == null)
yield break;
}
var state = context.GetResolverStateBefore(invocation);
var guessedType = invocation.Parent is ExpressionStatement ? new PrimitiveType("void") : CreateFieldAction.GuessAstType(context, invocation);
@ -125,10 +123,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -125,10 +123,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
bool isStatic;
if (createInOtherType) {
if (targetResolveResult.Type.GetDefinition() == null || targetResolveResult.Type.GetDefinition().Region.IsEmpty)
yield break;
isStatic = targetResolveResult is TypeResolveResult;
if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface) {
if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface)
yield break;
}
} else {
isStatic = invocation.Target is IdentifierExpression && state.CurrentMember.IsStatic;
}
@ -159,7 +158,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -159,7 +158,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
decl.Modifiers = Modifiers.None;
} else {
decl.Modifiers |= Modifiers.Public;
}
script.InsertWithCursor(context.TranslateString("Create method"), decl, targetResolveResult.Type.GetDefinition());

3
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs

@ -82,8 +82,9 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -82,8 +82,9 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
return context.doc.Text;
}
protected static void TestWrongContext (ICodeActionProvider action, string input)
protected static void TestWrongContext<T> (string input) where T : ICodeActionProvider, new ()
{
ICodeActionProvider action = new T ();
var context = TestRefactoringContext.Create (input);
bool isValid = action.GetActions (context).Any ();
if (!isValid)

3
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertForeachToForTests.cs

@ -99,8 +99,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -99,8 +99,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestEnumerableOfT ()
{
TestWrongContext (
new ConvertForeachToForAction (),
TestWrongContext<ConvertForeachToForAction> (
"using System;" + Environment.NewLine +
"using System.Collections.Generic;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +

158
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationActionTests.cs

@ -0,0 +1,158 @@ @@ -0,0 +1,158 @@
//
// CreateClassActionTests.cs
//
// Author:
// Mike Krüger <mkrueger@xamarin.com>
//
// Copyright (c) 2012 Xamarin <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 CreateConstructorDeclarationActionTests : ContextActionTestBase
{
[Test()]
public void TestCreateConstructor ()
{
Test<CreateConstructorDeclarationAction> (
@"
class Foo
{
}
class TestClass
{
void TestMethod ()
{
$new Foo (0, ""Hello"");
}
}
", @"
class Foo
{
public Foo (int i, string hello)
{
throw new System.NotImplementedException ();
}
}
class TestClass
{
void TestMethod ()
{
new Foo (0, ""Hello"");
}
}
");
}
[Test()]
public void TestCreateConstructorInnerClass ()
{
Test<CreateConstructorDeclarationAction> (
@"
class TestClass
{
void TestMethod ()
{
$new Foo (0, ""Hello"");
}
class Foo
{
}
}
", @"
class TestClass
{
void TestMethod ()
{
new Foo (0, ""Hello"");
}
class Foo
{
public Foo (int i, string hello)
{
throw new System.NotImplementedException ();
}
}
}
");
}
[Test()]
public void TestCreateConstructorInStaticClass ()
{
TestWrongContext<CreateConstructorDeclarationAction> (
@"
static class Foo
{
}
class TestClass
{
void TestMethod ()
{
$new Foo (0, ""Hello"");
}
}
");
}
[Test()]
public void TestCreateConstructorInSealedClass ()
{
TestWrongContext<CreateConstructorDeclarationAction> (
@"
sealed class Foo
{
}
class TestClass
{
void TestMethod ()
{
$new Foo (0, ""Hello"");
}
}
");
}
[Test()]
public void TestCreateConstructorInFramework ()
{
TestWrongContext<CreateConstructorDeclarationAction> (
@"
class TestClass
{
void TestMethod ()
{
$new System.NotImplementedException (0, ""Hello"", new TestClass ());
}
}
");
}
}
}

9
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs

@ -35,8 +35,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -35,8 +35,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestWrongContext1 ()
{
TestWrongContext (
new CreateFieldAction (),
TestWrongContext<CreateFieldAction> (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
@ -51,8 +50,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -51,8 +50,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestWrongContext2 ()
{
TestWrongContext (
new CreateFieldAction (),
TestWrongContext<CreateFieldAction> (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
@ -68,8 +66,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -68,8 +66,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
public void TestWrongContext3 ()
{
// May be syntactically possible, but very unlikely.
TestWrongContext (
new CreateFieldAction (),
TestWrongContext<CreateFieldAction> (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +

14
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateIndexerTests.cs

@ -151,6 +151,20 @@ class TestClass @@ -151,6 +151,20 @@ class TestClass
fb[0] = 2;
}
}
");
}
[Test()]
public void TestindexerInFrameworkClass ()
{
TestWrongContext<CreateIndexerAction> (
@"class TestClass
{
void TestMethod ()
{
$new System.Buffer ()[0] = 2;
}
}
");
}

3
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs

@ -176,8 +176,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -176,8 +176,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
public void TestWrongContext1 ()
{
// May be syntactically possible, but very unlikely.
TestWrongContext (
new CreateLocalVariableAction (),
TestWrongContext<CreateLocalVariableAction> (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +

14
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateMethodDeclarationTests.cs

@ -551,6 +551,20 @@ namespace Test { @@ -551,6 +551,20 @@ namespace Test {
}");
}
[Test()]
public void TestMethodInFrameworkClass ()
{
TestWrongContext<CreateMethodDeclarationAction> (
@"class TestClass
{
void TestMethod ()
{
$System.Console.ImprovedWriteLine (""Think of it"");
}
}
");
}
}
}

3
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs

@ -207,8 +207,7 @@ class TestClass @@ -207,8 +207,7 @@ class TestClass
public void TestWrongContext1 ()
{
// May be syntactically possible, but very unlikely.
TestWrongContext (
new CreatePropertyAction (),
TestWrongContext<CreatePropertyAction> (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +

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

@ -63,8 +63,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -63,8 +63,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestBug3292 ()
{
TestWrongContext (
new RemoveBackingStoreAction (),
TestWrongContext<RemoveBackingStoreAction> (
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" int field;" + Environment.NewLine +
@ -81,8 +80,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -81,8 +80,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestBug3292Case2 ()
{
TestWrongContext (
new RemoveBackingStoreAction (),
TestWrongContext<RemoveBackingStoreAction> (
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" int field;" + Environment.NewLine +

1
ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj

@ -256,6 +256,7 @@ @@ -256,6 +256,7 @@
<Compile Include="CSharp\CodeActions\CreateIndexerTests.cs" />
<Compile Include="CSharp\CodeActions\InlineLocalVariableTests.cs" />
<Compile Include="CSharp\CodeActions\DeclareLocalVariableTests.cs" />
<Compile Include="CSharp\CodeActions\CreateConstructorDeclarationActionTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Mono.Cecil\Mono.Cecil.csproj">

Loading…
Cancel
Save