Keep comments between imports with the import after them - #44
Merged
Merged
Conversation
ImportOrderer.scanImports read imports while the next token was "import" and took along only the rest of an import's line and the // comments right under it. A blank line followed by a comment, or any block comment, ended the scan, and the import after it made reorderImports throw "Imports not contiguous", so the whole file went unformatted (#39, from google/google-java-format#424 and google/google-java-format#546). 18 files of the JDK 21 sources failed this way, with group headings such as "// Javadoc imports:", commented-out imports, and a block comment around an import. Comments between two imports now go with the import after them and move with it when the imports are sorted. A block comment on an import's own line stays with that import, as a // comment there already did. Comments after the last import still belong to what follows. Only files that failed before change: of the 15,747 JDK 21 files, the 18 now format and no other file changes. Two of the 18 change again on a second run, in string concatenations away from the imports, as other files of the JDK already do. The two import tests that expected the error now expect the sorted imports.
abashev
enabled auto-merge
September 23, 2026 07:41
abashev
disabled auto-merge
September 23, 2026 08:24
abashev
added a commit
that referenced
this pull request
Sep 23, 2026
Conflicts with #43 and #44 in ImportOrderer, RemoveUnusedImports and their tests. ImportOrderer: an import now carries both the comments on the lines before it, from #44, and its declaration rendered from its toks, from this branch (palantir#1707). This branch's loop over same-line block comments already covers #44's single block comment after the `;`, so #44's copy of that step goes. Two interactions are decided here: - A javadoc comment right after an import's `;` is no longer an error. This branch rejected it because the formatter moves it onto a line of its own, which used to separate the imports. Since #44 a comment between imports goes with the import after it, and so does this one. - Two copies of an import that differ only in the comment on the lines before them no longer collapse into one, so that comment stays. This branch already kept copies that differ in a comment inside the declaration. RemoveUnusedImports: this branch's loop over JCTree with Trees.getEndPosition, and #43's coalesced ranges and blank-line cleanup. Tests: every case from both sides. This branch's case for a same-line block comment repeated #44's and is folded into it. Its javadoc case now expects the comment to move with the next import. A new case covers the duplicate. Checked: all 1493 formatter tests pass. On the JDK 21 sources (15,747 files, Temurin 21) and the JDK 25 sources (15,368 files, Corretto 25), the merge formats every file exactly as main does, with no errors.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #39. Upstream reports: google/google-java-format#424, google/google-java-format#546, and a sample in palantir#789.
What changes
A comment between two imports made
ImportOrdererthrow "Imports not contiguous (perhaps a comment separates them?)", so nothing in the file was formatted. Now://comment there already did;//lines right under an import stay its trailing comment, as before.In the JDK 21 sources, for example:
Checked
GoogleImportStyleTest: the two cases that expected the error now expect sorted imports, and three new ones cover a line comment after a blank line, a javadoc between imports (the file from Formatter fails with Javadoc on import google/google-java-format#424) and commented-out imports next to static ones.MainTest.commentBetweenImportsMovesWithTheImportAfterItruns the example from A comment between imports fails the whole file: "Imports not contiguous" #39 through the command line twice. All six fail without the change../gradlew :open-java-format:teston JDK 21: 1421 tests, all green.