From 46a200faa5df2f54e6b194e01d689aa947bdb46b Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 17 Nov 2026 06:43:47 -0800 Subject: [PATCH] Skip import reordering when comments separate imports. When a block or Javadoc comment separates `import` declarations, it is ambiguous which import the comment belongs to. Return an empty replacement list in `ImportOrderer.findReplacements` instead of throwing a `FormatterException` so the rest of the file can still be formatted. Fixes https://github.com/google/google-java-format/issues/424 PiperOrigin-RevId: 1023694754 --- .../googlejavaformat/java/ImportOrderer.java | 2 +- .../googlejavaformat/java/FormatterTest.java | 27 +++++++++++++++++++ .../java/ImportOrdererTest.java | 8 ++---- 3 files changed, 30 insertions(+), 7 deletions(-) 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?)", - } + {} }, { {