Skip to content

Commit 53d5ab3

Browse files
NoamTDmvdan
authored andcommitted
cue/format: collapse empty structs/lists to single line
This changes the cue formatting logic to always format an empty struct on a single line. The following cue: struct: { } list: [ ] Will now be formatted like so: struct: {} list: [] Note that we preserve the multi-line structure in case a comment is present in an empty list/struct. For example: struct: { // some comment } will be formatted as-is. Fixes #278. Signed-off-by: Noam Dolovich <[email protected]> Change-Id: I8ab8b7975d374e25b9fba8e02d53f643020b793b Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193759 Reviewed-by: Daniel Martí <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Unity-Result: CUE porcuepine <[email protected]>
1 parent a835fb5 commit 53d5ab3

File tree

11 files changed

+43
-37
lines changed

11 files changed

+43
-37
lines changed

cmd/cue/cmd/testdata/script/get_go_types.txtar

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,7 @@ _#internalIdentifier: #Identifier & "internal"
401401

402402
#CustomJSON: _
403403

404-
#CustomYAML: {
405-
}
404+
#CustomYAML: {}
406405

407406
_#localType: int
408407

cmd/cue/cmd/testdata/script/trim.txtar

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ foo: multipath: {
4343
x: >=5 & <=8 & int
4444
}
4545

46-
t: u: {
47-
}
46+
t: u: {}
4847
}
4948

5049
group: {

cue/format/node.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -655,16 +655,15 @@ func (f *formatter) exprRaw(expr ast.Expr, prec1, depth int) {
655655

656656
switch {
657657
case len(x.Elts) == 0:
658-
if !x.Rbrace.HasRelPos() {
659-
// collapse curly braces if the body is empty.
660-
ffAlt := blank | nooverride
661-
for _, c := range x.Comments() {
662-
if c.Position == 1 {
663-
ffAlt = ff
664-
}
658+
// collapse curly braces if the body is empty.
659+
ffAlt := blank | nooverride
660+
for _, c := range x.Comments() {
661+
if c.Position == 1 {
662+
ffAlt = ff
663+
break
665664
}
666-
ff = ffAlt
667665
}
666+
ff = ffAlt
668667
case !x.Rbrace.HasRelPos() || !x.Elts[0].Pos().HasRelPos():
669668
ws |= newline | nooverride
670669
}
@@ -684,7 +683,20 @@ func (f *formatter) exprRaw(expr ast.Expr, prec1, depth int) {
684683
f.print(ws, x.Rbrace, token.RBRACE)
685684

686685
case *ast.ListLit:
687-
f.print(x.Lbrack, token.LBRACK, noblank, indent)
686+
ws := noblank | indent
687+
if len(x.Elts) == 0 {
688+
// collapse square brackets if the body is empty.
689+
collapseWs := blank | nooverride
690+
for _, c := range x.Comments() {
691+
if c.Position == 1 {
692+
collapseWs = ws
693+
break
694+
}
695+
}
696+
ws |= collapseWs
697+
}
698+
699+
f.print(x.Lbrack, token.LBRACK, ws)
688700
f.walkListElems(x.Elts)
689701
f.print(trailcomma, noblank)
690702
f.visitComments(f.current.pos)

cue/format/testdata/comments.golden

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ a: b: c: 2
3737
// rewrite okay
3838
a: b: c: d: 2
3939

40-
m: {
41-
}
40+
m: {}
4241
m: {
4342
// empty with comment
4443
}
@@ -51,8 +50,7 @@ struct1: {
5150

5251
// Another comment
5352

54-
something: {
55-
}
53+
something: {}
5654
// extra comment
5755
}
5856

@@ -62,8 +60,7 @@ struct2: {
6260
// This is a comment
6361

6462
// Another comment
65-
something: {
66-
}
63+
something: {}
6764

6865
// extra comment
6966
}
@@ -74,8 +71,7 @@ list1: [
7471
// Comment2
7572

7673
// Another comment
77-
{
78-
},
74+
{},
7975

8076
// Comment 3
8177
]

cue/format/testdata/expressions.golden

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ import "list"
3333
a: bb: cc: 3
3434

3535
empty: {}
36-
emptyNewLine: {
37-
38-
}
36+
emptyNewLine: {}
3937
someObject: {
4038
a: 8
4139
aa: 9
@@ -125,6 +123,8 @@ import "list"
125123
e: 'aa \(aaa) aa'
126124
e: "aa \(aaa)"
127125

126+
e: []
127+
e: []
128128
e: [1, 2,
129129
]
130130
e: [1, 2]

cue/format/testdata/expressions.input

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,11 @@ import "list"
123123
e: 'aa \(aaa) aa'
124124
e: "aa \(aaa)"
125125

126+
e: [
127+
]
128+
e: [
129+
130+
]
126131
e: [1, 2
127132
]
128133
e: [1, 2]

encoding/protobuf/testdata/istio.io/api/mixer/v1/mixer_proto_gen.cue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,5 +258,4 @@ import (
258258
}
259259

260260
// Used to carry responses to telemetry reports
261-
#ReportResponse: {
262-
}
261+
#ReportResponse: {}

internal/third_party/yaml/decode_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,12 @@ b: {
543543
`
544544
a: {}
545545
c: 1
546-
d: [
547-
]
546+
d: []
548547
e: []
549548
`,
550549
`a: {}
551550
c: 1
552-
d: [
553-
]
551+
d: []
554552
e: []`,
555553
},
556554

tools/trim/testdata/empty.txtar

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ deployment: alertmanager: {
1313
}
1414
-- out/trim --
1515
== in.cue
16-
deployment: [ID=string]: {
17-
}
16+
deployment: [ID=string]: {}
1817

1918
deployment: alertmanager: {
2019
empty: {}

tools/trim/testdata/kube1.txtar

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,5 @@ service: a: {
4949
}
5050

5151
service: a: {
52-
ports: [{
53-
}]
52+
ports: [{}]
5453
}

0 commit comments

Comments
 (0)