diff --git a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/StringConcatToTextBlockFixCore.java b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/StringConcatToTextBlockFixCore.java index fcb802db8dd..23a818182ba 100644 --- a/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/StringConcatToTextBlockFixCore.java +++ b/org.eclipse.jdt.core.manipulation/core extension/org/eclipse/jdt/internal/corext/fix/StringConcatToTextBlockFixCore.java @@ -367,20 +367,22 @@ private static List unescapeBlock(String escapedText) { List parts= new ArrayList<>(); while ((bsIndex= escapedText.indexOf("\\", readIndex)) >= 0) { //$NON-NLS-1$ "\" - if (escapedText.startsWith("\\n", bsIndex)) { //$NON-NLS-1$ "\n" + if (escapedText.startsWith("\\n", bsIndex) || escapedText.startsWith("\\u005cn", bsIndex)) { //$NON-NLS-1$ //$NON-NLS-2$ "\n" transformed.append(escapedText.substring(readIndex, bsIndex)); parts.add(escapeTrailingWhitespace(transformed.toString())+ System.lineSeparator()); transformed= new StringBuilder(); - readIndex= bsIndex + 2; - } else if (escapedText.startsWith("\\\"", bsIndex)) { //$NON-NLS-1$ "\"" + readIndex= bsIndex + (escapedText.startsWith("\\n", bsIndex) ? 2 : 7); //$NON-NLS-1$ + } else if (escapedText.startsWith("\\\"", bsIndex) || escapedText.startsWith("\\u005c\"", bsIndex)) { //$NON-NLS-1$ //$NON-NLS-2$ "\"" // if there are more than three quotes in a row, escape the first quote of every triplet to // avoid it being interpreted as a text block terminator. This code would be much simpler if // we could escape the third quote of each triplet, but the text block spec recommends this way. transformed.append(escapedText.substring(readIndex, bsIndex)); int quoteCount= 1; - while (escapedText.startsWith("\\\"", bsIndex + 2 * quoteCount)) { //$NON-NLS-1$ + int index= (escapedText.startsWith("\\\"", bsIndex) ? 2 : 7); //$NON-NLS-1$ + while (escapedText.startsWith("\\\"", bsIndex + index) || escapedText.startsWith("\\u005c\"", bsIndex + index)) { //$NON-NLS-1$ //$NON-NLS-2$ quoteCount++; + index += (escapedText.startsWith("\\\"", bsIndex + index) ? 2 : 7); //$NON-NLS-1$ } int i= 0; while (i < quoteCount / 3) { @@ -394,11 +396,11 @@ private static List unescapeBlock(String escapedText) { for (int j = 0; j < quoteCount % 3; j++) { transformed.append("\""); //$NON-NLS-1$ } - readIndex= bsIndex + 2 * quoteCount; - } else if (escapedText.startsWith("\\t", bsIndex)) { //$NON-NLS-1$ "\t" + readIndex= bsIndex + index; + } else if (escapedText.startsWith("\\t", bsIndex) || escapedText.startsWith("\\u005ct", bsIndex)) { //$NON-NLS-1$ //$NON-NLS-2$ "\t" transformed.append(escapedText.substring(readIndex, bsIndex)); transformed.append("\t"); //$NON-NLS-1$ - readIndex= bsIndex+2; + readIndex= bsIndex + (escapedText.startsWith("\\t", bsIndex) ? 2 : 7); //$NON-NLS-1$ } else { transformed.append(escapedText.substring(readIndex, bsIndex)); transformed.append("\\").append(escapedText.charAt(bsIndex + 1)); //$NON-NLS-1$ @@ -424,15 +426,12 @@ private static String escapeTrailingWhitespace(String unescaped) { } int whitespaceStart= unescaped.length()-1; StringBuilder trailingWhitespace= new StringBuilder(); - while (whitespaceStart > 0) { - if (unescaped.charAt(whitespaceStart) == ' ') { - whitespaceStart--; - trailingWhitespace.append("\\s"); //$NON-NLS-1$ - } else if (unescaped.charAt(whitespaceStart) == '\t') { - whitespaceStart--; - trailingWhitespace.append("\\t"); //$NON-NLS-1$ - } - break; + if (unescaped.charAt(whitespaceStart) == ' ') { + --whitespaceStart; + trailingWhitespace.append("\\s"); //$NON-NLS-1$ + } else if (unescaped.charAt(whitespaceStart) == '\t') { + --whitespaceStart; + trailingWhitespace.append("\\t"); //$NON-NLS-1$ } return unescaped.substring(0, whitespaceStart + 1) + trailingWhitespace; diff --git a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java index 1b239edc20f..7d25ac63996 100644 --- a/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java +++ b/org.eclipse.jdt.ui.tests/ui/org/eclipse/jdt/ui/tests/quickfix/CleanUpTest15.java @@ -54,6 +54,20 @@ public void testConcatToTextBlock() throws Exception { String sample= "" // + "package test1;\n" // + "\n" // + + "/**\n" // + + " * Performs:\u005cn" // + + " *
{@code\n" //
+			    + " *    for (String s : strings) {\u005cn" //
+			    + " *        if (s.equals(value)) {\n" //
+			    + " *            return \\u0030;\n" //
+			    + " *        }\n" //
+			    + " *        if (s.startsWith(value)) {\n" //
+			    + " *            return 1;\n" //
+			    + " *        }\n" //
+			    + " *    }\n" //
+			    + " *    return -1;\n" //
+			    + " * }
\n" // + + " */\n" // + "public class E {\n" // + " static String str = \"\" + //$NON-NLS-1$\n" // + " \"public class B { \\n\" + //$NON-NLS-1$\n" // @@ -62,6 +76,11 @@ public void testConcatToTextBlock() throws Exception { + " \" }\\n\" + //$NON-NLS-1$\n" // + " \"}\"; //$NON-NLS-1$\n" // + "\n" // + + " private static final String CU_POSTFIX= \" {\\n\" +\n" // + + " \" \\n\" +\n" // + + " \"}\\n\" +\n" // + + " \"}\\n\";\n" // + + "\n" // + " public void testSimple() {\n" // + " // comment 1\n" // + " String x = \"\" + //$NON-NLS-1$\n" // @@ -139,6 +158,20 @@ public void testConcatToTextBlock() throws Exception { String expected1= "" // + "package test1;\n" // + "\n" // + + "/**\n" // + + " * Performs:\n" // + + " *
{@code\n" //
+				+ " *    for (String s : strings) {\n" //
+				+ " *        if (s.equals(value)) {\n" //
+				+ " *            return \\u0030;\n" //
+				+ " *        }\n" //
+				+ " *        if (s.startsWith(value)) {\n" //
+				+ " *            return 1;\n" //
+				+ " *        }\n" //
+				+ " *    }\n" //
+				+ " *    return -1;\n" //
+				+ " * }
\n" // + + " */\n" // + "public class E {\n" // + " static String str = \"\"\"\n" // + " public class B {\\s\n" // @@ -148,6 +181,13 @@ public void testConcatToTextBlock() throws Exception { + " }\n" // + " }\"\"\"; //$NON-NLS-1$\n" // + "\n" // + + " private static final String CU_POSTFIX= \"\"\"\n" // + + " {\n" // + + " \\t\n" // + + " }\n" // + + " }\n" // + + " \"\"\";\n" // + + "\n" // + " public void testSimple() {\n" // + " // comment 1\n" // + " String x = \"\"\"\n" //