Skip to content

Commit

Permalink
Omit blank lines after imports in package-info files
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=157895967
  • Loading branch information
cushon authored and ronshapiro committed Jun 7, 2017
1 parent 9b6d334 commit 02de39a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import com.google.common.collect.ImmutableSortedSet;
import com.google.googlejavaformat.Newlines;
import com.google.googlejavaformat.java.JavaInput.Tok;
import java.util.ArrayList;
import java.util.List;
import org.openjdk.tools.javac.parser.Tokens.TokenKind;

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

// Add back the text from after the point where we stopped tokenizing.
String tail;
if (toks.isEmpty()) {
tail = "";
} else {
Tok lastTok = getLast(toks);
int tailStart = lastTok.getPosition() + lastTok.length();
tail = text.substring(tailStart);
}

StringBuilder result = new StringBuilder();
result.append(
CharMatcher.whitespace().trimTrailingFrom(tokString(0, unindentedFirstImportStart)));
if (result.length() > 0) {
result.append(lineSeparator).append(lineSeparator);
}
result.append(reorderedImportsString(imports.imports));
result.append(lineSeparator);
result.append(
CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size())));
result.append(tail);

List<String> tail = new ArrayList<>();
tail.add(CharMatcher.whitespace().trimLeadingFrom(tokString(afterLastImport, toks.size())));
if (!toks.isEmpty()) {
Tok lastTok = getLast(toks);
int tailStart = lastTok.getPosition() + lastTok.length();
tail.add(text.substring(tailStart));
}
if (tail.stream().anyMatch(s -> !s.isEmpty())) {
result.append(lineSeparator);
tail.forEach(result::append);
}

return result.toString();
}

Expand Down
22 changes: 22 additions & 0 deletions core/src/test/java/com/google/googlejavaformat/java/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,26 @@ public void importRemoveErrorParseError() throws Exception {
assertThat(main.format("-")).isEqualTo(1);
assertThat(err.toString()).contains("<stdin>:4:3: error: class, interface, or enum expected");
}

@Test
public void packageInfo() throws Exception {
String[] input = {
"@CheckReturnValue",
"@ParametersAreNonnullByDefault",
"package com.google.common.labs.base;",
"",
"import javax.annotation.CheckReturnValue;",
"import javax.annotation.ParametersAreNonnullByDefault;",
"",
};
StringWriter out = new StringWriter();
StringWriter err = new StringWriter();
Main main =
new Main(
new PrintWriter(out, true),
new PrintWriter(err, true),
new ByteArrayInputStream(joiner.join(input).getBytes(UTF_8)));
assertThat(main.format("-")).isEqualTo(0);
assertThat(out.toString()).isEqualTo(joiner.join(input));
}
}

0 comments on commit 02de39a

Please sign in to comment.