Browse Source

[CodeAction] Fixed bug in create field/property action.

newNRvisualizers
Mike Krüger 13 years ago
parent
commit
b1a5ccffb1
  1. 10
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs
  2. 4
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs
  3. 24
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs
  4. 29
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs

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

@ -44,9 +44,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -44,9 +44,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return invoke != null && invoke.Target == node;
}
internal static Expression GetCreatePropertyOrFieldNode(RefactoringContext context)
{
return context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression || n is NamedExpression) as Expression;
}
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var expr = context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression) as Expression;
var expr = GetCreatePropertyOrFieldNode(context);
if (expr == null)
yield break;
@ -174,6 +179,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -174,6 +179,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}
if (expr.Parent is ArrayInitializerExpression) {
if (expr is NamedExpression)
return new [] { resolver.Resolve(((NamedExpression)expr).Expression).Type };
var aex = expr.Parent as ArrayInitializerExpression;
if (aex.IsSingleElement)
aex = aex.Parent as ArrayInitializerExpression;

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

@ -35,7 +35,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -35,7 +35,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
public IEnumerable<CodeAction> GetActions(RefactoringContext context)
{
var identifier = context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression) as Expression;
var identifier = CreateFieldAction.GetCreatePropertyOrFieldNode (context);
if (identifier == null)
yield break;
if (CreateFieldAction.IsInvocationTarget(identifier))
@ -118,6 +118,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -118,6 +118,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
return ((IdentifierExpression)expr).Identifier;
if (expr is MemberReferenceExpression)
return ((MemberReferenceExpression)expr).MemberName;
if (expr is NamedExpression)
return ((NamedExpression)expr).Name;
return null;
}

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

@ -209,5 +209,29 @@ class Foo @@ -209,5 +209,29 @@ class Foo
"}", result);
}
[Test()]
public void TestObjectInitializer ()
{
// Not 100% correct input code, but should work in that case as well.
Test<CreateFieldAction> (@"class TestClass
{
void TestMethod ()
{
new TestClass {
$NonExistantProperty = 5
};
}
}", @"class TestClass
{
int NonExistantProperty;
void TestMethod ()
{
new TestClass {
NonExistantProperty = 5
};
}
}");
}
}
}

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

@ -276,6 +276,35 @@ class Foo @@ -276,6 +276,35 @@ class Foo
AEnum e = AEnum.B$ar;
}
}
");
}
[Test()]
public void TestPropertyFromObjectInitializer ()
{
TestCreateProperty (
@"class TestClass
{
void TestMethod ()
{
new TestClass {
$NonExistantProperty = 5
};
}
}
", @"class TestClass
{
int NonExistantProperty {
get;
set;
}
void TestMethod ()
{
new TestClass {
NonExistantProperty = 5
};
}
}
");
}
}

Loading…
Cancel
Save