Browse Source

Improved create class action.

pull/32/merge
Mike Krüger 13 years ago
parent
commit
f8259b4b8b
  1. 48
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateClassDeclarationAction.cs
  2. 34
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateClassDeclarationTests.cs

48
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)) if (simpleType != null && !(simpleType.Parent is EventDeclaration || simpleType.Parent is CustomEventDeclaration))
return GetActions(context, simpleType); return GetActions(context, simpleType);
var identifier = context.GetNode<IdentifierExpression>();
if (identifier != null && (identifier.Parent is MemberReferenceExpression))
return GetActions(context, identifier);
return Enumerable.Empty<CodeAction>(); return Enumerable.Empty<CodeAction>();
} }
@ -73,13 +77,27 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
static TypeDeclaration CreateType(RefactoringContext context, NamingConventionService service, AstNode node) static TypeDeclaration CreateType(RefactoringContext context, NamingConventionService service, AstNode node)
{ {
var result = node is SimpleType ? TypeDeclaration result;
CreateClassFromType(context, (SimpleType)node) : if (node is SimpleType) {
CreateClassFromObjectCreation(context, (ObjectCreateExpression)node); 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); return AddBaseTypesAccordingToNamingRules(context, service, result);
} }
static TypeDeclaration CreateClassFromIdentifier(RefactoringContext context, IdentifierExpression identifierExpression)
{
var result = new TypeDeclaration { Name = identifierExpression.Identifier };
var entity = identifierExpression.GetParent<EntityDeclaration>();
if (entity != null)
result.Modifiers |= entity.Modifiers & Modifiers.Public;
return result;
}
static TypeDeclaration CreateClassFromType(RefactoringContext context, SimpleType simpleType) static TypeDeclaration CreateClassFromType(RefactoringContext context, SimpleType simpleType)
{ {
TypeDeclaration result; TypeDeclaration result;
@ -90,11 +108,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
className += "Attribute"; className += "Attribute";
} }
result = new TypeDeclaration() { Name = className }; result = new TypeDeclaration { Name = className };
var entity = simpleType.GetParent<EntityDeclaration>(); var entity = simpleType.GetParent<EntityDeclaration>();
if (entity != null) if (entity != null)
result.Modifiers |= entity.Modifiers & Modifiers.Public; result.Modifiers |= entity.Modifiers & Modifiers.Public;
return result; return result;
} }
@ -103,16 +121,16 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
TypeDeclaration result; TypeDeclaration result;
string className = createExpression.Type.GetText(); string className = createExpression.Type.GetText();
if (!createExpression.Arguments.Any()) { if (!createExpression.Arguments.Any()) {
result = new TypeDeclaration() { Name = className }; result = new TypeDeclaration { Name = className };
} else { } else {
var decl = new ConstructorDeclaration() { var decl = new ConstructorDeclaration {
Name = className, Name = className,
Modifiers = Modifiers.Public, Modifiers = Modifiers.Public,
Body = new BlockStatement() { Body = new BlockStatement {
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException"))) new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
} }
}; };
result = new TypeDeclaration() { result = new TypeDeclaration {
Name = className, Name = className,
Members = { Members = {
decl decl
@ -150,13 +168,13 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return Modifiers.Override; 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 ()) { foreach (var property in guessedType.GetProperties ()) {
if (!property.IsAbstract) if (!property.IsAbstract)
continue; continue;
if (property.IsIndexer) { if (property.IsIndexer) {
var indexerDecl = new IndexerDeclaration() { var indexerDecl = new IndexerDeclaration {
ReturnType = context.CreateShortType(property.ReturnType), ReturnType = context.CreateShortType(property.ReturnType),
Modifiers = GetModifiers(property), Modifiers = GetModifiers(property),
Name = property.Name Name = property.Name
@ -169,7 +187,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
result.AddChild(indexerDecl, Roles.TypeMemberRole); result.AddChild(indexerDecl, Roles.TypeMemberRole);
continue; continue;
} }
var propDecl = new PropertyDeclaration() { var propDecl = new PropertyDeclaration {
ReturnType = context.CreateShortType(property.ReturnType), ReturnType = context.CreateShortType(property.ReturnType),
Modifiers = GetModifiers (property), Modifiers = GetModifiers (property),
Name = property.Name Name = property.Name
@ -184,11 +202,11 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
foreach (var method in guessedType.GetMethods ()) { foreach (var method in guessedType.GetMethods ()) {
if (!method.IsAbstract) if (!method.IsAbstract)
continue; continue;
var decl = new MethodDeclaration() { var decl = new MethodDeclaration {
ReturnType = context.CreateShortType(method.ReturnType), ReturnType = context.CreateShortType(method.ReturnType),
Modifiers = GetModifiers (method), Modifiers = GetModifiers (method),
Name = method.Name, Name = method.Name,
Body = new BlockStatement() { Body = new BlockStatement {
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException"))) new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
} }
}; };
@ -199,7 +217,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
foreach (var evt in guessedType.GetEvents ()) { foreach (var evt in guessedType.GetEvents ()) {
if (!evt.IsAbstract) if (!evt.IsAbstract)
continue; continue;
var decl = new EventDeclaration() { var decl = new EventDeclaration {
ReturnType = context.CreateShortType(evt.ReturnType), ReturnType = context.CreateShortType(evt.ReturnType),
Modifiers = GetModifiers (evt), Modifiers = GetModifiers (evt),
Name = evt.Name Name = evt.Name

34
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateClassDeclarationTests.cs

@ -224,7 +224,7 @@ class TestClass
"); ");
} }
[Test()] [Test]
public void TestNotShowInEventTypes() public void TestNotShowInEventTypes()
{ {
TestWrongContext<CreateClassDeclarationAction>( TestWrongContext<CreateClassDeclarationAction>(
@ -236,7 +236,7 @@ class TestClass
"); ");
} }
[Test()] [Test]
public void TestCreateClassImplementingInterface() public void TestCreateClassImplementingInterface()
{ {
Test<CreateClassDeclarationAction>( Test<CreateClassDeclarationAction>(
@ -266,7 +266,7 @@ class TestClass
"); ");
} }
[Test()] [Test]
public void TestCreateClassExtendingAbstractClass() public void TestCreateClassExtendingAbstractClass()
{ {
Test<CreateClassDeclarationAction>( Test<CreateClassDeclarationAction>(
@ -301,7 +301,7 @@ class TestClass
"); ");
} }
[Test()] [Test]
public void TestModifierBug () public void TestModifierBug ()
{ {
Test<CreateClassDeclarationAction> ( Test<CreateClassDeclarationAction> (
@ -322,5 +322,31 @@ class TestClass
} }
[Test]
public void TestCreateClassFromMemberReferenceExpression ()
{
Test<CreateClassDeclarationAction> (
@"
class TestClass
{
void TestMethod ()
{
$Foo.Bar (1);
}
}
", @"
class Foo
{
}
class TestClass
{
void TestMethod ()
{
Foo.Bar (1);
}
}
");
}
} }
} }

Loading…
Cancel
Save