this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <noam.tzvi.dolovich@gmail.com>
Change-Id: I8ab8b7975d374e25b9fba8e02d53f643020b793b
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1193759
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Unity-Result: CUE porcuepine <cue.porcuepine@gmail.com>

authored by

Noam Dolovich and committed by
Daniel Martí
53d5ab31 a835fb51

+43 -37
+1 -2
cmd/cue/cmd/testdata/script/get_go_types.txtar
··· 401 401 402 402 #CustomJSON: _ 403 403 404 - #CustomYAML: { 405 - } 404 + #CustomYAML: {} 406 405 407 406 _#localType: int 408 407
+1 -2
cmd/cue/cmd/testdata/script/trim.txtar
··· 43 43 x: >=5 & <=8 & int 44 44 } 45 45 46 - t: u: { 47 - } 46 + t: u: {} 48 47 } 49 48 50 49 group: {
+21 -9
cue/format/node.go
··· 655 655 656 656 switch { 657 657 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 665 664 } 666 - ff = ffAlt 667 665 } 666 + ff = ffAlt 668 667 case !x.Rbrace.HasRelPos() || !x.Elts[0].Pos().HasRelPos(): 669 668 ws |= newline | nooverride 670 669 } ··· 684 683 f.print(ws, x.Rbrace, token.RBRACE) 685 684 686 685 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) 688 700 f.walkListElems(x.Elts) 689 701 f.print(trailcomma, noblank) 690 702 f.visitComments(f.current.pos)
+4 -8
cue/format/testdata/comments.golden
··· 37 37 // rewrite okay 38 38 a: b: c: d: 2 39 39 40 - m: { 41 - } 40 + m: {} 42 41 m: { 43 42 // empty with comment 44 43 } ··· 51 50 52 51 // Another comment 53 52 54 - something: { 55 - } 53 + something: {} 56 54 // extra comment 57 55 } 58 56 ··· 62 60 // This is a comment 63 61 64 62 // Another comment 65 - something: { 66 - } 63 + something: {} 67 64 68 65 // extra comment 69 66 } ··· 74 71 // Comment2 75 72 76 73 // Another comment 77 - { 78 - }, 74 + {}, 79 75 80 76 // Comment 3 81 77 ]
+3 -3
cue/format/testdata/expressions.golden
··· 33 33 a: bb: cc: 3 34 34 35 35 empty: {} 36 - emptyNewLine: { 37 - 38 - } 36 + emptyNewLine: {} 39 37 someObject: { 40 38 a: 8 41 39 aa: 9 ··· 125 123 e: 'aa \(aaa) aa' 126 124 e: "aa \(aaa)" 127 125 126 + e: [] 127 + e: [] 128 128 e: [1, 2, 129 129 ] 130 130 e: [1, 2]
+5
cue/format/testdata/expressions.input
··· 123 123 e: 'aa \(aaa) aa' 124 124 e: "aa \(aaa)" 125 125 126 + e: [ 127 + ] 128 + e: [ 129 + 130 + ] 126 131 e: [1, 2 127 132 ] 128 133 e: [1, 2]
+1 -2
encoding/protobuf/testdata/istio.io/api/mixer/v1/mixer_proto_gen.cue
··· 258 258 } 259 259 260 260 // Used to carry responses to telemetry reports 261 - #ReportResponse: { 262 - } 261 + #ReportResponse: {}
+2 -4
internal/third_party/yaml/decode_test.go
··· 543 543 ` 544 544 a: {} 545 545 c: 1 546 - d: [ 547 - ] 546 + d: [] 548 547 e: [] 549 548 `, 550 549 `a: {} 551 550 c: 1 552 - d: [ 553 - ] 551 + d: [] 554 552 e: []`, 555 553 }, 556 554
+1 -2
tools/trim/testdata/empty.txtar
··· 13 13 } 14 14 -- out/trim -- 15 15 == in.cue 16 - deployment: [ID=string]: { 17 - } 16 + deployment: [ID=string]: {} 18 17 19 18 deployment: alertmanager: { 20 19 empty: {}
+1 -2
tools/trim/testdata/kube1.txtar
··· 49 49 } 50 50 51 51 service: a: { 52 - ports: [{ 53 - }] 52 + ports: [{}] 54 53 }
+3 -3
tools/trim/trim_test.go
··· 17 17 import ( 18 18 "testing" 19 19 20 + "golang.org/x/tools/txtar" 21 + 20 22 "cuelang.org/go/cue" 21 23 "cuelang.org/go/cue/ast" 22 24 "cuelang.org/go/cue/cuecontext" ··· 24 26 "cuelang.org/go/cue/format" 25 27 "cuelang.org/go/cue/parser" 26 28 "cuelang.org/go/internal/cuetxtar" 27 - "golang.org/x/tools/txtar" 28 29 ) 29 30 30 31 func TestFiles(t *testing.T) { ··· 196 197 out: `service: [string]: { 197 198 ports: [{a: 1}, {a: 1}, ...{extra: 3}] 198 199 } 199 - service: a: { 200 - } 200 + service: a: {} 201 201 `, 202 202 }, { 203 203 name: "do not overmark comprehension",