Browse Source

[CodeAction] JoinStringAction: handle the case when left operand is a BinaryOperatorExpression

newNRvisualizers
Mansheng Yang 14 years ago
parent
commit
f5d0318106
  1. 13
      ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/JoinStringAction.cs
  2. 5
      ICSharpCode.NRefactory.Tests/CSharp/CodeActions/JoinStringTests.cs

13
ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/JoinStringAction.cs

@ -31,10 +31,19 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring @@ -31,10 +31,19 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring
{
protected override CodeAction GetAction (RefactoringContext context, BinaryOperatorExpression node)
{
var left = node.Left as PrimitiveExpression;
if (node.Operator != BinaryOperatorType.Add)
return null;
PrimitiveExpression left;
var leftBinaryOperatorExpr = node.Left as BinaryOperatorExpression;
if (leftBinaryOperatorExpr != null && leftBinaryOperatorExpr.Operator == BinaryOperatorType.Add) {
left = leftBinaryOperatorExpr.Right as PrimitiveExpression;
} else {
left = node.Left as PrimitiveExpression;
}
var right = node.Right as PrimitiveExpression;
if (node.Operator != BinaryOperatorType.Add || left == null || right == null ||
if (left == null || right == null ||
!(left.Value is string) || !(right.Value is string) || !node.OperatorToken.Contains(context.Location))
return null;

5
ICSharpCode.NRefactory.Tests/CSharp/CodeActions/JoinStringTests.cs

@ -37,14 +37,14 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions @@ -37,14 +37,14 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions
Test<JoinStringAction> (@"
class TestClass
{
string TestMethod ()
string TestMethod (string arg)
{
return " + input + @";
}
}", @"
class TestClass
{
string TestMethod ()
string TestMethod (string arg)
{
return " + output + @";
}
@ -55,6 +55,7 @@ class TestClass @@ -55,6 +55,7 @@ class TestClass
public void TestRegularString ()
{
Test ("\"a\" $+ \"a\"", "\"aa\"");
Test ("arg + \"a\" $+ \"a\"", "arg + \"aa\"");
}
[Test]

Loading…
Cancel
Save