Skip to content

Commit 7225e17

Browse files
committed
Handle two imports on the same line during reordering
MOE_MIGRATED_REVID=172374669
1 parent 2b7d03a commit 7225e17

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

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

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ private ImportOrderer(String text, ImmutableList<Tok> toks) throws FormatterExce
6565
}
6666

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

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

7878
Import(String imported, String trailing, boolean isStatic) {
7979
this.imported = imported;
80-
this.trailing = trailing;
80+
this.trailing = trailing.trim();
8181
this.isStatic = isStatic;
8282
}
8383

@@ -93,8 +93,17 @@ public int compareTo(Import that) {
9393
// This is a complete line to be output for this import, including the line terminator.
9494
@Override
9595
public String toString() {
96-
String staticString = isStatic ? "static " : "";
97-
return "import " + staticString + imported + ";" + trailing;
96+
StringBuilder sb = new StringBuilder();
97+
sb.append("import ");
98+
if (isStatic) {
99+
sb.append("static ");
100+
}
101+
sb.append(imported).append(';');
102+
if (!trailing.isEmpty()) {
103+
sb.append(' ').append(trailing);
104+
}
105+
sb.append(lineSeparator);
106+
return sb.toString();
98107
}
99108
}
100109

@@ -215,11 +224,14 @@ private ImportsAndIndex scanImports(int i) throws FormatterException {
215224
trailing.append(tokenAt(i));
216225
i++;
217226
}
218-
if (!isNewlineToken(i)) {
227+
if (isNewlineToken(i)) {
228+
trailing.append(tokenAt(i));
229+
i++;
230+
} else if (tokenAt(i).equals("import")) {
231+
// continue
232+
} else {
219233
throw new FormatterException("Extra tokens after import: " + tokenAt(i));
220234
}
221-
trailing.append(tokenAt(i));
222-
i++;
223235
imports.add(new Import(importedName, trailing.toString(), isStatic));
224236
// Remember the position just after the import we just saw, before skipping blank lines.
225237
// If the next thing after the blank lines is not another import then we don't want to

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,11 +345,13 @@ public static Collection<Object[]> parameters() {
345345
},
346346
{
347347
{
348-
// we don't introduce new line breaks
349-
"import com.foo.Second; import com.foo.First;",
348+
"import com.foo.Second; import com.foo.First;", "class Test {}",
350349
},
351350
{
352-
"!!Extra tokens after import: import",
351+
"import com.foo.First;", //
352+
"import com.foo.Second;",
353+
"",
354+
"class Test {}",
353355
}
354356
}
355357
};
@@ -362,7 +364,7 @@ public static Collection<Object[]> parameters() {
362364
output = input;
363365
}
364366
String[] parameters = {
365-
Joiner.on('\n').join(input) + '\n',
367+
Joiner.on('\n').join(input) + '\n', //
366368
Joiner.on('\n').join(output) + '\n',
367369
};
368370
parameters[0] = parameters[0].replace("\\\n", "");

0 commit comments

Comments
 (0)