Stop comments that share a line from doubling each other's indent - #42
Merged
Merged
Conversation
Comment.computeBreaks moved the column past a comment by adding the length of the comment's last line to the column the comment started at. For a comment that spans lines, JavaCommentsHelper.rewrite has already indented that last line to the start column, so the column was counted twice, and each further comment on the line was re-indented from the doubled column. Commented-out code with javadoc in it, where every "*//** javadoc *//*" puts several multi-line comments on one line, grew from 817 bytes to 4.3 MB with 16 of them and ran out of a 2 GB heap with 26 (#36, from google/google-java-format#413). After a comment that spans lines the column is now the length of its last line; after a one-line comment it is still the start column plus the comment. The 26 comments now format into 12.5 kB in 0.33 s, each indented to the column where it starts. A golden of the reported file and a test with 16 such comments fail without the change. The 15,747 files of the JDK 21 sources format exactly as before.
abashev
enabled auto-merge
September 23, 2026 07:28
abashev
disabled auto-merge
September 23, 2026 08:20
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 #36. Upstream report: google/google-java-format#413.
What changes
Comment.computeBreaksset the column after a comment to the column it started at plus the length of its last line. For a comment that spans lines,JavaCommentsHelper.rewritehas already indented that last line to the start column, so the column was counted twice, and every further comment on the same line started twice as far right as the one before. Commented-out code with javadoc in it, where each*//** javadoc *//*puts several multi-line comments on one line, grew exponentially:After a comment that spans lines the column is now the length of its last line; after a one-line comment it is still the start column plus the comment.
Checked
ojf-issue-36-multiline-comments-on-one-line(the file from OutOfMemoryError on formating file with no actual code google/google-java-format#413) andFormatterTest.indentsCommentsThatShareALineLinearlyfail without the change: the golden'sformatand its three idempotence checks, and the size test. All pass with it../gradlew :open-java-format:teston JDK 21: 1422 tests, all green.