Skip to content

Commit eddccfc

Browse files
uhthomasmvdan
authored andcommitted
cue/format: indent list elements correctly
Vertical tabs are used when printing some struct fields for the sake of vertical alignment of the values. This does not happen for fields where the value needs a form feed, for the sake of not aligning every single struct field. For example, we don't align those with struct and list values since they tend to be complex or take multiple lines. The logic to detect whether a field value needs a form feed was brittle since it only looked for a struct or list directly in the expression, entirely missing one if it was behind a unary or binary expression. Fix up that logic and update the tests. Fixes #2314. Closes #2645 as merged as of commit cff4c1a. Signed-off-by: Thomas Way <[email protected]> Change-Id: Ie543e41ebf15d724632bc9a0dd1d9df28032ddc4 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1170966 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]> Reviewed-by: Paul Jolly <[email protected]>
1 parent f892345 commit eddccfc

File tree

11 files changed

+64
-18
lines changed

11 files changed

+64
-18
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -266,12 +266,12 @@ import (
266266
Slice1: [...int] @go(,[]int)
267267
Slice2: [...] @go(,[]interface{})
268268
Slice3?: null | [...] @go(,*[]json.Unmarshaler)
269-
Array1: 5 * [int] @go(,[5]int)
270-
Array2: 5 * [_] @go(,[5]interface{})
269+
Array1: 5 * [int] @go(,[5]int)
270+
Array2: 5 * [_] @go(,[5]interface{})
271271
Array3?: null | 5*[_] @go(,*[5]json.Marshaler)
272-
Array4: bytes @go(,[5]byte)
273-
Intf: #Interface @protobuf(2,varint,name=intf)
274-
Intf2: _ @go(,interface{})
272+
Array4: bytes @go(,[5]byte)
273+
Intf: #Interface @protobuf(2,varint,name=intf)
274+
Intf2: _ @go(,interface{})
275275
Intf3: {
276276
Interface: #Interface
277277
} @go(,struct{Interface})

cue/format/node.go

+10
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,16 @@ func (f *formatter) nextNeedsFormfeed(n ast.Expr) bool {
431431
return strings.IndexByte(x.Value, '\n') >= 0
432432
case *ast.ListLit:
433433
return true
434+
case *ast.UnaryExpr:
435+
return f.nextNeedsFormfeed(x.X)
436+
case *ast.BinaryExpr:
437+
return f.nextNeedsFormfeed(x.X) || f.nextNeedsFormfeed(x.Y)
438+
case *ast.CallExpr:
439+
for _, arg := range x.Args {
440+
if f.nextNeedsFormfeed(arg) {
441+
return true
442+
}
443+
}
434444
}
435445
return false
436446
}

cue/format/testdata/expressions.golden

+18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package expressions
22

3+
import "list"
4+
35
{
46
a: 1 // comment
57
aaa: 22 // comment
@@ -235,4 +237,20 @@ package expressions
235237

236238
"contains tabs": 123
237239
@jsonschema(foo="contains tabs")
240+
241+
j: cueckoo: _ | [
242+
1,
243+
244+
2,
245+
]
246+
k: cueckoo: *[
247+
1,
248+
249+
2,
250+
]
251+
l: cueckoo: list.Concat([
252+
1,
253+
254+
2,
255+
])
238256
}

cue/format/testdata/expressions.input

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package expressions
22

3+
import "list"
4+
35
{
46
a: 1 // comment
57
aaa: 22 // comment
68

79
"": 3
8-
10+
911
b: 3
1012

1113
c: b: a: 4
@@ -171,7 +173,7 @@ package expressions
171173
"\(k)":v
172174
}
173175
}
174-
176+
175177
e: { for k, v in someObject if k > "a" {"\(k)":v} }
176178
e: { for k, v in someObject if k > "a" {
177179
"\(k)":v }}
@@ -232,4 +234,20 @@ package expressions
232234

233235
"contains tabs": 123
234236
@jsonschema(foo="contains tabs")
237+
238+
j: cueckoo: _ | [
239+
1,
240+
241+
2,
242+
]
243+
k: cueckoo: *[
244+
1,
245+
246+
2,
247+
]
248+
l: cueckoo: list.Concat([
249+
1,
250+
251+
2,
252+
])
235253
}

encoding/jsonschema/testdata/list.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ import "list"
4040

4141
foo?: [...string]
4242
tuple?: [string, int, 2]
43-
has?: list.Contains(3)
43+
has?: list.Contains(3)
4444
size?: list.UniqueItems() & list.MaxItems(9) & [_, _, _, ...]
4545
additional?: [int, int, ...string]

encoding/jsonschema/testdata/type.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
-- out.cue --
3333
// Main schema
3434
intString?: null | bool | int | string | [...]
35-
object?: {
35+
object?: {
3636
...
3737
} | *{
3838
foo: "bar"

internal/core/export/testdata/main/adt.txtar

+3-3
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ l5: [1, 3] & {
4444
}
4545

4646
#foo: int
47-
l6: [1, #foo] & {
47+
l6: [1, #foo] & {
4848
[1, 3]
4949

5050
#foo: int
@@ -180,7 +180,7 @@ l5: [1, 3] & {
180180
#foo: int
181181
}
182182
#foo: int
183-
l6: [1, #foo] & {
183+
l6: [1, #foo] & {
184184
[1, 3]
185185
#foo: int
186186
}
@@ -197,7 +197,7 @@ e5: e1 + e2 - e3
197197
e6: !t
198198
e7: !t || !false
199199
e8?: !false
200-
m1: {
200+
m1: {
201201
[string]: int & >=0
202202
} & {
203203
{

internal/filetypes/types.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/list/testdata/list.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ unique: {
168168
// Issue #2099
169169
minItems: {
170170
incomplete1: [...] & list.MinItems(1)
171-
fail1: _|_ // minItems.fail1: invalid value [] (does not satisfy list.MinItems(1)): len(list) < MinItems(1) (0 < 1) (and 1 more errors)
171+
fail1: _|_ // minItems.fail1: invalid value [] (does not satisfy list.MinItems(1)): len(list) < MinItems(1) (0 < 1) (and 1 more errors)
172172
ok1: [0, ...]
173173
ok2: [0]
174174
}

pkg/struct/testdata/struct.txtar

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import "struct"
3636

3737
minFields: {
3838
incomplete1: {} & struct.MinFields(1)
39-
fail1: _|_ // minFields.fail1: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1)
39+
fail1: _|_ // minFields.fail1: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1)
4040
ok1: {
4141
a: 1
4242
}

pkg/tool/exec/pkg.go

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)