From ea7b0c40c9acae578ced5417ec8d592a0f64cfe9 Mon Sep 17 00:00:00 2001 From: Tanvir Alam Date: Thu, 1 Oct 2026 04:28:55 -0400 Subject: [PATCH] Indent trailing comments on "{" consistently (fixes #1260) 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. --- .../java/JavaInputAstVisitor.java | 10 ++++-- .../googlejavaformat/java/FormatterTest.java | 36 +++++++++++++++++++ .../java/testdata/I1260.input | 14 ++++++++ .../java/testdata/I1260.output | 23 ++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I1260.input create mode 100644 core/src/test/resources/com/google/googlejavaformat/java/testdata/I1260.output diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index d138011f0..91e62d07b 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -564,7 +564,8 @@ private boolean visitArrayInitializer(List expressions boolean allowFilledElementsOnOwnLine = shortItems || !inMemberValuePair; builder.open(plusTwo); - tokenBreakTrailingComment("{", plusTwo); + // Same as visitBlock (#1260): avoid double plusTwo when elements are present. + tokenBreakTrailingComment("{", expressions.isEmpty() ? plusTwo : ZERO); boolean hasTrailingComma = hasTrailingToken(builder.getInput(), expressions, ","); builder.breakOp(hasTrailingComma ? FillMode.FORCED : FillMode.UNIFIED, "", ZERO); if (allowFilledElementsOnOwnLine) { @@ -2317,7 +2318,12 @@ private void visitBlock( } else { builder.open(ZERO); builder.open(plusTwo); - tokenBreakTrailingComment("{", plusTwo); + // Inside plusTwo already. Empty blocks still need plusTwo so a trailing comment on "{" + // lands inside the braces; non-empty blocks use ZERO so the comment lines up with + // statements (otherwise the first format over-indents and a second pass corrects it — + // #1260). + tokenBreakTrailingComment( + "{", node.getStatements().isEmpty() ? plusTwo : ZERO); if (allowLeadingBlankLine == AllowLeadingBlankLine.NO) { builder.blankLineWanted(BlankLineWanted.NO); } else { diff --git a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java index 9ba460428..b180fc575 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/FormatterTest.java @@ -669,4 +669,40 @@ void f() { } """); } + + @Test + public void trailingCommentAfterOpenBraceIsIdempotent() throws Exception { + // Regression for #1260: trailing block comments on "{" were indented an extra + // plusTwo when the opener was already inside an open(plusTwo) level. + String input = + """ + class T { + void f() { + { /* c */ int x = 1; } + } + + int[] a = { /* c */ 1 }; + } + """; + String once = new Formatter().formatSource(input); + String twice = new Formatter().formatSource(once); + assertThat(once).isEqualTo(twice); + assertThat(once) + .isEqualTo( + """ + class T { + void f() { + { + /* c */ + int x = 1; + } + } + + int[] a = { + /* c */ + 1 + }; + } + """); + } } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1260.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1260.input new file mode 100644 index 000000000..6eae47f8b --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1260.input @@ -0,0 +1,14 @@ +class I1260 { + void block() { + { /* trailing on open brace */ int x = 1; } + } + + void switchCase() { + switch (0) { + case 1: { /* Break so we don't hit fall-through warning: */ break;/* ignore STRING */ + } + } + } + + int[] array = { /* trailing on array brace */ 1, 2 }; +} diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1260.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1260.output new file mode 100644 index 000000000..d093acfff --- /dev/null +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/I1260.output @@ -0,0 +1,23 @@ +class I1260 { + void block() { + { + /* trailing on open brace */ + int x = 1; + } + } + + void switchCase() { + switch (0) { + case 1: + { + /* Break so we don't hit fall-through warning: */ + break; /* ignore STRING */ + } + } + } + + int[] array = { + /* trailing on array brace */ + 1, 2 + }; +}