Skip to content

Commit

Permalink
cue/format: prevent vertical tabs from causing extra indentation
Browse files Browse the repository at this point in the history
nextNeedsFormfeed wasn't aware of parentheses, indexes, and selectors,
meaning that if they were used on a multi-line and indented expression,
we would not notice we needed to skip the vertical tab.

In many cases the vertical tab was harmless, but in this case,
it caused tabwriter to further indent the inner list elements.

Updates #2496.

Signed-off-by: Daniel Martí <mvdan@mvdan.cc>
Change-Id: Iba21684c6e7f794e4e2d5ff007b9d2ba8d75d491
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1196135
TryBot-Result: CUEcueckoo <cueckoo@cuelang.org>
Reviewed-by: Aram Hăvărneanu <aram@cue.works>
  • Loading branch information
mvdan committed Jun 14, 2024
1 parent d70f9d7 commit 1f197eb
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
6 changes: 6 additions & 0 deletions cue/format/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,10 +462,16 @@ func (f *formatter) nextNeedsFormfeed(n ast.Expr) bool {
return strings.IndexByte(x.Value, '\n') >= 0
case *ast.ListLit:
return true
case *ast.ParenExpr:
return f.nextNeedsFormfeed(x.X)
case *ast.UnaryExpr:
return f.nextNeedsFormfeed(x.X)
case *ast.BinaryExpr:
return f.nextNeedsFormfeed(x.X) || f.nextNeedsFormfeed(x.Y)
case *ast.IndexExpr:
return f.nextNeedsFormfeed(x.X)
case *ast.SelectorExpr:
return f.nextNeedsFormfeed(x.X)
case *ast.CallExpr:
for _, arg := range x.Args {
if f.nextNeedsFormfeed(arg) {
Expand Down
12 changes: 5 additions & 7 deletions cue/format/testdata/expressions.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,6 @@ import "list"
s: [string]: {s: string}
}
-- issue2496.input --
// TODO(mvdan): we indent the list elements too much.
machine_type: [
if arch == "amd" {"n2-standard-2"},
if arch == "arm" {"t2a-standard-2"},
Expand All @@ -589,19 +588,18 @@ _foo: {
// skip_create_image: true
}
-- issue2496.golden --
// TODO(mvdan): we indent the list elements too much.
machine_type: [
if arch == "amd" {"n2-standard-2"},
if arch == "arm" {"t2a-standard-2"},
"unknown arch",
if arch == "amd" {"n2-standard-2"},
if arch == "arm" {"t2a-standard-2"},
"unknown arch",
][0]

long_field_name: ([
"foo",
"foo",
])

long_field_name: [
"foo",
"foo",
].bar

// TODO(mvdan): we insert an empty line between the definitions.
Expand Down
4 changes: 2 additions & 2 deletions tools/flow/testdata/dep.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ graph TD

-- out/run/t3/value --
{
x: [0, 1][INDEX]
x: [0, 1][INDEX]
$id: "valToOut"

//cue:path: root.concreteValueInGeneratedSubfield.index
Expand Down Expand Up @@ -184,7 +184,7 @@ graph TD
-- out/run/t4/value --
{
$after: {
x: [0, 1][INDEX]
x: [0, 1][INDEX]
$id: "valToOut"
}
$id: "valToOut"
Expand Down

0 comments on commit 1f197eb

Please sign in to comment.