summaryrefslogtreecommitdiff
path: root/src/go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2014-09-11 12:57:51 -0700
committerRobert Griesemer <gri@golang.org>2014-09-11 12:57:51 -0700
commitc91c503389069f2d80bccd7d3f58d0bc0cfaf7cd (patch)
tree7645d147668db1096dda8664b5c976c294358783 /src/go
parentbc655bc6a455958accbfe1003e0cf7f8238b130c (diff)
downloadgo-c91c503389069f2d80bccd7d3f58d0bc0cfaf7cd.tar.gz
go/printer, gofmt: don't align map entries for irregular inputs
Details: Until now, when we saw a key:value pair that fit onto a single line, we assumed that it should be formatted with a vtab after the ':' for alignment of its value. This leads to odd behavior if there are more than one such pair on a line. This CL changes the behavior such that alignment is only used for the first pair on a line. This preserves existing behavior (in the std lib we have composite literals where the last line contains multiple entries and the first entry's value is aligned with the values on previous lines), and resolves this issue. No impact on formatting of std lib, go.tools, go.exp, go.net. Fixes issue 8685. LGTM=adonovan R=adonovan CC=golang-codereviews https://codereview.appspot.com/139430043
Diffstat (limited to 'src/go')
-rw-r--r--src/go/printer/nodes.go16
-rw-r--r--src/go/printer/testdata/declarations.golden25
-rw-r--r--src/go/printer/testdata/declarations.input25
3 files changed, 59 insertions, 7 deletions
diff --git a/src/go/printer/nodes.go b/src/go/printer/nodes.go
index 6e26f9a63..e52236ddc 100644
--- a/src/go/printer/nodes.go
+++ b/src/go/printer/nodes.go
@@ -163,8 +163,8 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp
size := 0
// print all list elements
+ prevLine := prev.Line
for i, x := range list {
- prevLine := line
line = p.lineFor(x.Pos())
// determine if the next linebreak, if any, needs to use formfeed:
@@ -207,8 +207,8 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp
}
}
+ needsLinebreak := 0 < prevLine && prevLine < line
if i > 0 {
- needsLinebreak := prevLine < line && prevLine > 0 && line > 0
// use position of expression following the comma as
// comma position for correct comment placement, but
// only if the expression is on the same line
@@ -232,16 +232,20 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp
}
}
- if isPair && size > 0 && len(list) > 1 {
- // we have a key:value expression that fits onto one line and
- // is in a list with more then one entry: use a column for the
- // key such that consecutive entries can align if possible
+ if len(list) > 1 && isPair && size > 0 && needsLinebreak {
+ // we have a key:value expression that fits onto one line
+ // and it's not on the same line as the prior expression:
+ // use a column for the key such that consecutive entries
+ // can align if possible
+ // (needsLinebreak is set if we started a new line before)
p.expr(pair.Key)
p.print(pair.Colon, token.COLON, vtab)
p.expr(pair.Value)
} else {
p.expr0(x, depth)
}
+
+ prevLine = line
}
if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line {
diff --git a/src/go/printer/testdata/declarations.golden b/src/go/printer/testdata/declarations.golden
index a27f21fc8..9acd41b7d 100644
--- a/src/go/printer/testdata/declarations.golden
+++ b/src/go/printer/testdata/declarations.golden
@@ -593,7 +593,7 @@ var (
)
func _() {
- var privateKey2 = &Block{Type: "RSA PRIVATE KEY",
+ var privateKey2 = &Block{Type: "RSA PRIVATE KEY",
Headers: map[string]string{},
Bytes: []uint8{0x30, 0x82, 0x1, 0x3a, 0x2, 0x1, 0x0, 0x2,
0x41, 0x0, 0xb2, 0x99, 0xf, 0x49, 0xc4, 0x7d, 0xfa, 0x8c,
@@ -698,6 +698,29 @@ var _ = T4{
c: z,
}
+// no alignment of map composite entries if they are not the first entry on a line
+var _ = T{0: 0} // not aligned
+var _ = T{0: 0, // not aligned
+ 1: 1, // aligned
+ 22: 22, // aligned
+ 333: 333, 1234: 12, 12345: 0, // first on line aligned
+}
+
+// test cases form issue 8685
+// not aligned
+var _ = map[int]string{1: "spring", 2: "summer",
+ 3: "autumn", 4: "winter"}
+
+// not aligned
+var _ = map[string]string{"a": "spring", "b": "summer",
+ "c": "autumn", "d": "winter"}
+
+// aligned
+var _ = map[string]string{"a": "spring",
+ "b": "summer",
+ "c": "autumn",
+ "d": "winter"}
+
func _() {
var _ = T{
a, // must introduce trailing comma
diff --git a/src/go/printer/testdata/declarations.input b/src/go/printer/testdata/declarations.input
index d9951d386..45beec25f 100644
--- a/src/go/printer/testdata/declarations.input
+++ b/src/go/printer/testdata/declarations.input
@@ -715,6 +715,31 @@ var _ = T4{
}
+// no alignment of map composite entries if they are not the first entry on a line
+var _ = T{0: 0} // not aligned
+var _ = T{0: 0, // not aligned
+ 1: 1, // aligned
+ 22: 22, // aligned
+ 333: 333, 1234: 12, 12345: 0, // first on line aligned
+}
+
+
+// test cases form issue 8685
+// not aligned
+var _ = map[int]string{1: "spring", 2: "summer",
+ 3: "autumn", 4: "winter"}
+
+// not aligned
+var _ = map[string]string{"a": "spring", "b": "summer",
+ "c": "autumn", "d": "winter"}
+
+// aligned
+var _ = map[string]string{"a": "spring",
+"b": "summer",
+ "c": "autumn",
+"d": "winter"}
+
+
func _() {
var _ = T{
a, // must introduce trailing comma