GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — Indent a comment after an opening delimiter like the code after it by abashev · Pull Request #175 · openjavaformat/open-java-format Conversation
Collaborator
Fixes #171. What happensA 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 Input: class T { void f(int n) { for ( // comment int i = 0; i < n; i++) {} } } enum E { A, // comment ; } Before: class T { void f(int n) { for ( // comment int i = 0; i < n; i++) {} } } enum E { A, // comment ; } After: class T { void f(int n) { for ( // comment int i = 0; i < n; i++) {} } } enum E { A, // comment ; } Why
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
TestsThere are six new cases, On JDK 21:
JDK corpusNone 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.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsA 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. Uh oh!
There was an error while loading. Please reload this page.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ReviewersNo reviewsUh oh!
There was an error while loading. Please reload this page. AssigneesNo one assignedLabels
None yet
ProjectsMilestoneNo milestoneDevelopmentSuccessfully merging this pull request may close these issues. Leading comment after an opening delimiter is hoisted out of its groupUh oh!
There was an error while loading. Please reload this page. 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. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |