diff --git a/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java b/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java index 8063eaada..e1a4b2baa 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java +++ b/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java @@ -227,10 +227,6 @@ private ImportsAndIndex scanImports(int i) throws FormatterException { 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)); } imports.add(new Import(importedName, trailing.toString(), isStatic)); // Remember the position just after the import we just saw, before skipping blank lines. diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavacTokens.java b/core/src/main/java/com/google/googlejavaformat/java/JavacTokens.java index f036ccd84..72f8bce3a 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavacTokens.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavacTokens.java @@ -99,7 +99,7 @@ public static ImmutableList getTokens( } if (stopTokens.contains(t.kind)) { if (t.kind != TokenKind.EOF) { - end = t.endPos - 1; + end = t.pos; } break; } diff --git a/core/src/test/java/com/google/googlejavaformat/java/ImportOrdererTest.java b/core/src/test/java/com/google/googlejavaformat/java/ImportOrdererTest.java index f6d34831c..d94d7df91 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/ImportOrdererTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/ImportOrdererTest.java @@ -286,7 +286,7 @@ public static Collection parameters() { "import com.foo.First;", }, { - "!!Extra tokens after import: /* no block comments after imports */", + "!!Imports not contiguous (perhaps a comment separates them?)", } }, { @@ -353,7 +353,18 @@ public static Collection parameters() { "", "class Test {}", } - } + }, + { + { + "import com.foo.Second; import com.foo.First; class Test {}", + }, + { + "import com.foo.First;", // + "import com.foo.Second;", + "", + "class Test {}", + } + }, }; ImmutableList.Builder builder = ImmutableList.builder(); for (String[][] inputAndOutput : inputsOutputs) {