diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateClassDeclarationAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateClassDeclarationAction.cs index fb9938b29a..cbfe544d85 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateClassDeclarationAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateClassDeclarationAction.cs @@ -44,6 +44,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring if (simpleType != null && !(simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration)) return GetActions(context, simpleType); + var identifier = context.GetNode(); + if (identifier != null && (identifier.Parent is MemberReferenceExpression)) + return GetActions(context, identifier); + return Enumerable.Empty(); } @@ -73,13 +77,27 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring static TypeDeclaration CreateType(RefactoringContext context, NamingConventionService service, AstNode node) { - var result = node is SimpleType ? - CreateClassFromType(context, (SimpleType)node) : - CreateClassFromObjectCreation(context, (ObjectCreateExpression)node); + TypeDeclaration result; + if (node is SimpleType) { + result = CreateClassFromType(context, (SimpleType)node); + } else if (node is ObjectCreateExpression) { + result = CreateClassFromObjectCreation(context, (ObjectCreateExpression)node); + } else { + result = CreateClassFromIdentifier(context, (IdentifierExpression)node); + } return AddBaseTypesAccordingToNamingRules(context, service, result); } + static TypeDeclaration CreateClassFromIdentifier(RefactoringContext context, IdentifierExpression identifierExpression) + { + var result = new TypeDeclaration { Name = identifierExpression.Identifier }; + var entity = identifierExpression.GetParent(); + if (entity != null) + result.Modifiers |= entity.Modifiers & Modifiers.Public; + return result; + } + static TypeDeclaration CreateClassFromType(RefactoringContext context, SimpleType simpleType) { TypeDeclaration result; @@ -90,11 +108,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring className += "Attribute"; } - result = new TypeDeclaration() { Name = className }; + result = new TypeDeclaration { Name = className }; var entity = simpleType.GetParent(); if (entity != null) result.Modifiers |= entity.Modifiers & Modifiers.Public; - + return result; } @@ -103,16 +121,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring TypeDeclaration result; string className = createExpression.Type.GetText(); if (!createExpression.Arguments.Any()) { - result = new TypeDeclaration() { Name = className }; + result = new TypeDeclaration { Name = className }; } else { - var decl = new ConstructorDeclaration() { + var decl = new ConstructorDeclaration { Name = className, Modifiers = Modifiers.Public, - Body = new BlockStatement() { + Body = new BlockStatement { new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException"))) } }; - result = new TypeDeclaration() { + result = new TypeDeclaration { Name = className, Members = { decl @@ -150,13 +168,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring return Modifiers.Override; } - static void AddImplementation(RefactoringContext context, TypeDeclaration result, ICSharpCode.NRefactory.TypeSystem.IType guessedType) + static void AddImplementation(RefactoringContext context, TypeDeclaration result, IType guessedType) { foreach (var property in guessedType.GetProperties ()) { if (!property.IsAbstract) continue; if (property.IsIndexer) { - var indexerDecl = new IndexerDeclaration() { + var indexerDecl = new IndexerDeclaration { ReturnType = context.CreateShortType(property.ReturnType), Modifiers = GetModifiers(property), Name = property.Name @@ -169,7 +187,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring result.AddChild(indexerDecl, Roles.TypeMemberRole); continue; } - var propDecl = new PropertyDeclaration() { + var propDecl = new PropertyDeclaration { ReturnType = context.CreateShortType(property.ReturnType), Modifiers = GetModifiers (property), Name = property.Name @@ -184,11 +202,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring foreach (var method in guessedType.GetMethods ()) { if (!method.IsAbstract) continue; - var decl = new MethodDeclaration() { + var decl = new MethodDeclaration { ReturnType = context.CreateShortType(method.ReturnType), Modifiers = GetModifiers (method), Name = method.Name, - Body = new BlockStatement() { + Body = new BlockStatement { new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException"))) } }; @@ -199,7 +217,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring foreach (var evt in guessedType.GetEvents ()) { if (!evt.IsAbstract) continue; - var decl = new EventDeclaration() { + var decl = new EventDeclaration { ReturnType = context.CreateShortType(evt.ReturnType), Modifiers = GetModifiers (evt), Name = evt.Name diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateClassDeclarationTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateClassDeclarationTests.cs index c70de1d472..1aae1d7129 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateClassDeclarationTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateClassDeclarationTests.cs @@ -224,7 +224,7 @@ class TestClass "); } - [Test()] + [Test] public void TestNotShowInEventTypes() { TestWrongContext( @@ -236,7 +236,7 @@ class TestClass "); } - [Test()] + [Test] public void TestCreateClassImplementingInterface() { Test( @@ -266,7 +266,7 @@ class TestClass "); } - [Test()] + [Test] public void TestCreateClassExtendingAbstractClass() { Test( @@ -301,7 +301,7 @@ class TestClass "); } - [Test()] + [Test] public void TestModifierBug () { Test ( @@ -322,5 +322,31 @@ class TestClass } + [Test] + public void TestCreateClassFromMemberReferenceExpression () + { + Test ( + @" +class TestClass +{ + void TestMethod () + { + $Foo.Bar (1); + } +} +", @" +class Foo +{ +} +class TestClass +{ + void TestMethod () + { + Foo.Bar (1); + } +} +"); + } + } }