From a4991be14c8375bc3258b3af02380e40362419f3 Mon Sep 17 00:00:00 2001
From: Liam Miller-Cushon <cushon@google.com>
Date: Thu, 21 Nov 2024 15:41:18 -0800
Subject: [PATCH] Fix a bug where leading whitespace was removed from the last
 line of a text block

If the last line contains contents other than the closing delimiter, it may have significant leading whitespace. This change prevents that whitespace from being removed.

https://github.com/google/google-java-format/issues/1195

PiperOrigin-RevId: 698939557
---
 .../google/googlejavaformat/java/StringWrapper.java   |  4 ++--
 .../java/FormatterIntegrationTest.java                |  3 ++-
 .../googlejavaformat/java/testdata/B380299722.input   |  9 +++++++++
 .../googlejavaformat/java/testdata/B380299722.output  | 11 +++++++++++
 4 files changed, 24 insertions(+), 3 deletions(-)
 create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/B380299722.input
 create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/B380299722.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 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\
+        """;
+  }
+}