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 52525ed8a..27d2fde68 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java +++ b/core/src/main/java/com/google/googlejavaformat/java/ImportOrderer.java @@ -94,7 +94,7 @@ private ImmutableList findReplacements() throws FormatterException // Make sure there are no more imports before the next class (etc) definition. Optional maybeLaterImport = findIdentifier(afterLastImport, IMPORT_OR_CLASS_START); if (maybeLaterImport.isPresent() && tokenAt(maybeLaterImport.get()).equals("import")) { - throw new FormatterException("Imports not contiguous (perhaps a comment separates them?)"); + return ImmutableList.of(); } int start = toks.get(unindentedFirstImportStart).getPosition(); diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java index a63e4ad53..07b0ca7bd 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java @@ -1320,4 +1320,31 @@ void f() { """; assertThat(new Formatter().formatSource(input)).isEqualTo(expected); } + + @Test + public void blockCommentBetweenImports() throws Exception { + String input = + """ + import java.util.Set; + /**Javadoc*/ + import java.util.List; + + class T { + List xs; + Set ys; + } + """; + String expected = + """ + import java.util.Set; + /** Javadoc */ + import java.util.List; + + class T { + List xs; + Set ys; + } + """; + assertThat(new Formatter().formatSourceAndFixImports(input)).isEqualTo(expected); + } } 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 c281c8276..d161d85d0 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/ImportOrdererTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/ImportOrdererTest.java @@ -402,18 +402,14 @@ public static Collection parameters() { " between imports either */", "import com.foo.Third;", }, - { - "!!Imports not contiguous (perhaps a comment separates them?)", - } + {} }, { { "import com.foo.Second; /* no block comments after imports */", // "import com.foo.First;", }, - { - "!!Imports not contiguous (perhaps a comment separates them?)", - } + {} }, { {