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 c3a36ab5..eb7eacfa 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -219,7 +219,7 @@ private void indentTextBlocks( StringBuilder output = new StringBuilder(initialLines.get(0).stripLeading()); for (int i = 0; i < lines.size(); i++) { String line = lines.get(i); - String trimmed = line.stripLeading().stripTrailing(); + String trimmed = line.stripTrailing(); output.append(separator); if (!trimmed.isEmpty()) { // Don't add incidental leading whitespace to empty lines @@ -228,7 +228,7 @@ private void indentTextBlocks( if (i == lines.size() - 1) { String withoutDelimiter = trimmed.substring(0, trimmed.length() - TEXT_BLOCK_DELIMITER.length()); - if (!withoutDelimiter.isEmpty()) { + if (!withoutDelimiter.stripLeading().isEmpty()) { output.append(withoutDelimiter).append('\\').append(separator).append(prefix); } // If the trailing line is just """, indenting it more than the prefix of incidental diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java index ef6bef08..b83be477 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java @@ -57,7 +57,8 @@ public class FormatterIntegrationTest { "ExpressionSwitch", "I574", "I594", - "SwitchComment") + "SwitchComment", + "B380299722") .putAll(15, "I603") .putAll(16, "I588", "Sealed") .putAll(17, "I683", "I684", "I696") diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B380299722.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B380299722.input new file mode 100644 index 00000000..8da9601c --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B380299722.input @@ -0,0 +1,9 @@ +package com.helloworld; + +class Foo { + void foo() { + var bar = """ + bar\ + bar"""; + } +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/B380299722.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B380299722.output new file mode 100644 index 00000000..39707314 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/B380299722.output @@ -0,0 +1,11 @@ +package com.helloworld; + +class Foo { + void foo() { + var bar = + """ + bar\ + bar\ + """; + } +}