Skip to content

Indent trailing comments on "{" consistently (fixes #1260) - #1464

Open
tanvir-ux wants to merge 1 commit into
google:masterfrom
tanvir-ux:fix-1260-brace-trailing-comment-indent
Open

tanvir-ux wants to merge 1 commit into
google:masterfrom
tanvir-ux:fix-1260-brace-trailing-comment-indent

Conversation

@tanvir-ux

Copy link
Copy Markdown

Summary

Fixes #1260: formatting { /* comment */ stmt; } (and the analogous array-initializer form) was not idempotent — the first pass over-indented the broken-out trailing comment, and the second pass corrected it.

Cause

visitBlock / visitArrayInitializer emit "{" with tokenBreakTrailingComment("{", plusTwo) after already opening a plusTwo level. The trailing-comment break indent stacked another plusTwo, so a comment that was still attached to "{" in the input landed two spaces deeper than the following statements.

Fix

  • Non-empty block / initializer: use ZERO for the trailing-comment indent (already inside plusTwo).
  • Empty block / initializer: keep plusTwo so a lone trailing comment still sits inside the braces (preserves B20580212).

Tests

  • New I1260 integration input/output (block, switch case, array).
  • FormatterTest#trailingCommentAfterOpenBraceIsIdempotent.
  • mvn -pl core -Dtest=FormatterIntegrationTest,FormatterTest test: 1083 tests, 0 failures (includes full integration + idempotent suite and B20580212).

When "{" was emitted inside an already-open plusTwo level, passing plusTwo
again for breakAndIndentTrailingComment over-indented trailing block
comments. The next format pass then corrected them, so formatting was not
idempotent for "{ /* comment */ stmt; }" and array initializers.

Use ZERO for non-empty blocks/initializers (comment aligns with body) and
keep plusTwo when empty so standalone trailing comments still sit inside
the braces.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Inconsistent formatting (first pass, second pass results in a different output).

1 participant