Skip to content

Commit

Permalink
Fix a bug where leading whitespace was removed from the last line of …
Browse files Browse the repository at this point in the history
…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: 698887865
  • Loading branch information
cushon authored and google-java-format Team committed Nov 21, 2024
1 parent 20c526c commit b8bdb69
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.helloworld;

class Foo {
void foo() {
var bar = """
bar\
bar""";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.helloworld;

class Foo {
void foo() {
var bar =
"""
bar\
bar\
""";
}
}

0 comments on commit b8bdb69

Please sign in to comment.