Skip to content

Commit 295e7a4

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Don't try to reflow text blocks
Fixes #976 PiperOrigin-RevId: 577249036
1 parent 685a6c9 commit 295e7a4

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.googlejavaformat.java;
1616

1717
import static com.google.common.collect.Iterables.getLast;
18+
import static java.lang.Math.min;
1819
import static java.nio.charset.StandardCharsets.UTF_8;
1920
import static java.util.stream.Collectors.joining;
2021

@@ -122,6 +123,10 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
122123
if (literalTree.getKind() != Kind.STRING_LITERAL) {
123124
return null;
124125
}
126+
int pos = getStartPosition(literalTree);
127+
if (input.substring(pos, min(input.length(), pos + 3)).equals("\"\"\"")) {
128+
return null;
129+
}
125130
Tree parent = getCurrentPath().getParentPath().getLeaf();
126131
if (parent instanceof MemberSelectTree
127132
&& ((MemberSelectTree) parent).getExpression().equals(literalTree)) {

core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.googlejavaformat.java;
1616

1717
import static com.google.common.truth.Truth.assertThat;
18+
import static org.junit.Assume.assumeTrue;
1819

1920
import com.google.common.base.Joiner;
2021
import org.junit.Test;
@@ -52,6 +53,26 @@ public void testAwkwardLineEndWrapping() throws Exception {
5253
assertThat(StringWrapper.wrap(100, input, new Formatter())).isEqualTo(output);
5354
}
5455

56+
@Test
57+
public void textBlock() throws Exception {
58+
assumeTrue(Runtime.version().feature() >= 15);
59+
String input =
60+
lines(
61+
"package com.mypackage;",
62+
"public class ReproBug {",
63+
" private String myString;",
64+
" private ReproBug() {",
65+
" String str =",
66+
" \"\"\"",
67+
" "
68+
+ " {\"sourceEndpoint\":\"ri.something.1-1.object-internal.1\",\"targetEndpoint\":\"ri.some"
69+
+ "thing.1-1.object-internal.2\",\"typeId\":\"typeId\"}\"\"\";",
70+
" myString = str;",
71+
" }",
72+
"}");
73+
assertThat(StringWrapper.wrap(100, input, new Formatter())).isEqualTo(input);
74+
}
75+
5576
private static String lines(String... line) {
5677
return Joiner.on('\n').join(line) + '\n';
5778
}

0 commit comments

Comments
 (0)