Skip to content

Commit 02de39a

Browse files
cushonronshapiro
authored andcommitted
Omit blank lines after imports in package-info files
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=157895967
1 parent 9b6d334 commit 02de39a

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

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

+15-14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import com.google.common.collect.ImmutableSortedSet;
2323
import com.google.googlejavaformat.Newlines;
2424
import com.google.googlejavaformat.java.JavaInput.Tok;
25+
import java.util.ArrayList;
26+
import java.util.List;
2527
import org.openjdk.tools.javac.parser.Tokens.TokenKind;
2628

2729
/** Orders imports in Java source code. */
@@ -115,27 +117,26 @@ private String reorderImports() throws FormatterException {
115117
throw new FormatterException("Imports not contiguous (perhaps a comment separates them?)");
116118
}
117119

118-
// Add back the text from after the point where we stopped tokenizing.
119-
String tail;
120-
if (toks.isEmpty()) {
121-
tail = "";
122-
} else {
123-
Tok lastTok = getLast(toks);
124-
int tailStart = lastTok.getPosition() + lastTok.length();
125-
tail = text.substring(tailStart);
126-
}
127-
128120
StringBuilder result = new StringBuilder();
129121
result.append(
130122
CharMatcher.whitespace().trimTrailingFrom(tokString(0, unindentedFirstImportStart)));
131123
if (result.length() > 0) {
132124
result.append(lineSeparator).append(lineSeparator);
133125
}
134126
result.append(reorderedImportsString(imports.imports));
135-
result.append(lineSeparator);
136-
result.append(
137-
CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size())));
138-
result.append(tail);
127+
128+
List<String> tail = new ArrayList<>();
129+
tail.add(CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size())));
130+
if (!toks.isEmpty()) {
131+
Tok lastTok = getLast(toks);
132+
int tailStart = lastTok.getPosition() + lastTok.length();
133+
tail.add(text.substring(tailStart));
134+
}
135+
if (tail.stream().anyMatch(s -> !s.isEmpty())) {
136+
result.append(lineSeparator);
137+
tail.forEach(result::append);
138+
}
139+
139140
return result.toString();
140141
}
141142

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

+22
Original file line numberDiff line numberDiff line change
@@ -254,4 +254,26 @@ public void importRemoveErrorParseError() throws Exception {
254254
assertThat(main.format("-")).isEqualTo(1);
255255
assertThat(err.toString()).contains("<stdin>:4:3: error: class, interface, or enum expected");
256256
}
257+
258+
@Test
259+
public void packageInfo() throws Exception {
260+
String[] input = {
261+
"@CheckReturnValue",
262+
"@ParametersAreNonnullByDefault",
263+
"package com.google.common.labs.base;",
264+
"",
265+
"import javax.annotation.CheckReturnValue;",
266+
"import javax.annotation.ParametersAreNonnullByDefault;",
267+
"",
268+
};
269+
StringWriter out = new StringWriter();
270+
StringWriter err = new StringWriter();
271+
Main main =
272+
new Main(
273+
new PrintWriter(out, true),
274+
new PrintWriter(err, true),
275+
new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
276+
assertThat(main.format("-")).isEqualTo(0);
277+
assertThat(out.toString()).isEqualTo(joiner.join(input));
278+
}
257279
}

0 commit comments

Comments
 (0)