Skip to content

Commit

Permalink
cmd/format: be consistent with whitespace in lists
Browse files Browse the repository at this point in the history
Leading whitespace is not strictly enforced, but is incorrectly present
when the formatting of a list is fixed. The behaviour can be made
consistent by requesting no blanks at the start of a list.

	```diff
	-[ 1]
	-[ for x in y {}]
	+[1]
	+ [for x in y {}]
	```

Fixes #1023

Change-Id: I8a57461d99cf6fd67105ab01283906e8ee60fac8
Signed-off-by: Thomas Way <thomas@6f.io>
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1171302
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
  • Loading branch information
uhthomas authored and mvdan committed Nov 1, 2023
1 parent 8c826bc commit d70007c
Show file tree
Hide file tree
Showing 66 changed files with 164 additions and 132 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ configuration file, instead of the entire configuration file itself.
Examples:
$ cat <<EOF > foo.cue
a: [ "a", "b", "c" ]
a: ["a", "b", "c"]
EOF
$ cue eval foo.cue -e a[0] -e a[2]
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ Commands are defined in CUE as follows:
command: deploy: {
exec.Run
cmd: "kubectl"
args: [ "-f", "deploy" ]
args: ["-f", "deploy"]
in: json.Encode(userValue) // encode the emitted configuration.
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_cycle.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ for _, input in inputs {
}

command: print: cli.Print & {
text: strings.Join([ for key, val in outputs { "key=\(key) val=\(val)" } ], "\n")
text: strings.Join([for key, val in outputs { "key=\(key) val=\(val)" }], "\n")
}

-- stdout2397.golden --
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_func.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "encoding/json"

x: y: 2

objects: [ for v in [ json.Marshal(x) ] {v} ]
objects: [for v in [json.Marshal(x)] {v}]

-- k_tool.cue --
package kube
Expand Down
4 changes: 2 additions & 2 deletions cmd/cue/cmd/testdata/script/cmd_issue2060.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ buildSource: {
Bindings: [
for _, strokeDef in strokeDefs {
let bindingMap = strokeDef.Bindings["\(PlatformName)"]
let bindingIds = [ for k, v in bindingMap {name: k, idx: v}]
let bindingIds = [for k, v in bindingMap {name: k, idx: v}]
let bindingIdsSorted = list.Sort(bindingIds, {x: {}, y: {}, less: x.idx < y.idx})
let bindingKeys = [ for _, kv in bindingIdsSorted {kv.name}]
let bindingKeys = [for _, kv in bindingIdsSorted {kv.name}]
let bindingText = strings.Join(bindingKeys, "+")
"DefText": "\(strokeDef.DefText)"
"BindText": bindingText
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/eval_mixedfiletypes.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ checks: {
}

hasManager: {
ok: len([ for m in team if list.Contains(m, "EM") {m}]) >= 1
ok: len([for m in team if list.Contains(m, "EM") {m}]) >= 1
}
}
-- y.cue --
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/help.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Commands are defined in CUE as follows:
command: deploy: {
exec.Run
cmd: "kubectl"
args: [ "-f", "deploy" ]
args: ["-f", "deploy"]
in: json.Encode(userValue) // encode the emitted configuration.
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/cue/cmd/testdata/script/issue473.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ package x

Steps: [string]: #Step

#TerminalName: or([ for k, _ in Terminals {k}])
#TerminalName: or([for k, _ in Terminals {k}])

#Step: {
Terminal: #TerminalName
Expand All @@ -33,4 +33,4 @@ g: #Guide & {
Cmd: "ls"
}
}
}
}
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/load_pkg.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ foo: int
bar: 3
-- sort/print.cue --
files: {}
flat: [ for k, _ in files {k} ]
flat: [for k, _ in files {k}]
-- sort/root_1.cue --
files: root_1: 0
-- sort/root_2/child_1.cue --
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/merge_interaction.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"tool/cli"
)

objects: [ for x in map {x}]
objects: [for x in map {x}]

command: dump: {
cli.Print & {
Expand Down
2 changes: 1 addition & 1 deletion cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ func (f *formatter) exprRaw(expr ast.Expr, prec1, depth int) {
f.print(ws, x.Rbrace, token.RBRACE)

case *ast.ListLit:
f.print(x.Lbrack, token.LBRACK, indent)
f.print(x.Lbrack, token.LBRACK, noblank, indent)
f.walkListElems(x.Elts)
f.print(trailcomma, noblank)
f.visitComments(f.current.pos)
Expand Down
20 changes: 18 additions & 2 deletions cue/format/testdata/expressions.golden
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,10 @@ import "list"
e: [...int]
e: [...int]
e: [...int | float]
e: [ for x in someObject if x > 9 {
e: [for x in someObject if x > 9 {
x
}]
e: [ for x in someObject if x > 9 {x}]
e: [for x in someObject if x > 9 {x}]
e: [
for x in someObject
if x > 9 {x}]
Expand Down Expand Up @@ -253,4 +253,20 @@ import "list"

2,
])

m: [1, 2, 3]
m: [1, 2, 3]
m: [1, 2, 3]
m: [1, 2, 3]
m: [1, 2, 3]
m: [1, 2, 3]
m: [1, 2, 3]
m: [1, 2, 3]
m: [1, 2, 3]
m: [if true {1}, 2, 3]
n: [1]
o: [{}]
o: [{}]
o: [{}]
o: [{}]
}
20 changes: 18 additions & 2 deletions cue/format/testdata/expressions.input
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,10 @@ import "list"
e: [...int]
e: [...int,]
e: [...int | float]
e: [ for x in someObject if x > 9 {
e: [for x in someObject if x > 9 {
x
}]
e: [ for x in someObject if x > 9 {x}]
e: [for x in someObject if x > 9 {x}]
e: [
for x in someObject
if x > 9 {x}]
Expand Down Expand Up @@ -250,4 +250,20 @@ import "list"

2,
])

m: [1, 2, 3]
m: [1, 2, 3,]
m: [ 1, 2, 3, ]
m: [ 1, 2, 3,]
m: [ 1, 2, 3,]
m: [ 1, 2, 3]
m: [ 1, 2, 3,]
m: [ 1, 2, 3, ]
m: [ 1, 2, 3 ]
m: [ if true { 1 }, 2, 3]
n: [ 1]
o: [{}]
o: [ {}]
o: [{} ]
o: [ {} ]
}
4 changes: 2 additions & 2 deletions cue/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func TestParse(t *testing.T) {
"list types",
`{
a: 4*[int]
b: <=5*[ {a: 5} ]
b: <=5*[{a: 5}]
c1: [...int]
c2: [...]
c3: [1, 2, ...int,]
Expand All @@ -310,7 +310,7 @@ func TestParse(t *testing.T) {
"list comprehensions",
`{
y: [1,2,3]
b: [ for x in y if x == 1 { x } ],
b: [for x in y if x == 1 { x }],
}`,
`{y: [1, 2, 3], b: [for x in y if x==1 {x}]}`,
}, {
Expand Down
2 changes: 1 addition & 1 deletion cue/testdata/benchmarks/issue2176.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
}
}
}
[ for i, _ in unique_combinations {i}][0]
[for i, _ in unique_combinations {i}][0]
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cue/testdata/builtins/incomplete.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ list1: {

// This evaluates to a list with an incomplete element.
Top: [
[ for _, F in _Sub {F}],
[for _, F in _Sub {F}],
]

_Sub: a.b
Expand Down
6 changes: 3 additions & 3 deletions cue/testdata/compile/labels.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ ok9: [name=dis2]: string
ok10: [{foo: "bar"}]: string // disallowed in evaluator
ok11: [list.FlattenN([string], 1)]: string // disallowed in evaluator

bad1: [ for x in [1, 2, 3] {x}]: string
bad1: [for x in [1, 2, 3] {x}]: string

saneReferencesInComprehensions: {
for _ in [1] {
Expand All @@ -31,7 +31,7 @@ saneReferencesInComprehensions: {
}
-- out/compile --
bad1: comprehension values not allowed in this position:
./in.cue:24:9
./in.cue:24:8
--- in.cue
{
dis1: ("dev"|"prd")
Expand Down Expand Up @@ -91,4 +91,4 @@ bad1: comprehension values not allowed in this position:
}
-- out/eval --
bad1: comprehension values not allowed in this position:
./in.cue:24:9
./in.cue:24:8
6 changes: 3 additions & 3 deletions cue/testdata/comprehensions/015_list_comprehension.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#name: list comprehension
#evalFull
-- in.cue --
a: [ for k, v in b if k < "d" if v > b.a {k}]
a: [for k, v in b if k < "d" if v > b.a {k}]
b: {
a: 1
b: 2
c: 3
d: 4
}
c: [ for _, x in b for _, y in b if x < y {x}]
d: [ for x, _ in a {x}]
c: [for _, x in b for _, y in b if x < y {x}]
d: [for x, _ in a {x}]
-- out/def --
a: ["b", "c"]
b: {
Expand Down
12 changes: 6 additions & 6 deletions cue/testdata/comprehensions/checkdefined.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -17,31 +17,31 @@ okc1: { if xc.undefined != _|_ {a: 1} }
oko1: { if xo.undefined != _|_ {a: 1} }

okc2: {
if ({} & {s: [ for y in xc.undefined {}]}) != _|_ {a: 1}
if ({} & {s: [for y in xc.undefined {}]}) != _|_ {a: 1}
}

oko2: {
if ({} & {s: [ for y in xo.undefined {}]}) != _|_ {a: 1}
if ({} & {s: [for y in xo.undefined {}]}) != _|_ {a: 1}
}

okc3: {
if ({s: [ for y in xc.undefined {}]}) != _|_ {a: 1}
if ({s: [for y in xc.undefined {}]}) != _|_ {a: 1}
}

oko3: {
if ({s: [ for y in xo.undefined {}]}) != _|_ {a: 1}
if ({s: [for y in xo.undefined {}]}) != _|_ {a: 1}
}

issue1969: okc: {
let X = xc.undefined
let Y = {} & {s: [ for y in list.Range(0, X, 1) {}]}
let Y = {} & {s: [for y in list.Range(0, X, 1) {}]}

if Y != _|_ {Y}
}

issue1969: oko: {
let X = xo.undefined
let Y = {} & {s: [ for y in list.Range(0, X, 1) {}]}
let Y = {} & {s: [for y in list.Range(0, X, 1) {}]}

if Y != _|_ {Y}
}
Expand Down
4 changes: 2 additions & 2 deletions cue/testdata/comprehensions/incomplete.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ cond: bool
src: {}
top: _
a: [ if cond {}]
b: [ for x in src.foo {}]
b: [for x in src.foo {}]
c: {for x in top {}}
-- out/eval/stats --
Leaks: 0
Expand All @@ -27,7 +27,7 @@ Disjuncts: 7
}
b: (_|_){
// [incomplete] b: undefined field: foo:
// ./in.cue:5:19
// ./in.cue:5:18
}
c: (_|_){
// [incomplete] c: cannot range over top (incomplete type _):
Expand Down
2 changes: 1 addition & 1 deletion cue/testdata/comprehensions/lists.txtar
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
-- in.cue --
a: [{a: 1}, {b: 2 & 3}]

b: [ for x in a {x}]
b: [for x in a {x}]
-- out/eval/stats --
Leaks: 0
Freed: 7
Expand Down
6 changes: 3 additions & 3 deletions cue/testdata/cycle/chain.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ issue2052: t1: {
out: {
if (#in & #basic) != _|_ {1}
if (#in & #basic) == _|_ {
list.Max([ for k, v in #in {(#next & {#in: v}).out}]) + 1
list.Max([for k, v in #in {(#next & {#in: v}).out}]) + 1
}
}
}
Expand Down Expand Up @@ -139,7 +139,7 @@ issue2052: t2: {
out: {
if (#in & #basic) != _|_ {1}
if (#in & #basic) == _|_ {
list.Max([ for k, v in #in {(#next & {#in: v}).out}]) + 1
list.Max([for k, v in #in {(#next & {#in: v}).out}]) + 1
}
}
}
Expand Down Expand Up @@ -188,7 +188,7 @@ issue2052: full: {
// if we are not a basic type, then we are 1 + the max of children
if (#in & #basic) == _|_ {
// this is our "recursion" for each child
let depths = [ for k, v in #in {(#next & {#in: v}).out}]
let depths = [for k, v in #in {(#next & {#in: v}).out}]
list.Max(depths) + 1
}
}
Expand Down
Loading

0 comments on commit d70007c

Please sign in to comment.