Skip to content
Browse files

Added more cases for create class declaration.

  • Loading branch information...
1 parent 93fb777 commit 718dfe4685b5ecaed930a21523030d6287f90c8c @mkrueger mkrueger committed Mar 27, 2012
View
94 ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateClassDeclarationAction.cs
@@ -36,10 +36,19 @@ public class CreateClassDeclarationAction : ICodeActionProvider
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var createExpression = context.GetNode<ObjectCreateExpression>();
- if (createExpression == null)
- yield break;
+ if (createExpression != null)
+ return GetActions(context, createExpression);
- var resolveResult = context.Resolve(createExpression) as UnknownIdentifierResolveResult;
+ var simpleType = context.GetNode<SimpleType>();
+ if (simpleType != null)
+ return GetActions(context, simpleType);
+
+ return Enumerable.Empty<CodeAction>();
+ }
+
+ static IEnumerable<CodeAction> GetActions(RefactoringContext context, AstNode node)
+ {
+ var resolveResult = context.Resolve(node) as UnknownIdentifierResolveResult;
if (resolveResult == null)
yield break;
@@ -49,35 +58,76 @@ public IEnumerable<CodeAction> GetActions(RefactoringContext context)
}
yield return new CodeAction(context.TranslateString("Create class"), script => {
- script.CreateNewType(CreateClassDeclaration(context, createExpression));
+ script.CreateNewType(CreateType(context, service, node));
});
yield return new CodeAction(context.TranslateString("Create nested class"), script => {
- script.InsertWithCursor(context.TranslateString("Create nested class"), CreateClassDeclaration(context, createExpression), Script.InsertPosition.Before);
+ script.InsertWithCursor(context.TranslateString("Create nested class"), CreateType(context, service, node), Script.InsertPosition.Before);
});
}
- static TypeDeclaration CreateClassDeclaration(RefactoringContext context, ObjectCreateExpression createExpression)
+ static TypeDeclaration CreateType(RefactoringContext context, NamingConventionService service, AstNode node)
+ {
+ var result = node is SimpleType ?
+ CreateClassFromType(context, (SimpleType)node) :
+ CreateClassFromObjectCreation(context, (ObjectCreateExpression)node);
+
+ return AddBaseTypesAccordingToNamingRules(context, service, result);
+ }
+
+ static TypeDeclaration CreateClassFromType(RefactoringContext context, SimpleType simpleType)
+ {
+ TypeDeclaration result;
+ string className = simpleType.Identifier;
+
+ if (simpleType.Parent is Attribute) {
+ if (!className.EndsWith("Attribute"))
+ className += "Attribute";
+ }
+
+ result = new TypeDeclaration() { Name = className };
+ var entity = simpleType.GetParent<EntityDeclaration>();
+ if (entity != null)
+ result.Modifiers |= entity.Modifiers & ~Modifiers.Internal;
+
+ return result;
+ }
+
+ static TypeDeclaration CreateClassFromObjectCreation(RefactoringContext context, ObjectCreateExpression createExpression)
{
+ TypeDeclaration result;
string className = createExpression.Type.GetText();
if (!createExpression.Arguments.Any()) {
- return new TypeDeclaration() { Name = className };
+ result = new TypeDeclaration() { Name = className };
+ } else {
+ var decl = new ConstructorDeclaration() {
+ Name = className,
+ Modifiers = Modifiers.Public,
+ Body = new BlockStatement() {
+ new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
+ }
+ };
+ result = new TypeDeclaration() {
+ Name = className,
+ Members = {
+ decl
+ }
+ };
+ decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));
}
- var decl = new ConstructorDeclaration() {
- Name = className,
- Modifiers = Modifiers.Public,
- Body = new BlockStatement() {
- new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
- }
- };
- decl.Parameters.AddRange(CreateMethodDeclarationAction.GenerateParameters(context, createExpression.Arguments));
-
- return new TypeDeclaration() {
- Name = className,
- Members = {
- decl
- }
- };
+ return result;
+ }
+
+ static TypeDeclaration AddBaseTypesAccordingToNamingRules(RefactoringContext context, NamingConventionService service, TypeDeclaration result)
+ {
+ if (service.HasValidRule(result.Name, AffectedEntity.CustomAttributes, Modifiers.Public)) {
+ result.BaseTypes.Add(context.CreateShortType("System", "Attribute"));
+ } else if (service.HasValidRule(result.Name, AffectedEntity.CustomEventArgs, Modifiers.Public)) {
+ result.BaseTypes.Add(context.CreateShortType("System", "EventArgs"));
+ } else if (service.HasValidRule(result.Name, AffectedEntity.CustomExceptions, Modifiers.Public)) {
+ result.BaseTypes.Add(context.CreateShortType("System", "Exception"));
+ }
+ return result;
}
}
}
View
19 ...efactory.CSharp/Refactoring/CodeIssues/InconsistentNamingIssue/NamingConventionService.cs
@@ -53,6 +53,25 @@ public bool IsValidName(string name, AffectedEntity entity, Modifiers accessibil
}
return true;
}
+
+ public bool HasValidRule(string name, AffectedEntity entity, Modifiers accessibilty = Modifiers.Private, bool isStatic = false)
+ {
+ foreach (var rule in Rules) {
+ if (!rule.AffectedEntity.HasFlag(entity)) {
+ continue;
+ }
+ if (!rule.VisibilityMask.HasFlag(accessibilty)) {
+ continue;
+ }
+ if (isStatic && !rule.IncludeStaticEntities || !isStatic && !rule.IncludeInstanceMembers) {
+ continue;
+ }
+ if (rule.IsValid(name)) {
+ return true;
+ }
+ }
+ return false;
+ }
}
}
View
106 ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateClassDeclarationTests.cs
@@ -118,5 +118,111 @@ void TestMethod ()
");
}
+ [Test()]
+ public void TestCreatePublicEventArgs ()
+ {
+ Test<CreateClassDeclarationAction> (
+@"
+class TestClass
+{
+ public event EventHandler<$MyEventArgs> evt;
+}
+", @"
+public class MyEventArgs : System.EventArgs
+{
+}
+class TestClass
+{
+ public event EventHandler<MyEventArgs> evt;
+}
+");
+ }
+
+ [Test()]
+ public void TestCreateInternalEventArgs ()
+ {
+ Test<CreateClassDeclarationAction> (
+@"
+class TestClass
+{
+ internal event EventHandler<$MyEventArgs> evt;
+}
+", @"
+class MyEventArgs : System.EventArgs
+{
+}
+class TestClass
+{
+ internal event EventHandler<MyEventArgs> evt;
+}
+");
+ }
+
+ [Test()]
+ public void TestCreateAttribute ()
+ {
+ Test<CreateClassDeclarationAction> (
+@"
+[$MyAttribute]
+class TestClass
+{
+}
+", @"
+class MyAttribute : System.Attribute
+{
+}
+[MyAttribute]
+class TestClass
+{
+}
+");
+ }
+
+ [Test()]
+ public void TestCreateAttributeCase2 ()
+ {
+ Test<CreateClassDeclarationAction> (
+@"
+[$My]
+class TestClass
+{
+}
+", @"
+class MyAttribute : System.Attribute
+{
+}
+[My]
+class TestClass
+{
+}
+");
+ }
+
+ [Test()]
+ public void TestCreateException ()
+ {
+ Test<CreateClassDeclarationAction> (
+@"
+class TestClass
+{
+ void TestMethod ()
+ {
+ throw $new MyException ();
+ }
+}
+", @"
+class MyException : System.Exception
+{
+}
+class TestClass
+{
+ void TestMethod ()
+ {
+ throw new MyException ();
+ }
+}
+");
+ }
+
}
}

0 comments on commit 718dfe4

Please sign in to comment.
Something went wrong with that request. Please try again.