Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop unnecessary trailing escape in UseTextBlocks #558

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))));
}
}