Skip to content

Commit

Permalink
Fix text block conversion when ending in quotes (#1245)
Browse files Browse the repository at this point in the history
- fix StringConcatToTextBlockFixCore to look at resultant buffer
  before adding final block quotes and if last 1 or 2 characters are
  un-escaped quotes, replace them with escaped quotes
- add new scenarios to CleanUpTest15
- fixes #1238
  • Loading branch information
jjohnstn authored Mar 9, 2024
1 parent 43c8c66 commit c42fa23
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,22 @@ public void accept(Expression t) {
if (newLine) {
buf.append(fIndent);
}
// Replace trailing un-escaped quotes with escaped quotes before adding text block end
int i= buf.length() - 1;
int count= 0;
while (i >= 0 && buf.charAt(i) == '"' && count <= 3) {
--i;
++count;
}
if (i >= 0 && buf.charAt(i) == '\\') {
--count;
}
for (i= count; i > 0; --i) {
buf.deleteCharAt(buf.length() - 1);
}
for (i= count; i > 0; --i) {
buf.append("\\\""); //$NON-NLS-1$
}
buf.append("\"\"\""); //$NON-NLS-1$
if (!isTagged) {
TextBlock textBlock= (TextBlock) rewrite.createStringPlaceholder(buf.toString(), ASTNode.TEXT_BLOCK);
Expand Down Expand Up @@ -370,7 +386,6 @@ private static List<String> 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"
transformed.append(escapedText.substring(readIndex, bsIndex));
Expand Down Expand Up @@ -729,6 +744,23 @@ public SourceRange computeSourceRange(final ASTNode nodeWithComment) {
if (newLine) {
buf.append(fIndent);
}

// Replace trailing un-escaped quotes with escaped quotes before adding text block end
int readIndex= buf.length() - 1;
int count= 0;
while (readIndex >= 0 && buf.charAt(readIndex) == '"' && count <= 3) {
--readIndex;
++count;
}
if (readIndex >= 0 && buf.charAt(readIndex) == '\\') {
--count;
}
for (int i= count; i > 0; --i) {
buf.deleteCharAt(buf.length() - 1);
}
for (int i= count; i > 0; --i) {
buf.append("\\\""); //$NON-NLS-1$
}
buf.append("\"\"\""); //$NON-NLS-1$
MethodInvocation firstToStringCall= fToStringList.get(0);
AST ast= firstToStringCall.getAST();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,15 @@ public void testConcatToTextBlock() throws Exception {
+ " \"\\t} else\\n\" +\n" //
+ " \"\\t\\tnoStuff\";\n" //
+ " }\n" //
+ "}\n";
+ " public void testEndEscapedQuotes() {\n" //
+ " String a =\n" //
+ " \"1\\n\" +\n" //
+ " \"2\\n\" +\n" //
+ " \"3\\n\" +\n" //
+ " \"4\\n\" +\n" //
+ " \"\\\"\\\"\\\"\\\"\";\n" //
+ " }\n" //
+ "}\n";

ICompilationUnit cu1= pack1.createCompilationUnit("E.java", sample, false, null);

Expand Down Expand Up @@ -195,6 +203,15 @@ public void testConcatToTextBlock() throws Exception {
+ " \t} else\n" //
+ " \t\tnoStuff\"\"\";\n" //
+ " }\n" //
+ " public void testEndEscapedQuotes() {\n" //
+ " String a =\n" //
+ " \"\"\"\n" //
+ " 1\n" //
+ " 2\n" //
+ " 3\n" //
+ " 4\n" //
+ " \\\"\"\"\\\"\"\"\";\n" //
+ " }\n" //
+ "}\n";

assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
Expand Down Expand Up @@ -243,6 +260,11 @@ public void testConcatToTextBlock2() throws Exception {
+ " \"def\\n\" +\n" //
+ " \"ghi\\n\";\n" //
+ " new StringBuffer(\"abc\\n\" + \"def\\n\" + \"ghi\");\n" //
+ " new StringBuffer(\"1\\n\" +\n" //
+ " \"2\\n\" +\n" //
+ " \"3\\n\" +\n" //
+ " \"4\\n\" +\n" //
+ " \"\\\"\\\"\\\"\");\n" //
+ " }\n" //
+ "}";

Expand Down Expand Up @@ -301,7 +323,13 @@ public void testConcatToTextBlock2() throws Exception {
+ " abc\n" //
+ " def\n" //
+ " ghi\"\"\");\n" //
+ " }\n" //
+ " new StringBuffer(\"\"\"\n" //
+ " 1\n" //
+ " 2\n" //
+ " 3\n" //
+ " 4\n" //
+ " \\\"\\\"\\\"\"\"\");\n" //
+ " }\n" //
+ "}";

assertRefactoringResultAsExpected(new ICompilationUnit[] { cu1 }, new String[] { expected1 }, null);
Expand Down

0 comments on commit c42fa23

Please sign in to comment.