Skip to content

Commit

Permalink
Drop unnecessary trailing escape in UseTextBlocks (#558)
Browse files Browse the repository at this point in the history
* issue-555: fix UseTextBlocks creating unnecessary trailing escape

* issue-555: add the issue annotation

* Slight polish

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
BramliAK and timtebeek authored Sep 18, 2024
1 parent 53afa8a commit 37ac5f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> s

StringBuilder sb = new StringBuilder();
StringBuilder originalContent = new StringBuilder();
stringLiterals = stringLiterals.stream().filter(s -> s.getValue() != null && !s.getValue().toString().isEmpty()).collect(Collectors.toList());
stringLiterals = stringLiterals.stream()
.filter(s -> s.getValue() != null && !s.getValue().toString().isEmpty())
.collect(Collectors.toList());
for (int i = 0; i < stringLiterals.size(); i++) {
String s = requireNonNull(stringLiterals.get(i).getValue()).toString();
sb.append(s);
Expand All @@ -139,21 +141,18 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> s
}
}

content = sb.toString();

TabsAndIndentsStyle tabsAndIndentsStyle = Optional.ofNullable(getCursor().firstEnclosingOrThrow(SourceFile.class)
.getStyle(TabsAndIndentsStyle.class)).orElse(IntelliJ.tabsAndIndents());
boolean useTab = tabsAndIndentsStyle.getUseTabCharacter();
int tabSize = tabsAndIndentsStyle.getTabSize();

String indentation = getIndents(concatenation, useTab, tabSize);

boolean isEndsWithNewLine = content.endsWith("\n");

// references:
// - https://docs.oracle.com/en/java/javase/14/docs/specs/text-blocks-jls.html
// - https://javaalmanac.io/features/textblocks/

content = sb.toString();
// escape backslashes
content = content.replace("\\", "\\\\");
// escape triple quotes
Expand All @@ -170,13 +169,20 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> s

// add last line to ensure the closing delimiter is in a new line to manage indentation & remove the
// need to escape ending quote in the content
if (!isEndsWithNewLine) {
if (isEndsWithSpecialCharacters(sb.toString())) {
content = content + "\\\n" + indentation;
}

return new J.Literal(randomId(), binary.getPrefix(), Markers.EMPTY, originalContent.toString(),
String.format("\"\"\"%s\"\"\"", content), null, JavaType.Primitive.String);
}

private boolean isEndsWithSpecialCharacters(String content) {
return content.endsWith("\"") ||
content.endsWith("=") ||
content.endsWith("}") ||
content.endsWith(";");
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ class Test {
String query = \"""
SELECT * FROM \\s
my_table \\s
WHERE something = 1; \\
\""";
WHERE something = 1; \""";
}
"""
)
Expand Down Expand Up @@ -738,8 +737,7 @@ class Test {
class Test {
String eightQuotes = ""\"
""\\""\"\\""\"\\
after 8 quotes\\
""\";
after 8 quotes""\";
}
"""
)
Expand Down Expand Up @@ -860,4 +858,30 @@ void shouldNotUpdateKotlinCode() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/555")
void textBlockTrailingEscape() {
rewriteRun(
spec -> spec.recipe(new UseTextBlocks()),
java(
"""
package com.helloworld;
public class Main {
String foo =
"hello\\n"
+ "world";
}""",
"""
package com.helloworld;
public class Main {
String foo =
\"""
hello
world\""";
}""",
src -> src.markers(javaVersion(17))));
}
}

0 comments on commit 37ac5f9

Please sign in to comment.