@@ -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
0 commit comments