IDEADEV-40959: ClassCastException at ConvertConcatenationToGstringIntention.processIntention() on applying "Convert concatenation to GString" intention to GString literals with backslash

This commit is contained in:
Maxim Medvedev
2009-11-16 20:04:09 +03:00
parent 57797f6db5
commit 0264d70a54
8 changed files with 23 additions and 7 deletions
@@ -73,7 +73,8 @@ public class ConvertConcatenationToGstringIntention extends Intention {
builder.append(GrStringUtil.removeQuotes(operand.getText()));
}
else if (operand instanceof GrLiteral) {
String text = GrStringUtil.escapeSymbolsForGString(GrStringUtil.removeQuotes(operand.getText()));
String text = GrStringUtil.escapeSymbolsForGString(GrStringUtil.removeQuotes(operand.getText()), false);
if (text.contains("\n")) text = GrStringUtil.escapeSymbolsForGString(text, true);
builder.append(text);
}
else if (MyPredicate.satisfiedBy(operand, false)) {
@@ -148,7 +148,8 @@ public class ConvertGStringToStringIntention extends Intention {
}
if (text.length() == 0) return null;
String escaped = GrStringUtil.escapeSymbolsForString(text);
String escaped = GrStringUtil.escapeSymbolsForString(text, false);
if (escaped.contains("\n")) escaped = GrStringUtil.escapeSymbolsForGString(escaped, true);
return GrStringUtil.addQuotes(escaped, false);
}
}
@@ -27,7 +27,7 @@ public class GrStringUtil {
private GrStringUtil() {
}
public static String escapeSymbolsForGString(String s) {
public static String escapeSymbolsForGString(String s, boolean escapeDoubleQuotes) {
StringBuilder b = new StringBuilder();
final char[] chars = s.toCharArray();
final int len = chars.length - 1;
@@ -43,7 +43,7 @@ public class GrStringUtil {
b.append('\n');
i++;
}
else if (next == '"') {
else if (escapeDoubleQuotes && next == '"') {
b.append('"');
i++;
}
@@ -64,7 +64,7 @@ public class GrStringUtil {
return b.toString();
}
public static String escapeSymbolsForString(String s) {
public static String escapeSymbolsForString(String s, boolean escapeQuotes) {
StringBuilder b = new StringBuilder();
final char[] chars = s.toCharArray();
final int len = chars.length - 1;
@@ -78,7 +78,7 @@ public class GrStringUtil {
else if (next == 'n') {
b.append('\n');
}
else if (next == '\'') {
else if (escapeQuotes && next == '\'') {
b.append(next);
}
else {
@@ -166,7 +166,8 @@ public class GrStringUtil {
literal = addAllBracesInGString((GrString)literal);
}
String literalText = escapeSymbolsForGString(removeQuotes(literal.getText()));
String literalText = escapeSymbolsForGString(removeQuotes(literal.getText()), false);
if(literalText.contains("\n")) literalText=escapeSymbolsForGString(literalText, true);
final GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(grString.getProject());
final GrExpression expression = factory.createExpressionFromText("\"${}" + literalText + "\"");
@@ -62,4 +62,12 @@ public class ConvertConcatenationToGstringTest extends GrIntentionTestCase {
public void testQuotes() throws Exception {
doTest("Convert concatenation to GString", true);
}
public void testQuotes2() throws Exception {
doTest("Convert concatenation to GString", true);
}
public void testQuotesInMultilineString() throws Exception {
doTest("Convert concatenation to GString", true);
}
}
@@ -0,0 +1 @@
def s = "Say \"hello\" to the" +<caret> " world"
@@ -0,0 +1 @@
def s = "Say \"hello\" to the world"
@@ -0,0 +1 @@
def s = "Say \"hello\" \n to" +<caret> " the world"
@@ -0,0 +1,2 @@
def s = """Say "hello"
to the world"""