Repository navigation
Indent a comment after an opening delimiter like the code after it - #175
Merged
Merged
Conversation
A comment on its own line right after `for (`, after the `(` of a try with several resources or of a multi-catch clause, after the `<` of explicit type arguments or after `assert` with a detail message, or before the `;` that ends the constants of an enum, came out at the indent of the statement, and so did the code after it; the comment and the `;` of an enum even landed in the enum's own column (#171): for ( // comment int i = 0; i < n; i++) {} OpsBuilder.build puts the comments before a token ahead of the OpenOps right before it, so that a comment before a statement or an expression stays out of their levels. These delimiters open the level that indents what follows them right before its first token, so the comment left that level, and the forced break after it took the next line out with it. OpsBuilder.fenceComments marks where build stops looking back, and the six places call it right after opening their level, as google/google-java-format#1501 does in its own layout builder (groundwork in google/google-java-format#1500, cases in google/google-java-format#1502). The comment now breaks the level it is in, so the parts of a for header and the `:` of an assert get a line each: for ( // comment int i = 0; i < n; i++) {} An assert without a detail message and a try with one resource open a level without indent, so their comments stay where they were. A block comment that shares its line with the code after it stays with it, on the line of the delimiter when that fits. The six cases commentAfterOpeningDelimiter-* fail without the fix. Of the 15,747 files of the JDK 21 sources none formats differently: none has a comment on its own line after one of these delimiters. Tests on JDK 21: FormatterCasesTest 887 -> 893; 1414 tests in the module.
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.
Fixes #171.
What happens
A comment on its own line right after an opening delimiter came out at the indent of the statement, and so did the code after it. The delimiters are
for (, the(of a try with several resources and of a multi-catch clause, the<of explicit type arguments (new <String>T(),T::<String>g),assertwith a detail message, and the;that ends the constants of an enum. For the enum, the comment and the;even landed in the enum's own column.Input:
Before:
After:
Why
OpsBuilder.buildplaces the comments before a token ahead of theOpenOps right before it, so that a comment before a statement or an expression stays out of their levels. Each of these delimiters opens the level that indents what follows it right before that level's first token. The comment therefore left the level, and the forced break after the comment took the next line out with it.google/google-java-format#1501 fixes the same defect in google-java-format's new layout builder (groundwork in google/google-java-format#1500, baseline cases in google/google-java-format#1502). Its code does not apply to our engine, but the idea does.
The fix
OpsBuilder.fenceComments()adds a marker thatbuildstops at when it looks back overOpenOps, and drops before the ops become aDoc.visitAssert, the;after enum constants,visitForLoop,visitTry,visitCatchClauseandaddTypeArguments.:of an assert get a line each, as they do whenever that level breaks.for (/* c */ int i = 0; i < n; i++) {}.Tests
There are six new cases,
test-cases/commentAfterOpeningDelimiter-{assert,enumSemicolon,forHeader,tryResources,multiCatch,typeArguments}.case. Each input mixes upstream's shape, the shape 21.X.X produces today, and odd breaks and spacing. All six failed on the unchanged code with the comment at the statement's indent, and they pass with the fix, including the second run.On JDK 21:
./gradlew test --rerunis green for all modules (Gradle plugin 51 tests, IntelliJ plugin 10).JDK corpus
None of the 15,747 files of the JDK 21 sources formats differently. None of them has a comment on its own line after one of these delimiters.