diff --git a/cue/format/format.go b/cue/format/format.go index cd1d460f5..6b38c2e6c 100644 --- a/cue/format/format.go +++ b/cue/format/format.go @@ -262,13 +262,6 @@ func (f *formatter) formfeed() whiteSpace { return formfeed } -func (f *formatter) wsOverride(def whiteSpace) whiteSpace { - if f.current.override == ignore { - return def - } - return f.current.override -} - func (f *formatter) onOneLine(node ast.Node) bool { a := node.Pos() b := node.End() diff --git a/cue/format/format_test.go b/cue/format/format_test.go index 47e738206..49c6339d6 100644 --- a/cue/format/format_test.go +++ b/cue/format/format_test.go @@ -229,7 +229,7 @@ func init() { } } -// TestNodes tests nodes that are that are invalid CUE, but are accepted by +// TestNodes tests nodes that are invalid CUE, but are accepted by // format. func TestNodes(t *testing.T) { testCases := []struct { diff --git a/cue/format/node.go b/cue/format/node.go index 1aff23749..5db122f6b 100644 --- a/cue/format/node.go +++ b/cue/format/node.go @@ -235,7 +235,7 @@ func (f *formatter) inlineField(n *ast.Field) *ast.Field { regular := internal.IsRegularField(n) // shortcut single-element structs. // If the label has a valid position, we assume that an unspecified - // Lbrace signals the intend to collapse fields. + // Lbrace signals the intent to collapse fields. if !n.Label.Pos().IsValid() && !(f.printer.cfg.simplify && regular) { return nil } @@ -304,7 +304,7 @@ func (f *formatter) decl(decl ast.Decl) { nextFF := f.nextNeedsFormfeed(n.Value) tab := vtab - if nextFF { + if nextFF || f.prevLbraceOnLine { tab = blank } @@ -645,6 +645,7 @@ func (f *formatter) exprRaw(expr ast.Expr, prec1, depth int) { ws |= newline | nooverride } f.print(x.Lbrace, token.LBRACE, &l, ws, ff, indent) + f.prevLbraceOnLine = l == f.lineout f.walkDeclList(x.Elts) f.matchUnindent() @@ -849,7 +850,7 @@ func (f *formatter) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int) { prec := x.Op.Precedence() if prec < prec1 { // parenthesis needed - // Note: The parser inserts an syntax.ParenExpr node; thus this case + // Note: The parser inserts a syntax.ParenExpr node; thus this case // can only occur if the AST is created in a different way. // defer p.pushComment(nil).pop() f.print(token.LPAREN, nooverride) diff --git a/cue/format/printer.go b/cue/format/printer.go index 3e02ca637..a9ffc0271 100644 --- a/cue/format/printer.go +++ b/cue/format/printer.go @@ -41,9 +41,10 @@ type printer struct { lastTok token.Token // last token printed (syntax.ILLEGAL if it's whitespace) - output []byte - indent int - spaceBefore bool + output []byte + indent int + spaceBefore bool + prevLbraceOnLine bool // true if a '{' has been written on the current line errs errors.Error } @@ -269,17 +270,17 @@ func (p *printer) writeWhitespace(ws whiteSpace) { case ws&newsection != 0: p.maybeIndentLine(ws) p.writeByte('\f', 2) - p.lineout += 2 + p.incrementLine(2) p.spaceBefore = true case ws&formfeed != 0: p.maybeIndentLine(ws) p.writeByte('\f', 1) - p.lineout++ + p.incrementLine(1) p.spaceBefore = true case ws&newline != 0: p.maybeIndentLine(ws) p.writeByte('\n', 1) - p.lineout++ + p.incrementLine(1) p.spaceBefore = true case ws&declcomma != 0: p.writeByte(',', 1) @@ -295,6 +296,13 @@ func (p *printer) writeWhitespace(ws whiteSpace) { } } +func (p *printer) incrementLine(n int) { + if n != 0 { + p.prevLbraceOnLine = false + } + p.lineout += line(n) +} + func (p *printer) markLineIndent(ws whiteSpace) { p.indentStack = append(p.indentStack, ws) } diff --git a/cue/format/testdata/expressions.golden b/cue/format/testdata/expressions.golden index a485f6bb6..e9199ff19 100644 --- a/cue/format/testdata/expressions.golden +++ b/cue/format/testdata/expressions.golden @@ -270,12 +270,12 @@ import "list" o: [{}] o: [{}] - p: 1 + p: 1 p: p & {p: 2} - q: 1 + q: 1 q: q | {q: 2} - r: 1 + r: 1 r: b & [1, 2, {a: 4}] s: [string]: [string]: a - s: [string]: {s: string} + s: [string]: {s: string} } diff --git a/cue/token/position.go b/cue/token/position.go index 4f52c9493..2643763c4 100644 --- a/cue/token/position.go +++ b/cue/token/position.go @@ -149,7 +149,7 @@ func (p RelPos) Pos() Pos { return Pos{nil, int(p)} } -// HasRelPos repors whether p has a relative position. +// HasRelPos reports whether p has a relative position. func (p Pos) HasRelPos() bool { return p.offset&relMask != 0 diff --git a/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/github.com/golang/protobuf/protoc-gen-go/descriptor/descriptor_proto_gen.cue b/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/github.com/golang/protobuf/protoc-gen-go/descriptor/descriptor_proto_gen.cue index 2da6ce1b3..0caa0cc98 100644 --- a/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/github.com/golang/protobuf/protoc-gen-go/descriptor/descriptor_proto_gen.cue +++ b/encoding/protobuf/testdata/istio.io/api/cue.mod/gen/github.com/golang/protobuf/protoc-gen-go/descriptor/descriptor_proto_gen.cue @@ -153,17 +153,17 @@ package descriptor // 0 is reserved for errors. // Order is weird for historical reasons. "TYPE_DOUBLE" - #enumValue: 1 + #enumValue: 1 } | {"TYPE_FLOAT", #enumValue: 2} | { // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT64 if // negative values are likely. "TYPE_INT64" - #enumValue: 3 + #enumValue: 3 } | {"TYPE_UINT64", #enumValue: 4} | { // Not ZigZag encoded. Negative numbers take 10 bytes. Use TYPE_SINT32 if // negative values are likely. "TYPE_INT32" - #enumValue: 5 + #enumValue: 5 } | {"TYPE_FIXED64", #enumValue: 6} | {"TYPE_FIXED32", #enumValue: 7} | {"TYPE_BOOL", #enumValue: 8} | @@ -180,7 +180,7 @@ package descriptor } | { // New in version 2. "TYPE_BYTES" - #enumValue: 12 + #enumValue: 12 } | {"TYPE_UINT32", #enumValue: 13} | {"TYPE_ENUM", #enumValue: 14} | {"TYPE_SFIXED32", #enumValue: 15} | @@ -216,7 +216,7 @@ package descriptor #Label: { // 0 is reserved for errors "LABEL_OPTIONAL" - #enumValue: 1 + #enumValue: 1 } | {"LABEL_REQUIRED", #enumValue: 2} | {"LABEL_REPEATED", #enumValue: 3} @@ -518,7 +518,7 @@ package descriptor #CType: { // Default mode. "STRING" - #enumValue: 0 + #enumValue: 0 } | {"CORD", #enumValue: 1} | {"STRING_PIECE", #enumValue: 2}