Browse Source

DeclareLocalVariable can now remove redundant parens.

pull/32/merge
Mike Krüger 13 years ago
parent
commit
25445fcd55
  1. 27
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/DeclareLocalVariableAction.cs
  2. 49
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs

27
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/DeclareLocalVariableAction.cs

@ -63,18 +63,19 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -63,18 +63,19 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
var name = CreateMethodDeclarationAction.CreateBaseName(expr, guessedType);
var type = context.UseExplicitTypes ? context.CreateShortType(guessedType) : new SimpleType("var");
var varDecl = new VariableDeclarationStatement(type, name, expr.Clone());
if (expr.Parent is ExpressionStatement) {
script.Replace(expr.Parent, varDecl);
var replaceNode = visitor.Matches.First () as Expression;
if (replaceNode.Parent is ExpressionStatement) {
script.Replace(replaceNode.Parent, varDecl);
script.Select(varDecl.Variables.First().NameToken);
} else {
var containing = expr.Parent;
var containing = replaceNode.Parent;
while (!(containing.Parent is BlockStatement)) {
containing = containing.Parent;
}
script.InsertBefore(containing, varDecl);
var identifierExpression = new IdentifierExpression(name);
script.Replace(expr, identifierExpression);
script.Replace(replaceNode, identifierExpression);
script.Link(varDecl.Variables.First().NameToken, identifierExpression);
}
});
@ -149,14 +150,24 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -149,14 +150,24 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
public SearchNodeVisitior (AstNode searchForNode)
{
this.searchForNode = searchForNode;
Matches.Add (searchForNode);
AddNode (searchForNode);
}
void AddNode(AstNode node)
{
if (node.Parent is ParenthesizedExpression) {
Matches.Add(node.Parent);
} else {
Matches.Add(node);
}
}
protected override void VisitChildren(AstNode node)
{
if (node.StartLocation > searchForNode.StartLocation && node.IsMatch (searchForNode))
Matches.Add (node);
if (node.StartLocation > searchForNode.StartLocation && node.IsMatch(searchForNode)) {
AddNode(node);
return;
}
base.VisitChildren (node);
}
}

49
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/DeclareLocalVariableTests.cs

@ -217,6 +217,53 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -217,6 +217,53 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
}");
}
[Test]
public void TestRemoveParens ()
{
TestRefactoringContext.UseExplict = true;
Test<DeclareLocalVariableAction> (@"class TestClass
{
void Test ()
{
Console.WriteLine ((<- 1 + 9 ->).ToString ());
}
}
", @"class TestClass
{
void Test ()
{
int i = 1 + 9;
Console.WriteLine (i.ToString ());
}
}
");
}
[Test]
public void TestRemoveParensComplexCase ()
{
TestRefactoringContext.UseExplict = true;
Test<DeclareLocalVariableAction> (@"class TestClass
{
void Test ()
{
int a = <-1 + 9->;
Console.WriteLine ((1 + 9).ToString ());
Console.WriteLine ((1 + 9));
}
}
", @"class TestClass
{
void Test ()
{
int i = 1 + 9;
int a = i;
Console.WriteLine (i.ToString ());
Console.WriteLine (i);
}
}
", 1);
}
}
}

Loading…
Cancel
Save