Skip to content

Commit

Permalink
cmd/cue: correctly import comments after a field key
Browse files Browse the repository at this point in the history
The YAML decoder attached an inline key comment to the label,
rather than to the entire field, which caused the comment to be printed
before the colon character and cause weird formatting.

Then teach cue/format to print an inline comment on a field colon.

Fixes #3214.

Change-Id: I17e669bc6c129dde86478d604a28d07fc63f7b69
Signed-off-by: haoqixu <hq.xu0o0@gmail.com>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196291
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
haoqixu authored and mvdan committed Jun 14, 2024
1 parent 859f1f6 commit cc7cb90
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
12 changes: 12 additions & 0 deletions cmd/cue/cmd/testdata/script/import_auto.txtar
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
exec cue import -o - ./openapi.yaml
cmp stdout expect-openapi

# Issue #3214
exec cue import -o - -f ./import1.yaml
cmp stdout expect-stdout1

-- import1.yaml --
a: # inline comment for a
b: "value" # inline comment for b
-- expect-stdout1 --
a: // inline comment for a
{
b: "value" // inline comment for b
}
-- expect-openapi --
// An OpenAPI file

Expand Down
1 change: 1 addition & 0 deletions cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,7 @@ func (f *formatter) decl(decl ast.Decl) {
} else {
f.print(blank, nooverride, n.Token)
}
f.visitComments(f.current.pos)

if mem := f.inlineField(n); mem != nil {
switch {
Expand Down
2 changes: 1 addition & 1 deletion internal/encoding/yaml/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ outer:
if err != nil {
return err
}
d.addCommentsToNode(label, yk, 1)
d.addCommentsToNode(field, yk, 2)
field.Label = label

if mergeValues {
Expand Down

0 comments on commit cc7cb90

Please sign in to comment.