Skip to content

Commit

Permalink
Fix logic for checking if text blocks are already deindented
Browse files Browse the repository at this point in the history
Check the last line instead of the first line of the text block contents. Empty lines in the text block have no leading whitespace but do not indicate the contents are deindented, only non-empty lines should be considered. The last line will never be empty, it contains at least the close delimiter.

PiperOrigin-RevId: 697437392
  • Loading branch information
cushon authored and google-java-format Team committed Nov 18, 2024
1 parent 3356bd3 commit 45fcce8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ private void indentTextBlocks(
String stripped = stripIndent(initialLines.stream().skip(1).collect(joining(separator)));
ImmutableList<String> lines = stripped.lines().collect(toImmutableList());
int deindent =
initialLines.get(1).stripTrailing().length() - lines.get(0).stripTrailing().length();
getLast(initialLines).stripTrailing().length()
- getLast(lines).stripTrailing().length();

String prefix =
(deindent == 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,33 @@ public void textBlockSpaceTabMix() throws Exception {
assertThat(actual).isEqualTo(expected);
}

@Test
public void leadingBlankLine() throws Exception {
assumeTrue(Runtime.version().feature() >= 15);
String input =
lines(
"public class T {",
" String s =",
" \"\"\"",
"",
" lorem",
" ipsum",
" \"\"\";",
"}");
String expected =
lines(
"public class T {",
" String s =",
" \"\"\"",
"",
" lorem",
" ipsum",
" \"\"\";",
"}");
String actual = StringWrapper.wrap(100, input, new Formatter());
assertThat(actual).isEqualTo(expected);
}

private static String lines(String... line) {
return Joiner.on('\n').join(line) + '\n';
}
Expand Down

0 comments on commit 45fcce8

Please sign in to comment.