Skip to content

Commit a4991be

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
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. #1195 PiperOrigin-RevId: 698939557
1 parent 20c526c commit a4991be

File tree

4 files changed

+24
-3
lines changed

4 files changed

+24
-3
lines changed

core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ private void indentTextBlocks(
219219
StringBuilder output = new StringBuilder(initialLines.get(0).stripLeading());
220220
for (int i = 0; i < lines.size(); i++) {
221221
String line = lines.get(i);
222-
String trimmed = line.stripLeading().stripTrailing();
222+
String trimmed = line.stripTrailing();
223223
output.append(separator);
224224
if (!trimmed.isEmpty()) {
225225
// Don't add incidental leading whitespace to empty lines
@@ -228,7 +228,7 @@ private void indentTextBlocks(
228228
if (i == lines.size() - 1) {
229229
String withoutDelimiter =
230230
trimmed.substring(0, trimmed.length() - TEXT_BLOCK_DELIMITER.length());
231-
if (!withoutDelimiter.isEmpty()) {
231+
if (!withoutDelimiter.stripLeading().isEmpty()) {
232232
output.append(withoutDelimiter).append('\\').append(separator).append(prefix);
233233
}
234234
// If the trailing line is just """, indenting it more than the prefix of incidental

core/src/test/java/com/google/googlejavaformat/java/FormatterIntegrationTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ public class FormatterIntegrationTest {
5757
"ExpressionSwitch",
5858
"I574",
5959
"I594",
60-
"SwitchComment")
60+
"SwitchComment",
61+
"B380299722")
6162
.putAll(15, "I603")
6263
.putAll(16, "I588", "Sealed")
6364
.putAll(17, "I683", "I684", "I696")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.helloworld;
2+
3+
class Foo {
4+
void foo() {
5+
var bar = """
6+
bar\
7+
bar""";
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.helloworld;
2+
3+
class Foo {
4+
void foo() {
5+
var bar =
6+
"""
7+
bar\
8+
bar\
9+
""";
10+
}
11+
}

0 commit comments

Comments
 (0)