Browse Source

[CodeActions] CreateOverloadWithoutParameterAction: fixed missing ref/out in generated call

newNRvisualizers
Mansheng Yang 13 years ago
parent
commit
0270ce48b0
  1. 18
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateOverloadWithoutParameterAction.cs
  2. 23
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateOverloadWithoutParameterTests.cs

18
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateOverloadWithoutParameterAction.cs

@ -66,15 +66,15 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
Expression argExpr; Expression argExpr;
if (node.ParameterModifier == ParameterModifier.Ref) { if (node.ParameterModifier == ParameterModifier.Ref) {
body.Add (new VariableDeclarationStatement (node.Type.Clone (), node.Name, defaultExpr)); body.Add (new VariableDeclarationStatement (node.Type.Clone (), node.Name, defaultExpr));
argExpr = new DirectionExpression (FieldDirection.Ref, new IdentifierExpression (node.Name)); argExpr = GetArgumentExpression (node);
} else if (node.ParameterModifier == ParameterModifier.Out) { } else if (node.ParameterModifier == ParameterModifier.Out) {
body.Add (new VariableDeclarationStatement (node.Type.Clone (), node.Name)); body.Add (new VariableDeclarationStatement (node.Type.Clone (), node.Name));
argExpr = new DirectionExpression (FieldDirection.Out, new IdentifierExpression (node.Name)); argExpr = GetArgumentExpression (node);
} else { } else {
argExpr = defaultExpr; argExpr = defaultExpr;
} }
body.Add (new InvocationExpression (new IdentifierExpression (methodDecl.Name), body.Add (new InvocationExpression (new IdentifierExpression (methodDecl.Name),
methodDecl.Parameters.Select (param => param == node ? argExpr : new IdentifierExpression (param.Name)))); methodDecl.Parameters.Select (param => param == node ? argExpr : GetArgumentExpression(param))));
var decl = (MethodDeclaration)methodDecl.Clone (); var decl = (MethodDeclaration)methodDecl.Clone ();
decl.Parameters.Remove (decl.Parameters.First (param => param.Name == node.Name)); decl.Parameters.Remove (decl.Parameters.First (param => param.Name == node.Name));
@ -87,6 +87,18 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
}); });
} }
static Expression GetArgumentExpression(ParameterDeclaration parameter)
{
var identifierExpr = new IdentifierExpression(parameter.Name);
switch (parameter.ParameterModifier) {
case ParameterModifier.Out:
return new DirectionExpression (FieldDirection.Out, identifierExpr);
case ParameterModifier.Ref:
return new DirectionExpression (FieldDirection.Ref, identifierExpr);
}
return identifierExpr;
}
static Expression GetDefaultValueExpression (RefactoringContext context, AstType astType) static Expression GetDefaultValueExpression (RefactoringContext context, AstType astType)
{ {
var type = context.ResolveType (astType); var type = context.ResolveType (astType);

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

@ -203,6 +203,29 @@ class Test : ITest
void ITest.Test (int a, int $b) void ITest.Test (int a, int $b)
{ {
} }
}");
}
[Test]
public void TestGenereatedCall ()
{
Test<CreateOverloadWithoutParameterAction> (
@"class Test
{
void TestMethod (ref int $i, ref int j, out int k)
{
}
}",
@"class Test
{
void TestMethod (ref int j, out int k)
{
int i = 0;
TestMethod (ref i, ref j, out k);
}
void TestMethod (ref int i, ref int j, out int k)
{
}
}"); }");
} }
} }

Loading…
Cancel
Save