]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: make empty lines break table alignment
authorDaniel Martí <mvdan@mvdan.cc>
Fri, 20 Jul 2018 20:55:07 +0000 (21:55 +0100)
committerRobert Griesemer <gri@golang.org>
Thu, 9 Aug 2018 18:17:00 +0000 (18:17 +0000)
In previous versions of Go including 1.10, an empty line would break the
alignment of elements within an expression list.

golang.org/cl/104755 changed the heuristic, with the side effect that
empty lines no longer broke the table alignment.

Reintroduce the behavior and add a regression test for it.

Fixes #26352.

Change-Id: I410bcff4cba25c7f8497d46bd7890a2c7ee11d46
Reviewed-on: https://go-review.googlesource.com/125260
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/go/printer/nodes.go
src/go/printer/testdata/alignment.golden
src/go/printer/testdata/alignment.input

index 18f2371d24b545b0f582ac2548fb5d9fc3aa5481..3723f30e56cf410f23246342878103c8ec0a37fd 100644 (file)
@@ -221,13 +221,22 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp
                // If the previous line and the current line had single-
                // line-expressions and the key sizes are small or the
                // ratio between the current key and the geometric mean
-               // if the previous key sizes does not exceed a threshold,
-               // align columns and do not use formfeed.
+               // does not exceed a threshold, align columns and do not use
+               // formfeed.
+               // If the previous line was an empty line, break the alignment.
+               // (The text/tabwriter will break alignment after an empty line
+               // even if we don't do anything here, but we can't see that; yet
+               // we need to reset the variables used in the geomean
+               // computation after an alignment break. Do it explicitly
+               // instead so we're aware of the break. Was issue #26352.)
                if prevSize > 0 && size > 0 {
                        const smallSize = 40
-                       if count == 0 || prevSize <= smallSize && size <= smallSize {
+                       switch {
+                       case prevLine+1 < line:
+                               useFF = true
+                       case count == 0, prevSize <= smallSize && size <= smallSize:
                                useFF = false
-                       } else {
+                       default:
                                const r = 2.5                               // threshold
                                geomean := math.Exp(lnsum / float64(count)) // count > 0
                                ratio := float64(size) / geomean
index c65defe6aec9e640a3c9607581f967f0d24a4511..302b32e7667a7ba9e04a177d65e959777bd2a559 100644 (file)
@@ -128,3 +128,12 @@ func main() {
                abcdefghijklmnopqrstuvwxyz:             "foo",
        }
 }
+
+// ----------------------------------------------------------------------------
+// Examples from issue #26352.
+var _ = map[int]string{
+       1:      "",
+
+       12345678901234567890123456789:          "",
+       12345678901234567890123456789012345678: "",
+}
index 9b0aae6bec1e6b5b8f245da97f63438e6dec0e7a..83361cc7c168a20d019e312605dbfecd6066ac36 100644 (file)
@@ -128,3 +128,12 @@ func main() {
                abcdefghijklmnopqrstuvwxyz: "foo",
        }
 }
+
+// ----------------------------------------------------------------------------
+// Examples from issue #26352.
+var _ = map[int]string{
+       1: "",
+
+       12345678901234567890123456789: "",
+       12345678901234567890123456789012345678: "",
+}