From 3d2f63e1a2363e1c2a225e37db5d7a95b0d64325 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 11 Dec 2024 13:24:53 -0800 Subject: [PATCH] Fix another text block special case Handle removing trailing whitespace when the closing delimiter is on the same line as the final line of the text block. Fixes https://github.com/google/google-java-format/issues/1205 PiperOrigin-RevId: 705218533 --- .../com/google/googlejavaformat/java/StringWrapper.java | 4 +++- .../com/google/googlejavaformat/java/testdata/I1205.input | 7 +++++++ .../google/googlejavaformat/java/testdata/I1205.output | 8 ++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I1205.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I1205.output 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 c1c38d34..5d10da38 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -225,7 +225,9 @@ private void indentTextBlocks( } if (i == lines.size() - 1) { String withoutDelimiter = - trimmed.substring(0, trimmed.length() - TEXT_BLOCK_DELIMITER.length()); + trimmed + .substring(0, trimmed.length() - TEXT_BLOCK_DELIMITER.length()) + .stripTrailing(); if (!withoutDelimiter.stripLeading().isEmpty()) { output.append(withoutDelimiter).append('\\').append(separator).append(prefix); } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1205.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1205.input new file mode 100644 index 00000000..8f8acaa1 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1205.input @@ -0,0 +1,7 @@ +public interface Foo { + + private static String foo = + """ + foo\ + bar """; +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1205.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1205.output new file mode 100644 index 00000000..c82862c0 --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1205.output @@ -0,0 +1,8 @@ +public interface Foo { + + private static String foo = + """ + foo\ + bar\ + """; +}