Skip to content

Commit 80c91e5

Browse files
authored
Expand arrays with attached comments in the formatter (#740)
Expand arrays with attached comments in the formatter. This matches the logic in print dict and fixes the trailing `//` comments corrupting the output when collapsed onto one line. Goldens updated to match new behavior.
1 parent 9fce8a6 commit 80c91e5

7 files changed

Lines changed: 41 additions & 3 deletions

File tree

experimental/ast/printer/expr.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,12 @@ func (p *printer) printArray(expr ast.ExprArray, gap gapStyle) {
224224
}
225225

226226
hasComments := triviaHasComments(slots)
227+
// Also check for comments attached to any token in the scope
228+
// (trailing on open bracket, leading on close bracket, or on any
229+
// interior token). These force multi-line expansion.
230+
if !hasComments {
231+
hasComments = p.scopeHasAttachedComments(brackets)
232+
}
227233

228234
if elements.Len() == 0 && !hasComments {
229235
p.printToken(openTok, gap)

experimental/ast/printer/testdata/bufformat/proto2/option/v1/option_message_field.golden

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,11 @@ message Test {
6363
foo: "goo" // Trailing comment on foo
6464

6565
// Leading comment on extension name.
66-
[/* One */ foo.bar.Test.Nested._NestedNested._garblez /* Two */] /* Three */ : "boo" // Trailing comment on extension name.
66+
[
67+
/* One */
68+
foo.bar.Test.Nested._NestedNested._garblez
69+
/* Two */
70+
] /* Three */ : "boo" // Trailing comment on extension name.
6771

6872
// Leading comment on '}'.
6973
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
option (any) = {
4+
array: [
5+
"x" // comment
6+
]
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
option (any) = {
4+
array: [
5+
"x" // comment
6+
]
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
option (any) = {
4+
array: [
5+
"x" // comment
6+
]
7+
};

experimental/ast/printer/testdata/format/option_message_field.proto.default.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@ message Foo {
3232
foo: "goo" // Trailing comment on foo
3333

3434
// Leading comment on extension name.
35-
[/* One */ foo.bar.Test.Nested._NestedNested._garblez /* Two */] /* Three */ : "boo" // Trailing comment on extension name.
35+
[
36+
/* One */
37+
foo.bar.Test.Nested._NestedNested._garblez /* Two */
38+
] /* Three */ : "boo" // Trailing comment on extension name.
3639

3740
// Leading comment on '}'.
3841
};

experimental/ast/printer/testdata/format/option_message_field.proto.legacy.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,11 @@ message Foo {
3232
foo: "goo" // Trailing comment on foo
3333

3434
// Leading comment on extension name.
35-
[/* One */ foo.bar.Test.Nested._NestedNested._garblez /* Two */] /* Three */ : "boo" // Trailing comment on extension name.
35+
[
36+
/* One */
37+
foo.bar.Test.Nested._NestedNested._garblez
38+
/* Two */
39+
] /* Three */ : "boo" // Trailing comment on extension name.
3640

3741
// Leading comment on '}'.
3842
};

0 commit comments

Comments
 (0)