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 42099d6b..c3a36ab5 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -207,7 +207,8 @@ private void indentTextBlocks( String stripped = stripIndent(initialLines.stream().skip(1).collect(joining(separator))); ImmutableList 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 diff --git a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java index 7854126c..339ed134 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/StringWrapperTest.java @@ -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'; }