Browse Source

[CodeActions] ConvertAsToCastAction: fixed some parentheses issues

newNRvisualizers
Mansheng Yang 14 years ago
parent
commit
84126bbddc
  1. 20
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertAsToCastAction.cs
  2. 42
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ConvertAsToCastTests.cs

20
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/ConvertAsToCastAction.cs

@ -32,13 +32,27 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -32,13 +32,27 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
[ContextAction("Convert 'as' to cast.", Description = "Convert 'as' to cast.")]
public class ConvertAsToCastAction : SpecializedCodeAction <AsExpression>
{
static InsertParenthesesVisitor insertParentheses = new InsertParenthesesVisitor ();
protected override CodeAction GetAction (RefactoringContext context, AsExpression node)
{
if (!node.AsToken.Contains (context.Location))
return null;
return new CodeAction (context.TranslateString ("Convert 'as' to cast"),
script => script.Replace (node, new CastExpression (node.Type.Clone (), node.Expression.Clone ())));
return new CodeAction (context.TranslateString ("Convert 'as' to cast"), script => {
var castExpr = new CastExpression (node.Type.Clone (), node.Expression.Clone ());
var parenthesizedExpr = node.Parent as ParenthesizedExpression;
if (parenthesizedExpr != null && parenthesizedExpr.Parent is Expression) {
// clone parent expression and replace the ParenthesizedExpression with castExpr to remove
// parentheses, then insert parentheses if necessary
var parentExpr = (Expression)parenthesizedExpr.Parent.Clone ();
parentExpr.GetNodeContaining (parenthesizedExpr.StartLocation, parenthesizedExpr.EndLocation)
.ReplaceWith (castExpr);
parentExpr.AcceptVisitor (insertParentheses);
script.Replace (parenthesizedExpr.Parent, parentExpr);
} else {
castExpr.AcceptVisitor (insertParentheses);
script.Replace (node, castExpr);
}
});
}
}
}

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

@ -34,7 +34,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -34,7 +34,7 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
public class ConvertAsToCastTests : ContextActionTestBase
{
[Test]
public void Test()
public void Test ()
{
Test<ConvertAsToCastAction> (@"
using System;
@ -54,5 +54,45 @@ class TestClass @@ -54,5 +54,45 @@ class TestClass
}
}");
}
[Test]
public void TestRemoveParentheses ()
{
string input = @"
class TestClass {
void TestMethod (object o)
{
var b = 1 + (o $as TestClass);
}
}";
string output = @"
class TestClass {
void TestMethod (object o)
{
var b = 1 + (TestClass)o;
}
}";
Test<ConvertAsToCastAction> (input, output);
}
[Test]
public void TestInsertParentheses ()
{
string input = @"
class TestClass {
void TestMethod (object o)
{
var b = 1 + o $as TestClass;
}
}";
string output = @"
class TestClass {
void TestMethod (object o)
{
var b = (TestClass)(1 + o);
}
}";
Test<ConvertAsToCastAction> (input, output);
}
}
}

Loading…
Cancel
Save