Skip to content

Commit

Permalink
Handle two imports on the same line during reordering
Browse files Browse the repository at this point in the history
MOE_MIGRATED_REVID=172374669
  • Loading branch information
cushon committed Oct 17, 2017
1 parent 2b7d03a commit 7225e17
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private ImportOrderer(String text, ImmutableList<Tok> toks) throws FormatterExce
}

/** An import statement. */
private static class Import implements Comparable<Import> {
private class Import implements Comparable<Import> {
/** The name being imported, for example {@code java.util.List}. */
final String imported;

Expand All @@ -77,7 +77,7 @@ private static class Import implements Comparable<Import> {

Import(String imported, String trailing, boolean isStatic) {
this.imported = imported;
this.trailing = trailing;
this.trailing = trailing.trim();
this.isStatic = isStatic;
}

Expand All @@ -93,8 +93,17 @@ public int compareTo(Import that) {
// This is a complete line to be output for this import, including the line terminator.
@Override
public String toString() {
String staticString = isStatic ? "static " : "";
return "import " + staticString + imported + ";" + trailing;
StringBuilder sb = new StringBuilder();
sb.append("import ");
if (isStatic) {
sb.append("static ");
}
sb.append(imported).append(';');
if (!trailing.isEmpty()) {
sb.append(' ').append(trailing);
}
sb.append(lineSeparator);
return sb.toString();
}
}

Expand Down Expand Up @@ -215,11 +224,14 @@ private ImportsAndIndex scanImports(int i) throws FormatterException {
trailing.append(tokenAt(i));
i++;
}
if (!isNewlineToken(i)) {
if (isNewlineToken(i)) {
trailing.append(tokenAt(i));
i++;
} else if (tokenAt(i).equals("import")) {
// continue
} else {
throw new FormatterException("Extra tokens after import: " + tokenAt(i));
}
trailing.append(tokenAt(i));
i++;
imports.add(new Import(importedName, trailing.toString(), isStatic));
// Remember the position just after the import we just saw, before skipping blank lines.
// If the next thing after the blank lines is not another import then we don't want to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,13 @@ public static Collection<Object[]> parameters() {
},
{
{
// we don't introduce new line breaks
"import com.foo.Second; import com.foo.First;",
"import com.foo.Second; import com.foo.First;", "class Test {}",
},
{
"!!Extra tokens after import: import",
"import com.foo.First;", //
"import com.foo.Second;",
"",
"class Test {}",
}
}
};
Expand All @@ -362,7 +364,7 @@ public static Collection<Object[]> parameters() {
output = input;
}
String[] parameters = {
Joiner.on('\n').join(input) + '\n',
Joiner.on('\n').join(input) + '\n', //
Joiner.on('\n').join(output) + '\n',
};
parameters[0] = parameters[0].replace("\\\n", "");
Expand Down

0 comments on commit 7225e17

Please sign in to comment.