diff --git a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
index 6839495b30..369a6b98a1 100644
--- a/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
+++ b/ICSharpCode.NRefactory.CSharp/ICSharpCode.NRefactory.CSharp.csproj
@@ -354,6 +354,7 @@
+
diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs
new file mode 100644
index 0000000000..cab66dd7e3
--- /dev/null
+++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateConstructorDeclarationAction.cs
@@ -0,0 +1,63 @@
+//
+// CreateClassDeclarationAction.cs
+//
+// Author:
+// Mike Krüger
+//
+// Copyright (c) 2012 Xamarin
+//
+// 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 GetActions(RefactoringContext context)
+ {
+ var createExpression = context.GetNode();
+ 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);
+ });
+ }
+ }
+}
\ No newline at end of file
diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateIndexerAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateIndexerAction.cs
index 1767459a47..cc088df5dc 100644
--- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateIndexerAction.cs
+++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateIndexerAction.cs
@@ -37,13 +37,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public IEnumerable GetActions(RefactoringContext context)
{
var indexer = context.GetNode();
- 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
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
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) {
diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateMethodDeclarationAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateMethodDeclarationAction.cs
index 4a20266edf..5ee83581c1 100644
--- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateMethodDeclarationAction.cs
+++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateMethodDeclarationAction.cs
@@ -105,14 +105,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public IEnumerable 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
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
decl.Modifiers = Modifiers.None;
} else {
decl.Modifiers |= Modifiers.Public;
-
}
script.InsertWithCursor(context.TranslateString("Create method"), decl, targetResolveResult.Type.GetDefinition());
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs
index ee8bfe7210..a094c3c002 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs
@@ -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 (string input) where T : ICodeActionProvider, new ()
{
+ ICodeActionProvider action = new T ();
var context = TestRefactoringContext.Create (input);
bool isValid = action.GetActions (context).Any ();
if (!isValid)
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertForeachToForTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertForeachToForTests.cs
index 6af35b47c3..89c7ed3ec3 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertForeachToForTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertForeachToForTests.cs
@@ -99,8 +99,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestEnumerableOfT ()
{
- TestWrongContext (
- new ConvertForeachToForAction (),
+ TestWrongContext (
"using System;" + Environment.NewLine +
"using System.Collections.Generic;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationActionTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationActionTests.cs
new file mode 100644
index 0000000000..9f1819b8fc
--- /dev/null
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateConstructorDeclarationActionTests.cs
@@ -0,0 +1,158 @@
+//
+// CreateClassActionTests.cs
+//
+// Author:
+// Mike Krüger
+//
+// Copyright (c) 2012 Xamarin
+//
+// 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 (
+@"
+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 (
+@"
+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 (
+@"
+static class Foo
+{
+}
+
+class TestClass
+{
+ void TestMethod ()
+ {
+ $new Foo (0, ""Hello"");
+ }
+}
+");
+ }
+
+ [Test()]
+ public void TestCreateConstructorInSealedClass ()
+ {
+ TestWrongContext (
+@"
+sealed class Foo
+{
+}
+
+class TestClass
+{
+ void TestMethod ()
+ {
+ $new Foo (0, ""Hello"");
+ }
+}
+");
+ }
+
+ [Test()]
+ public void TestCreateConstructorInFramework ()
+ {
+
+ TestWrongContext (
+@"
+class TestClass
+{
+ void TestMethod ()
+ {
+ $new System.NotImplementedException (0, ""Hello"", new TestClass ());
+ }
+}
+");
+ }
+ }
+}
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs
index b37e4cc5f9..4bd55042b7 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs
@@ -35,8 +35,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestWrongContext1 ()
{
- TestWrongContext (
- new CreateFieldAction (),
+ TestWrongContext (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
@@ -51,8 +50,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestWrongContext2 ()
{
- TestWrongContext (
- new CreateFieldAction (),
+ TestWrongContext (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
@@ -68,8 +66,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
public void TestWrongContext3 ()
{
// May be syntactically possible, but very unlikely.
- TestWrongContext (
- new CreateFieldAction (),
+ TestWrongContext (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateIndexerTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateIndexerTests.cs
index 4d3076ab6d..1f35b34c12 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateIndexerTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateIndexerTests.cs
@@ -151,6 +151,20 @@ class TestClass
fb[0] = 2;
}
}
+");
+ }
+
+ [Test()]
+ public void TestindexerInFrameworkClass ()
+ {
+ TestWrongContext (
+@"class TestClass
+{
+ void TestMethod ()
+ {
+ $new System.Buffer ()[0] = 2;
+ }
+}
");
}
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs
index 66e78ad6a2..ec63425c8e 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateLocalVariableTests.cs
@@ -176,8 +176,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
public void TestWrongContext1 ()
{
// May be syntactically possible, but very unlikely.
- TestWrongContext (
- new CreateLocalVariableAction (),
+ TestWrongContext (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateMethodDeclarationTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateMethodDeclarationTests.cs
index 18f5d90560..60694104e1 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateMethodDeclarationTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateMethodDeclarationTests.cs
@@ -551,6 +551,20 @@ namespace Test {
}");
}
+ [Test()]
+ public void TestMethodInFrameworkClass ()
+ {
+ TestWrongContext (
+@"class TestClass
+{
+ void TestMethod ()
+ {
+ $System.Console.ImprovedWriteLine (""Think of it"");
+ }
+}
+");
+ }
+
}
}
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs
index 41f69e5d5f..fad2763f16 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs
@@ -207,8 +207,7 @@ class TestClass
public void TestWrongContext1 ()
{
// May be syntactically possible, but very unlikely.
- TestWrongContext (
- new CreatePropertyAction (),
+ TestWrongContext (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveBackingStoreTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveBackingStoreTests.cs
index 9ff3a60db5..edae1c0523 100644
--- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveBackingStoreTests.cs
+++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/RemoveBackingStoreTests.cs
@@ -63,8 +63,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestBug3292 ()
{
- TestWrongContext (
- new RemoveBackingStoreAction (),
+ TestWrongContext (
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" int field;" + Environment.NewLine +
@@ -81,8 +80,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
[Test()]
public void TestBug3292Case2 ()
{
- TestWrongContext (
- new RemoveBackingStoreAction (),
+ TestWrongContext (
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" int field;" + Environment.NewLine +
diff --git a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
index 945d5cc6a6..eed14292e6 100644
--- a/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
+++ b/ICSharpCode.NRefactory.Tests/ICSharpCode.NRefactory.Tests.csproj
@@ -256,6 +256,7 @@
+