cue/format: add form feed before composit fields

A field followed by a field with a list or struct could
result in bad indentation, as a one-liner might still
have tabs in subfields. Adding a form feed to the
field before forces these to be ignored.

Change-Id: Ie3314d7e1b9e7e289e56ce7311c1011d1bcbc142
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/2172
Reviewed-by: Marcel van Lohuizen <mpvl@google.com>
diff --git a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue
index cd9b143..1d2ad81 100644
--- a/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue
+++ b/cmd/cue/cmd/testdata/pkg/cuelang.org/go/cmd/cue/cmd/testdata/code/go/pkg1/file1_go_gen.cue
@@ -21,8 +21,8 @@
 	bar?:       int               @go(Bar)
 
 	// Time is mapped to CUE's internal type.
-	Time:      time.Time
-	Barzer:    p2.Barzer
+	Time:   time.Time
+	Barzer: p2.Barzer
 	Map: {<_>: null | CustomJSON} @go(,map[string]*CustomJSON)
 	Slice1: [...int] @go(,[]int)
 	Slice2: [...] @go(,[]interface{})
diff --git a/cue/format/node.go b/cue/format/node.go
index 97850f2..0b19a33 100644
--- a/cue/format/node.go
+++ b/cue/format/node.go
@@ -59,6 +59,24 @@
 			f.print(declcomma)
 		}
 		f.decl(x)
+		if j := i + 1; j < len(list) {
+			switch x := list[j].(type) {
+			case *ast.Field:
+				switch x := x.Value.(type) {
+				case *ast.StructLit:
+					// TODO: not entirely correct: could have multiple elements,
+					// not have a valid Lbrace, and be marked multiline. This
+					// cannot occur for ASTs resulting from a parse, though.
+					if x.Lbrace.IsValid() || len(x.Elts) != 1 {
+						f.print(f.formfeed())
+						continue
+					}
+				case *ast.ListLit:
+					f.print(f.formfeed())
+					continue
+				}
+			}
+		}
 		f.print(f.current.parentSep)
 	}
 	f.after(nil)
diff --git a/cue/format/testdata/expressions.golden b/cue/format/testdata/expressions.golden
index 47cf2c1..98a8f9e 100644
--- a/cue/format/testdata/expressions.golden
+++ b/cue/format/testdata/expressions.golden
@@ -164,4 +164,9 @@
 	e: e.f(1, 2)
 
 	e: (3 + 4)
+
+	// field before list
+	f: 3
+	a: [1, 2, // add comma
+	]
 }
diff --git a/cue/format/testdata/expressions.input b/cue/format/testdata/expressions.input
index e613f06..ccd38bb 100644
--- a/cue/format/testdata/expressions.input
+++ b/cue/format/testdata/expressions.input
@@ -164,4 +164,9 @@
     e: e.f(1, 2)
 
     e: ((3 + 4))
+
+    // field before list
+    f: 3
+    a: [1, 2 // add comma
+    ]
 }
\ No newline at end of file
diff --git a/internal/protobuf/testdata/gateway.proto.out.cue b/internal/protobuf/testdata/gateway.proto.out.cue
index dc17a26..d85721e 100644
--- a/internal/protobuf/testdata/gateway.proto.out.cue
+++ b/internal/protobuf/testdata/gateway.proto.out.cue
@@ -213,7 +213,7 @@
 	//  label search is restricted to the configuration namespace in which the
 	//  the resource is present. In other words, the Gateway resource must
 	//  reside in the same namespace as the gateway workload instance.
-	selector <_>:     string
+	selector <_>: string
 	selector? <name>: name
 }