From 779518d3ee3e79ad085e5af6bf35e133d1955c34 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Thu, 26 Oct 2023 11:41:47 -0700 Subject: [PATCH] Don't try to reflow text blocks Fixes https://github.com/google/google-java-format/issues/976 PiperOrigin-RevId: 576940738 --- .../googlejavaformat/java/StringWrapper.java | 5 +++++ .../java/StringWrapperTest.java | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index d801c9b86..f241ae47f 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -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; @@ -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)) { diff --git a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java index 99e1b2fc5..0430f6534 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java @@ -52,6 +52,25 @@ public void testAwkwardLineEndWrapping() throws Exception { assertThat(StringWrapper.wrap(100, input, new Formatter())).isEqualTo(output); } + @Test + public void textBlock() throws Exception { + 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'; }