Skip to content

Commit

Permalink
Don't try to reflow text blocks
Browse files Browse the repository at this point in the history
Fixes #976

PiperOrigin-RevId: 577249036
  • Loading branch information
cushon authored and google-java-format Team committed Oct 27, 2023
1 parent 685a6c9 commit 295e7a4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.googlejavaformat.java;

import static com.google.common.collect.Iterables.getLast;
import static java.lang.Math.min;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.joining;

Expand Down Expand Up @@ -122,6 +123,10 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
if (literalTree.getKind() != Kind.STRING_LITERAL) {
return null;
}
int pos = getStartPosition(literalTree);
if (input.substring(pos, min(input.length(), pos + 3)).equals("\"\"\"")) {
return null;
}
Tree parent = getCurrentPath().getParentPath().getLeaf();
if (parent instanceof MemberSelectTree
&& ((MemberSelectTree) parent).getExpression().equals(literalTree)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package com.google.googlejavaformat.java;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assume.assumeTrue;

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

@Test
public void textBlock() throws Exception {
assumeTrue(Runtime.version().feature() >= 15);
String input =
lines(
"package com.mypackage;",
"public class ReproBug {",
" private String myString;",
" private ReproBug() {",
" String str =",
" \"\"\"",
" "
+ " {\"sourceEndpoint\":\"ri.something.1-1.object-internal.1\",\"targetEndpoint\":\"ri.some"
+ "thing.1-1.object-internal.2\",\"typeId\":\"typeId\"}\"\"\";",
" myString = str;",
" }",
"}");
assertThat(StringWrapper.wrap(100, input, new Formatter())).isEqualTo(input);
}

private static String lines(String... line) {
return Joiner.on('\n').join(line) + '\n';
}
Expand Down

0 comments on commit 295e7a4

Please sign in to comment.