Skip to content

Commit

Permalink
Fix field grouping when rewriting multiline compact options to a sing…
Browse files Browse the repository at this point in the history
…le line
  • Loading branch information
kralicky committed Oct 14, 2024
1 parent 6479c46 commit 44bf8db
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
4 changes: 4 additions & 0 deletions pkg/format/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,10 @@ func columnFormatElements[T ast.Node, C elementsContainer[T]](f *formatter, ctr
default:
shouldStartNewGroup = false
}
case *ast.FieldNode:
if prevNode.Options != nil && !f.compactOptionsShouldBeExpanded(prevNode.Options) {
shouldStartNewGroup = false
}
}
}

Expand Down
43 changes: 34 additions & 9 deletions pkg/format/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,21 +210,46 @@ message Foo {
// comment 1
// comment 2
];
}`[1:],
},
13: {
input: `
message Foo {
reserved 50; // comment
repeated string field1 = 52;
optional string field2 = 84 [ (opt) = {foo: true, bar: ENUM_VALUE}
];
optional string field3 = 101;
optional string field4 = 102 [(opt).test = true];
optional bool field5 = 106;
optional Aaaaaaaaaaaaaaaaaaaaaa field6 = 116;}
`[1:],
want: `
message Foo {
reserved 50; // comment
repeated string field1 = 52;
optional string field2 = 84 [(opt) = {foo: true, bar: ENUM_VALUE}];
optional string field3 = 101;
optional string field4 = 102 [(opt).test = true];
optional bool field5 = 106;
optional Aaaaaaaaaaaaaaaaaaaaaa field6 = 116;
}`[1:],
},
}

for i, c := range cases[12:] {
if c.want == "" {
c.want = c.input
}
for i, c := range cases {
t.Run("", func(t *testing.T) {
if c.want == "" {
c.want = c.input
}

root, err := parser.Parse("", strings.NewReader(c.input), reporter.NewHandler(nil), 0)
require.NoError(t, err)
root, err := parser.Parse("", strings.NewReader(c.input), reporter.NewHandler(nil), 0)
require.NoError(t, err)

got, err := format.PrintNode(root, root)
require.NoError(t, err)
got, err := format.PrintNode(root, root)
require.NoError(t, err)

require.Equal(t, c.want, got, "case %d", i)
require.Equal(t, c.want, got, "case %d", i)
})
}
}

0 comments on commit 44bf8db

Please sign in to comment.