Skip to content

Commit

Permalink
Do not partially use text blocks (#273)
Browse files Browse the repository at this point in the history
* Do not partially use text blocks

Fixes #260

* Restore original order

* Restore original order

* Clear out duplication in recursive function

* Move function down; IDE keeps moving this
  • Loading branch information
timtebeek authored Aug 16, 2023
1 parent 2a8527b commit 07443b1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
19 changes: 16 additions & 3 deletions src/main/java/org/openrewrite/java/migrate/lang/UseTextBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.time.Duration;
import java.util.*;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.openrewrite.Tree.randomId;
Expand Down Expand Up @@ -82,6 +85,11 @@ public J visitBinary(J.Binary binary, ExecutionContext ctx) {
StringBuilder contentSb = new StringBuilder();
StringBuilder concatenationSb = new StringBuilder();

boolean allLiterals = allLiterals(binary);
if (!allLiterals) {
return binary; // Not super.visitBinary(binary, ctx) because we don't want to visit the children
}

boolean flattenable = flatAdditiveStringLiterals(binary, stringLiterals, contentSb, concatenationSb);
if (!flattenable) {
return super.visitBinary(binary, ctx);
Expand Down Expand Up @@ -133,7 +141,7 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> s
boolean useTab = tabsAndIndentsStyle.getUseTabCharacter();
int tabSize = tabsAndIndentsStyle.getTabSize();

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

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

Expand Down Expand Up @@ -167,6 +175,12 @@ private J.Literal toTextBlock(J.Binary binary, String content, List<J.Literal> s
});
}

private static boolean allLiterals(Expression exp) {
return isRegularStringLiteral(exp) || exp instanceof J.Binary
&& ((J.Binary) exp).getOperator() == J.Binary.Type.Addition
&& allLiterals(((J.Binary) exp).getLeft()) && allLiterals(((J.Binary) exp).getRight());
}

private static boolean flatAdditiveStringLiterals(Expression expression,
List<J.Literal> stringLiterals,
StringBuilder contentSb,
Expand Down Expand Up @@ -194,7 +208,6 @@ private static boolean flatAdditiveStringLiterals(Expression expression,
private static boolean isRegularStringLiteral(Expression expr) {
if (expr instanceof J.Literal) {
J.Literal l = (J.Literal) expr;

return TypeUtils.isString(l.getType()) &&
l.getValueSource() != null &&
!l.getValueSource().startsWith("\"\"\"");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,16 @@
import org.openrewrite.style.NamedStyles;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import java.util.function.Consumer;
import java.util.function.UnaryOperator;

import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.openrewrite.Tree.randomId;
import static org.openrewrite.java.Assertions.*;
import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.javaVersion;

class UseTextBlocksTest implements RewriteTest {

Expand Down Expand Up @@ -95,6 +97,7 @@ class Test {
@Test
void preserveTrailingWhiteSpaces2() {
rewriteRun(
//language=java
java(
"""
class Test {
Expand Down Expand Up @@ -372,11 +375,11 @@ class Test {

private static Consumer<RecipeSpec> tabsAndIndents(UnaryOperator<TabsAndIndentsStyle> with, int tabSize) {
return spec -> spec.parser(JavaParser.fromJavaVersion().styles(singletonList(
new NamedStyles(
randomId(), "TabsOnlyFile", "TabsOnlyFile", "tabSize is x", emptySet(),
singletonList(with.apply(buildTabsAndIndents(tabSize)))
)
)));
new NamedStyles(
randomId(), "TabsOnlyFile", "TabsOnlyFile", "tabSize is x", emptySet(),
singletonList(with.apply(buildTabsAndIndents(tabSize)))
)
)));
}

private static TabsAndIndentsStyle buildTabsAndIndents(int tabSize) {
Expand Down Expand Up @@ -500,6 +503,7 @@ void textBlockDemo() {
@Test
void newlineAtBeginningOfLines() {
rewriteRun(
//language=java
java(
"""
class A {
Expand Down Expand Up @@ -541,6 +545,7 @@ void log(String s) {}
@Test
void consecutiveNewLines() {
rewriteRun(
//language=java
java(
"""
class Test {
Expand Down Expand Up @@ -693,6 +698,7 @@ class Test {
@Test
void eightQuotes() {
rewriteRun(
//language=java
java(
"""
class Test {
Expand Down Expand Up @@ -758,10 +764,12 @@ class Test {
);
}

@Disabled
@Test
void grouping() {
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/260")
//@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/261")
void doNotPartiallyReplaceWithTextBlocks() {
rewriteRun(
//language=java
java(
"""
public class Test {
Expand All @@ -779,23 +787,30 @@ public void method() {
"AFTER the variable. ";
}
}
""",
"""
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-migrate-java/issues/261")
void doNotPartiallyReplaceWithTextBlocksWithIntLiteral() {
rewriteRun(
//language=java
java(
"""
public class Test {
public void method() {
int variable = 1;
String stringWithVariableInIt =
\"""
This \\
is \\
text \\
BEFORE the variable \\
\"""
This \\
is \\
text \\
AFTER the variable. \\
\""";
"This " +
"is " +
"text " +
"BEFORE the variable " +
1 +
"This " +
"is " +
"text " +
"AFTER the variable. ";
}
}
"""
Expand Down

0 comments on commit 07443b1

Please sign in to comment.