Skip to content

Commit

Permalink
cue/format: indent list elements correctly
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
uhthomas authored and mvdan committed Oct 20, 2023
1 parent f892345 commit eddccfc
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 18 deletions.
10 changes: 5 additions & 5 deletions cmd/cue/cmd/testdata/script/get_go_types.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ import (
Slice1: [...int] @go(,[]int)
Slice2: [...] @go(,[]interface{})
Slice3?: null | [...] @go(,*[]json.Unmarshaler)
Array1: 5 * [int] @go(,[5]int)
Array2: 5 * [_] @go(,[5]interface{})
Array1: 5 * [int] @go(,[5]int)
Array2: 5 * [_] @go(,[5]interface{})
Array3?: null | 5*[_] @go(,*[5]json.Marshaler)
Array4: bytes @go(,[5]byte)
Intf: #Interface @protobuf(2,varint,name=intf)
Intf2: _ @go(,interface{})
Array4: bytes @go(,[5]byte)
Intf: #Interface @protobuf(2,varint,name=intf)
Intf2: _ @go(,interface{})
Intf3: {
Interface: #Interface
} @go(,struct{Interface})
Expand Down
10 changes: 10 additions & 0 deletions cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ func (f *formatter) nextNeedsFormfeed(n ast.Expr) bool {
return strings.IndexByte(x.Value, '\n') >= 0
case *ast.ListLit:
return true
case *ast.UnaryExpr:
return f.nextNeedsFormfeed(x.X)
case *ast.BinaryExpr:
return f.nextNeedsFormfeed(x.X) || f.nextNeedsFormfeed(x.Y)
case *ast.CallExpr:
for _, arg := range x.Args {
if f.nextNeedsFormfeed(arg) {
return true
}
}
}
return false
}
Expand Down
18 changes: 18 additions & 0 deletions cue/format/testdata/expressions.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package expressions

import "list"

{
a: 1 // comment
aaa: 22 // comment
Expand Down Expand Up @@ -235,4 +237,20 @@ package expressions

"contains tabs": 123
@jsonschema(foo="contains tabs")

j: cueckoo: _ | [
1,

2,
]
k: cueckoo: *[
1,

2,
]
l: cueckoo: list.Concat([
1,

2,
])
}
22 changes: 20 additions & 2 deletions cue/format/testdata/expressions.input
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package expressions

import "list"

{
a: 1 // comment
aaa: 22 // comment

"": 3

b: 3

c: b: a: 4
Expand Down Expand Up @@ -171,7 +173,7 @@ package expressions
"\(k)":v
}
}

e: { for k, v in someObject if k > "a" {"\(k)":v} }
e: { for k, v in someObject if k > "a" {
"\(k)":v }}
Expand Down Expand Up @@ -232,4 +234,20 @@ package expressions

"contains tabs": 123
@jsonschema(foo="contains tabs")

j: cueckoo: _ | [
1,

2,
]
k: cueckoo: *[
1,

2,
]
l: cueckoo: list.Concat([
1,

2,
])
}
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/list.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ import "list"

foo?: [...string]
tuple?: [string, int, 2]
has?: list.Contains(3)
has?: list.Contains(3)
size?: list.UniqueItems() & list.MaxItems(9) & [_, _, _, ...]
additional?: [int, int, ...string]
2 changes: 1 addition & 1 deletion encoding/jsonschema/testdata/type.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
-- out.cue --
// Main schema
intString?: null | bool | int | string | [...]
object?: {
object?: {
...
} | *{
foo: "bar"
Expand Down
6 changes: 3 additions & 3 deletions internal/core/export/testdata/main/adt.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ l5: [1, 3] & {
}

#foo: int
l6: [1, #foo] & {
l6: [1, #foo] & {
[1, 3]

#foo: int
Expand Down Expand Up @@ -180,7 +180,7 @@ l5: [1, 3] & {
#foo: int
}
#foo: int
l6: [1, #foo] & {
l6: [1, #foo] & {
[1, 3]
#foo: int
}
Expand All @@ -197,7 +197,7 @@ e5: e1 + e2 - e3
e6: !t
e7: !t || !false
e8?: !false
m1: {
m1: {
[string]: int & >=0
} & {
{
Expand Down
4 changes: 2 additions & 2 deletions internal/filetypes/types.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/list/testdata/list.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ unique: {
// Issue #2099
minItems: {
incomplete1: [...] & list.MinItems(1)
fail1: _|_ // minItems.fail1: invalid value [] (does not satisfy list.MinItems(1)): len(list) < MinItems(1) (0 < 1) (and 1 more errors)
fail1: _|_ // minItems.fail1: invalid value [] (does not satisfy list.MinItems(1)): len(list) < MinItems(1) (0 < 1) (and 1 more errors)
ok1: [0, ...]
ok2: [0]
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/struct/testdata/struct.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import "struct"

minFields: {
incomplete1: {} & struct.MinFields(1)
fail1: _|_ // minFields.fail1: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1)
fail1: _|_ // minFields.fail1: invalid value {} (does not satisfy struct.MinFields(1)): len(fields) < MinFields(1) (0 < 1)
ok1: {
a: 1
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/tool/exec/pkg.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit eddccfc

Please sign in to comment.