Skip to content

Commit

Permalink
cue/format: preserve comments associated with ellipsis
Browse files Browse the repository at this point in the history
When simplification is enabled, the current implementation prints a
"dummy" ellipsis during formatting, instead of the original ellipsis.
This means that comments associated with the original ellipsis are not
printed in the formatted CUE.
To fix this, we associate the original comments with the dummy ellipsis
so they won't get lost.

Fixes #3198.

Signed-off-by: Noam Dolovich <noam.tzvi.dolovich@gmail.com>
Change-Id: Ibebbd97ac768de7a2c0d9855c9ec6c7a497435c8
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195884
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
NoamTD authored and mvdan committed Jun 7, 2024
1 parent da2a575 commit 9bd3e17
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
11 changes: 7 additions & 4 deletions cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func hasDocComments(d ast.Decl) bool {
func (f *formatter) walkDeclList(list []ast.Decl) {
f.before(nil)
d := 0
hasEllipsis := false
var ellipsis ast.Decl
for i, x := range list {
if i > 0 {
f.print(declcomma)
Expand All @@ -128,7 +128,7 @@ func (f *formatter) walkDeclList(list []ast.Decl) {
}
}
if f.printer.cfg.simplify && internal.IsEllipsis(x) {
hasEllipsis = true
ellipsis = x
continue
}
f.decl(x)
Expand Down Expand Up @@ -156,8 +156,11 @@ func (f *formatter) walkDeclList(list []ast.Decl) {
}
f.print(f.current.parentSep)
}
if hasEllipsis {
f.decl(&ast.Ellipsis{})
if ellipsis != nil {
// ensure that comments associated with the original ellipsis are preserved
n := &ast.Ellipsis{}
ast.SetComments(n, ast.Comments(ellipsis))
f.decl(n)
f.print(f.current.parentSep)
}
f.after(nil)
Expand Down
8 changes: 6 additions & 2 deletions cue/format/testdata/comments.golden
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@ foo: [
{

foo: {
...
// some
... // comment
}
...
// comments
... // surrounding

// an ellipsis
}

0 comments on commit 9bd3e17

Please sign in to comment.