Skip to content

Commit 5269625

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Fix logic for checking if text blocks are already deindented
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: 697785256
1 parent 3356bd3 commit 5269625

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ private void indentTextBlocks(
207207
String stripped = stripIndent(initialLines.stream().skip(1).collect(joining(separator)));
208208
ImmutableList<String> lines = stripped.lines().collect(toImmutableList());
209209
int deindent =
210-
initialLines.get(1).stripTrailing().length() - lines.get(0).stripTrailing().length();
210+
getLast(initialLines).stripTrailing().length()
211+
- getLast(lines).stripTrailing().length();
211212

212213
String prefix =
213214
(deindent == 0

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,33 @@ public void textBlockSpaceTabMix() throws Exception {
174174
assertThat(actual).isEqualTo(expected);
175175
}
176176

177+
@Test
178+
public void leadingBlankLine() throws Exception {
179+
assumeTrue(Runtime.version().feature() >= 15);
180+
String input =
181+
lines(
182+
"public class T {",
183+
" String s =",
184+
" \"\"\"",
185+
"",
186+
" lorem",
187+
" ipsum",
188+
" \"\"\";",
189+
"}");
190+
String expected =
191+
lines(
192+
"public class T {",
193+
" String s =",
194+
" \"\"\"",
195+
"",
196+
" lorem",
197+
" ipsum",
198+
" \"\"\";",
199+
"}");
200+
String actual = StringWrapper.wrap(100, input, new Formatter());
201+
assertThat(actual).isEqualTo(expected);
202+
}
203+
177204
private static String lines(String... line) {
178205
return Joiner.on('\n').join(line) + '\n';
179206
}

0 commit comments

Comments
 (0)