Browse Source

Handled static class <--> non static member clash in create

field/method/property actions.
newNRvisualizers
Mike Krüger 14 years ago
parent
commit
5012f3f633
  1. 39
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs
  2. 57
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateMethodDeclarationAction.cs
  3. 39
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs
  4. 42
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs
  5. 23
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateMethodDeclarationTests.cs
  6. 22
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs

39
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs

@ -47,25 +47,23 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -47,25 +47,23 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var identifier = context.GetNode<IdentifierExpression>();
if (identifier == null) {
if (identifier == null)
yield break;
}
if (IsInvocationTarget(identifier)) {
if (IsInvocationTarget(identifier))
yield break;
}
var statement = identifier.GetParent<Statement>();
if (statement == null) {
if (statement == null)
yield break;
}
if (!(context.Resolve(identifier).IsError)) {
if (!(context.Resolve(identifier).IsError))
yield break;
}
var guessedType = CreateFieldAction.GuessAstType(context, identifier);
if (guessedType == null) {
if (guessedType == null)
yield break;
}
var state = context.GetResolverStateBefore(identifier);
bool isStatic = state.CurrentMember.IsStatic;
if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
yield break;
bool isStatic = state.CurrentMember.IsStatic | state.CurrentTypeDefinition.IsStatic;
// var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
// if (service != null && !service.IsValidName(identifier.Identifier, AffectedEntity.Field, Modifiers.Private, isStatic)) {
@ -77,9 +75,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -77,9 +75,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
ReturnType = guessedType,
Variables = { new VariableInitializer(identifier.Identifier) }
};
if (isStatic) {
if (isStatic)
decl.Modifiers |= Modifiers.Static;
}
script.InsertWithCursor(context.TranslateString("Create field"), decl, Script.InsertPosition.Before);
});
@ -101,9 +98,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -101,9 +98,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
static IEnumerable<IType> GetAllValidTypesFromInvokation(RefactoringContext context, InvocationExpression invoke, AstNode parameter)
{
int index = GetArgumentIndex(invoke.Arguments, parameter);
if (index < 0) {
if (index < 0)
yield break;
}
var targetResult = context.Resolve(invoke.Target);
if (targetResult is MethodGroupResolveResult) {
@ -118,9 +114,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -118,9 +114,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
static IEnumerable<IType> GetAllValidTypesFromObjectCreation(RefactoringContext context, ObjectCreateExpression invoke, AstNode parameter)
{
int index = GetArgumentIndex(invoke.Arguments, parameter);
if (index < 0) {
if (index < 0)
yield break;
}
var targetResult = context.Resolve(invoke.Type);
if (targetResult is TypeResolveResult) {
@ -130,9 +125,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -130,9 +125,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
yield break;
}
foreach (var constructor in type.GetConstructors ()) {
if (index < constructor.Parameters.Count) {
if (index < constructor.Parameters.Count)
yield return constructor.Parameters [index].Type;
}
}
}
}
@ -192,9 +186,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -192,9 +186,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (expr.Parent is ReturnStatement) {
var state = context.GetResolverStateBefore(expr);
if (state != null) {
if (state != null)
return new [] { state.CurrentMember.ReturnType };
}
}
if (expr.Parent is YieldReturnStatement) {
@ -216,9 +209,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -216,9 +209,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var typeInference = new TypeInference(context.Compilation);
typeInference.Algorithm = TypeInferenceAlgorithm.ImprovedReturnAllResults;
var inferedType = typeInference.FindTypeInBounds(type, emptyTypes);
if (inferedType.Kind == TypeKind.Unknown) {
if (inferedType.Kind == TypeKind.Unknown)
return new PrimitiveType("object");
}
return context.CreateShortType(inferedType);
}
@ -229,7 +221,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -229,7 +221,6 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
typeInference.Algorithm = TypeInferenceAlgorithm.ImprovedReturnAllResults;
var inferedType = typeInference.FindTypeInBounds(type, emptyTypes);
return inferedType;
}
#endregion
}

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

@ -38,34 +38,31 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -38,34 +38,31 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var invocation = context.GetNode<InvocationExpression>();
if (invocation != null) {
if (invocation != null)
return GetActionsFromInvocation(context, invocation);
}
var identifier = context.GetNode<IdentifierExpression>();
if (identifier != null) {
if (identifier != null)
return GetActionsFromIdentifier(context, identifier);
}
return Enumerable.Empty<CodeAction>();
}
public IEnumerable<CodeAction> GetActionsFromIdentifier(RefactoringContext context, IdentifierExpression identifier)
{
if (!(context.Resolve(identifier).IsError)) {
if (!(context.Resolve(identifier).IsError))
yield break;
}
var methodName = identifier.Identifier;
var guessedType = CreateFieldAction.GuessType(context, identifier);
if (guessedType.Kind != TypeKind.Delegate) {
if (guessedType.Kind != TypeKind.Delegate)
yield break;
}
var invocationMethod = guessedType.GetDelegateInvokeMethod();
var state = context.GetResolverStateBefore(identifier);
bool isStatic = state.CurrentMember.IsStatic;
if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
yield break;
bool isStatic = state.CurrentMember.IsStatic || state.CurrentTypeDefinition.IsStatic;
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic)) {
if (service != null && !service.IsValidName(methodName, AffectedEntity.Method, Modifiers.Private, isStatic))
yield break;
}
yield return new CodeAction(context.TranslateString("Create delegate handler"), script => {
var decl = new MethodDeclaration() {
@ -75,9 +72,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -75,9 +72,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
new ThrowStatement(new ObjectCreateExpression(context.CreateShortType("System", "NotImplementedException")))
}
};
if (isStatic) {
if (isStatic)
decl.Modifiers |= Modifiers.Static;
}
foreach (var parameter in invocationMethod.Parameters) {
decl.Parameters.Add(new ParameterDeclaration(context.CreateShortType (parameter.Type), parameter.Name) {
@ -91,15 +87,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -91,15 +87,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
static ParameterModifier GetModifiers(IParameter parameter)
{
if (parameter.IsOut) {
if (parameter.IsOut)
return ParameterModifier.Out;
}
if (parameter.IsRef) {
if (parameter.IsRef)
return ParameterModifier.Ref;
}
if (parameter.IsParams) {
if (parameter.IsParams)
return ParameterModifier.Params;
}
return ParameterModifier.None;
}
@ -129,7 +122,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -129,7 +122,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface)
yield break;
} else {
isStatic = invocation.Target is IdentifierExpression && state.CurrentMember.IsStatic;
if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
yield break;
isStatic = state.CurrentMember.IsStatic || state.CurrentTypeDefinition.IsStatic;
}
// var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
@ -146,9 +141,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -146,9 +141,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
};
decl.Parameters.AddRange(GenerateParameters (context, invocation.Arguments));
if (isStatic) {
if (isStatic)
decl.Modifiers |= Modifiers.Static;
}
if (createInOtherType) {
if (targetResolveResult.Type.Kind == TypeKind.Interface) {
@ -168,9 +162,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -168,9 +162,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public static IEnumerable<ParameterDeclaration> GenerateParameters(RefactoringContext context, IEnumerable<Expression> arguments)
{
Dictionary<string, int> nameCounter = new Dictionary<string, int>();
var nameCounter = new Dictionary<string, int>();
foreach (var argument in arguments) {
ParameterModifier direction = ParameterModifier.None;
var direction = ParameterModifier.None;
AstNode node;
if (argument is DirectionExpression) {
var de = (DirectionExpression)argument;
@ -206,9 +200,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -206,9 +200,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
wordStart = true;
continue;
}
if (!char.IsLetter(ch)) {
if (!char.IsLetter(ch))
continue;
}
if (firstLetter) {
sb.Append(char.ToLower(ch));
firstLetter = false;
@ -227,9 +220,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -227,9 +220,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public static string CreateBaseName(AstNode node, IType type)
{
string name = null;
if (node is DirectionExpression) {
if (node is DirectionExpression)
node = ((DirectionExpression)node).Expression;
}
if (node is IdentifierExpression) {
name = ((IdentifierExpression)node).Identifier;
} else if (node is MemberReferenceExpression) {
@ -242,9 +234,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -242,9 +234,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return char.ToLower(type.Name [0]).ToString();
}
} else {
if (type.Kind == TypeKind.Unknown) {
if (type.Kind == TypeKind.Unknown)
return "par";
}
name = GuessNameFromType(type);
}
@ -293,12 +284,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -293,12 +284,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
string GetMethodName(InvocationExpression invocation)
{
if (invocation.Target is IdentifierExpression) {
if (invocation.Target is IdentifierExpression)
return ((IdentifierExpression)invocation.Target).Identifier;
}
if (invocation.Target is MemberReferenceExpression) {
if (invocation.Target is MemberReferenceExpression)
return ((MemberReferenceExpression)invocation.Target).MemberName;
}
return null;
}

39
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs

@ -24,9 +24,6 @@ @@ -24,9 +24,6 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using ICSharpCode.NRefactory.PatternMatching;
using System.Linq;
using System.Threading;
using System.Collections.Generic;
using ICSharpCode.NRefactory.Semantics;
using ICSharpCode.NRefactory.TypeSystem;
@ -38,32 +35,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -38,32 +35,26 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
Expression identifier = context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression) as Expression;
if (identifier == null) {
var identifier = context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression) as Expression;
if (identifier == null)
yield break;
}
if (CreateFieldAction.IsInvocationTarget(identifier)) {
if (CreateFieldAction.IsInvocationTarget(identifier))
yield break;
}
var propertyName = GetPropertyName(identifier);
if (propertyName == null) {
if (propertyName == null)
yield break;
}
var statement = context.GetNode<Statement>();
if (statement == null) {
if (statement == null)
yield break;
}
if (!(context.Resolve(identifier).IsError)) {
if (!(context.Resolve(identifier).IsError))
yield break;
}
var guessedType = CreateFieldAction.GuessAstType(context, identifier);
if (guessedType == null) {
if (guessedType == null)
yield break;
}
var state = context.GetResolverStateBefore(identifier);
bool createInOtherType = false;
@ -76,11 +67,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -76,11 +67,12 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
bool isStatic;
if (createInOtherType) {
isStatic = targetResolveResult is TypeResolveResult;
if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface) {
if (isStatic && targetResolveResult.Type.Kind == TypeKind.Interface)
yield break;
}
} else {
isStatic = identifier is IdentifierExpression && state.CurrentMember.IsStatic;
if (state.CurrentMember == null || state.CurrentTypeDefinition == null)
yield break;
isStatic = state.CurrentMember.IsStatic || state.CurrentTypeDefinition.IsStatic;
}
// var service = (NamingConventionService)context.GetService(typeof(NamingConventionService));
@ -95,9 +87,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -95,9 +87,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
Getter = new Accessor(),
Setter = new Accessor()
};
if (isStatic) {
if (isStatic)
decl.Modifiers |= Modifiers.Static;
}
if (createInOtherType) {
if (targetResolveResult.Type.Kind == TypeKind.Interface) {
@ -115,12 +106,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -115,12 +106,10 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
static string GetPropertyName(Expression expr)
{
if (expr is IdentifierExpression) {
if (expr is IdentifierExpression)
return ((IdentifierExpression)expr).Identifier;
}
if (expr is MemberReferenceExpression) {
if (expr is MemberReferenceExpression)
return ((MemberReferenceExpression)expr).MemberName;
}
return null;
}

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

@ -150,31 +150,27 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -150,31 +150,27 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
}
[Test()]
public void TestStaticField ()
public void TestStaticClassField ()
{
string result = RunContextAction (
new CreateFieldAction (),
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" static void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" $Foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}"
);
// Not 100% correct input code, but should work in that case as well.
Test<CreateFieldAction> (@"static class TestClass
{
public TestClass ()
{
$foo = 5;
}
}", @"static class TestClass
{
static int foo;
Assert.AreEqual (
"using System;" + Environment.NewLine +
"class TestClass" + Environment.NewLine +
"{" + Environment.NewLine +
" static int Foo;" + Environment.NewLine +
"" + Environment.NewLine +
" static void Test ()" + Environment.NewLine +
" {" + Environment.NewLine +
" Foo = 0x10;" + Environment.NewLine +
" }" + Environment.NewLine +
"}", result);
public TestClass ()
{
foo = 5;
}
}");
}
}
}

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

@ -579,6 +579,29 @@ class Test @@ -579,6 +579,29 @@ class Test
");
}
[Test()]
public void TestStaticClassMethod ()
{
// Not 100% correct input code, but should work in that case as well.
Test<CreateMethodDeclarationAction> (@"static class TestClass
{
public TestClass ()
{
$Foo (5);
}
}", @"static class TestClass
{
static void Foo (int i)
{
throw new System.NotImplementedException ();
}
public TestClass ()
{
Foo (5);
}
}");
}
}
}

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

@ -219,6 +219,28 @@ class TestClass @@ -219,6 +219,28 @@ class TestClass
);
}
[Test()]
public void TestStaticClassProperty ()
{
// Not 100% correct input code, but should work in that case as well.
Test<CreatePropertyAction> (@"static class TestClass
{
public TestClass ()
{
$Foo = 5;
}
}", @"static class TestClass
{
static int Foo {
get;
set;
}
public TestClass ()
{
Foo = 5;
}
}");
}
}
}
Loading…
Cancel
Save