IDEADEV-40959: ClassCastException at ConvertConcatenationToGstringIntention.processIn...
authorMaxim Medvedev <maxim.medvedev@jetbrains.com>
Mon, 16 Nov 2009 17:04:09 +0000 (20:04 +0300)
committerMaxim Medvedev <maxim.medvedev@jetbrains.com>
Mon, 16 Nov 2009 17:04:09 +0000 (20:04 +0300)
plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/conversions/ConvertConcatenationToGstringIntention.java
plugins/groovy/src/org/jetbrains/plugins/groovy/intentions/conversions/ConvertGStringToStringIntention.java
plugins/groovy/src/org/jetbrains/plugins/groovy/lang/psi/util/GrStringUtil.java
plugins/groovy/test/org/jetbrains/plugins/groovy/intentions/ConvertConcatenationToGstringTest.java
plugins/groovy/testdata/intentions/convertConcatenationToGstring/Quotes2.groovy [new file with mode: 0644]
plugins/groovy/testdata/intentions/convertConcatenationToGstring/Quotes2_after.groovy [new file with mode: 0644]
plugins/groovy/testdata/intentions/convertConcatenationToGstring/QuotesInMultilineString.groovy [new file with mode: 0644]
plugins/groovy/testdata/intentions/convertConcatenationToGstring/QuotesInMultilineString_after.groovy [new file with mode: 0644]

index 805627b679ac272ef7585a9d6e0e25326b8c2356..cb1f0fe744f7902bc630754cfffe23b3759107eb 100644 (file)
@@ -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)) {
index 99cafafb297e70e4de26dd3782c0fe5cbf0421ba..e22e9c7d93ddba710792ceb1629513178d296b28 100644 (file)
@@ -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);
   }
 }
index 7660636bf0875bc2e5af826128450e61e0dd5874..cef2484d2291641006ceddd2bc2afb8d7980b7a3 100644 (file)
@@ -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 + "\"");
index 684207391d493d6c661041d60785c06fbebf0d9d..e58e3d72d49a07709de4f182e86850c23058ed68 100644 (file)
@@ -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);
+  }
 }
diff --git a/plugins/groovy/testdata/intentions/convertConcatenationToGstring/Quotes2.groovy b/plugins/groovy/testdata/intentions/convertConcatenationToGstring/Quotes2.groovy
new file mode 100644 (file)
index 0000000..487d5c8
--- /dev/null
@@ -0,0 +1 @@
+def s = "Say \"hello\" to the" +<caret> " world"
\ No newline at end of file
diff --git a/plugins/groovy/testdata/intentions/convertConcatenationToGstring/Quotes2_after.groovy b/plugins/groovy/testdata/intentions/convertConcatenationToGstring/Quotes2_after.groovy
new file mode 100644 (file)
index 0000000..d901977
--- /dev/null
@@ -0,0 +1 @@
+def s = "Say \"hello\" to the world"
\ No newline at end of file
diff --git a/plugins/groovy/testdata/intentions/convertConcatenationToGstring/QuotesInMultilineString.groovy b/plugins/groovy/testdata/intentions/convertConcatenationToGstring/QuotesInMultilineString.groovy
new file mode 100644 (file)
index 0000000..18ebdb0
--- /dev/null
@@ -0,0 +1 @@
+def s = "Say \"hello\" \n to" +<caret> " the world"
\ No newline at end of file
diff --git a/plugins/groovy/testdata/intentions/convertConcatenationToGstring/QuotesInMultilineString_after.groovy b/plugins/groovy/testdata/intentions/convertConcatenationToGstring/QuotesInMultilineString_after.groovy
new file mode 100644 (file)
index 0000000..2456ae7
--- /dev/null
@@ -0,0 +1,2 @@
+def s = """Say "hello" 
+ to the world"""
\ No newline at end of file