aboutsummaryrefslogtreecommitdiff
path: root/src/cmd
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2018-04-03 17:05:47 -0700
committerRobert Griesemer <gri@golang.org>2018-04-04 13:39:34 -0700
commit542ea5ad91367d2b40589942cc5757a4d5f43f97 (patch)
tree545359858202d1c339aa91d4032132c743f5705f /src/cmd
parentd7c7d88b2c991c744f3cc1eb5865218f52ef98b0 (diff)
downloadgo-542ea5ad91367d2b40589942cc5757a4d5f43f97.tar.xz
go/printer, gofmt: tuned table alignment for better results
The go/printer (and thus gofmt) uses a heuristic to determine whether to break alignment between elements of an expression list which is spread across multiple lines. The heuristic only kicked in if the entry sizes (character length) was above a certain threshold (20) and the ratio between the previous and current entry size was above a certain value (4). This heuristic worked reasonably most of the time, but also led to unfortunate breaks in many cases where a single entry was suddenly much smaller (or larger) then the previous one. The behavior of gofmt was sufficiently mysterious in some of these situations that many issues were filed against it. The simplest solution to address this problem is to remove the heuristic altogether and have a programmer introduce empty lines to force different alignments if it improves readability. The problem with that approach is that the places where it really matters, very long tables with many (hundreds, or more) entries, may be machine-generated and not "post-processed" by a human (e.g., unicode/utf8/tables.go). If a single one of those entries is overlong, the result would be that the alignment would force all comments or values in key:value pairs to be adjusted to that overlong value, making the table hard to read (e.g., that entry may not even be visible on screen and all other entries seem spaced out too wide). Instead, we opted for a slightly improved heuristic that behaves much better for "normal", human-written code. 1) The threshold is increased from 20 to 40. This disables the heuristic for many common cases yet even if the alignment is not "ideal", 40 is not that many characters per line with todays screens, making it very likely that the entire line remains "visible" in an editor. 2) Changed the heuristic to not simply look at the size ratio between current and previous line, but instead considering the geometric mean of the sizes of the previous (aligned) lines. This emphasizes the "overall picture" of the previous lines, rather than a single one (which might be an outlier). 3) Changed the ratio from 4 to 2.5. Now that we ignore sizes below 40, a ratio of 4 would mean that a new entry would have to be 4 times bigger (160) or smaller (10) before alignment would be broken. A ratio of 2.5 seems more sensible. Applied updated gofmt to all of src and misc. Also tested against several former issues that complained about this and verified that the output for the given examples is satisfactory (added respective test cases). Some of the files changed because they were not gofmt-ed in the first place. For #644. For #7335. For #10392. (and probably more related issues) Fixes #22852. Change-Id: I5e48b3d3b157a5cf2d649833b7297b33f43a6f6e
Diffstat (limited to 'src/cmd')
-rw-r--r--src/cmd/doc/doc_test.go2
-rw-r--r--src/cmd/go/testdata/src/dupload/p2/p2.go1
-rw-r--r--src/cmd/go/testdata/src/exclude/x_linux.go1
-rw-r--r--src/cmd/go/testdata/src/my.pkg/main/main.go2
-rw-r--r--src/cmd/go/testdata/src/vetcycle/p.go7
-rw-r--r--src/cmd/internal/obj/arm64/a.out.go2
-rw-r--r--src/cmd/internal/obj/arm64/asm7.go14
-rw-r--r--src/cmd/pprof/pprof.go4
-rw-r--r--src/cmd/vendor/github.com/google/pprof/internal/driver/fetch.go4
-rw-r--r--src/cmd/vet/testdata/composite.go2
10 files changed, 21 insertions, 18 deletions
diff --git a/src/cmd/doc/doc_test.go b/src/cmd/doc/doc_test.go
index c60e93743f..e68fb017b9 100644
--- a/src/cmd/doc/doc_test.go
+++ b/src/cmd/doc/doc_test.go
@@ -115,7 +115,7 @@ var tests = []test{
`var MultiLineVar = map\[struct{ ... }\]struct{ ... }{ ... }`, // Multi line variable.
`func MultiLineFunc\(x interface{ ... }\) \(r struct{ ... }\)`, // Multi line function.
`var LongLine = newLongLine\(("someArgument[1-4]", ){4}...\)`, // Long list of arguments.
- `type T1 = T2`, // Type alias
+ `type T1 = T2`, // Type alias
},
[]string{
`const internalConstant = 2`, // No internal constants.
diff --git a/src/cmd/go/testdata/src/dupload/p2/p2.go b/src/cmd/go/testdata/src/dupload/p2/p2.go
index 40f5a5b07c..8a80979b4e 100644
--- a/src/cmd/go/testdata/src/dupload/p2/p2.go
+++ b/src/cmd/go/testdata/src/dupload/p2/p2.go
@@ -1,2 +1,3 @@
package p2
+
import _ "dupload/vendor/p"
diff --git a/src/cmd/go/testdata/src/exclude/x_linux.go b/src/cmd/go/testdata/src/exclude/x_linux.go
index a5bbb61b14..41ef6e5d7b 100644
--- a/src/cmd/go/testdata/src/exclude/x_linux.go
+++ b/src/cmd/go/testdata/src/exclude/x_linux.go
@@ -1,4 +1,3 @@
// +build windows
package x
-
diff --git a/src/cmd/go/testdata/src/my.pkg/main/main.go b/src/cmd/go/testdata/src/my.pkg/main/main.go
index 397e8b66a2..c3e8de1276 100644
--- a/src/cmd/go/testdata/src/my.pkg/main/main.go
+++ b/src/cmd/go/testdata/src/my.pkg/main/main.go
@@ -1,5 +1,7 @@
package main
+
import "my.pkg"
+
func main() {
println(pkg.Text)
}
diff --git a/src/cmd/go/testdata/src/vetcycle/p.go b/src/cmd/go/testdata/src/vetcycle/p.go
index 857c3a611f..5b058e7806 100644
--- a/src/cmd/go/testdata/src/vetcycle/p.go
+++ b/src/cmd/go/testdata/src/vetcycle/p.go
@@ -1,11 +1,12 @@
package p
-
type (
- _ interface{ m(B1) }
+ _ interface{ m(B1) }
A1 interface{ a(D1) }
B1 interface{ A1 }
- C1 interface{ B1 /* ERROR issue #18395 */ }
+ C1 interface {
+ B1 /* ERROR issue #18395 */
+ }
D1 interface{ C1 }
)
diff --git a/src/cmd/internal/obj/arm64/a.out.go b/src/cmd/internal/obj/arm64/a.out.go
index 473ce08fe3..1b5d93d52a 100644
--- a/src/cmd/internal/obj/arm64/a.out.go
+++ b/src/cmd/internal/obj/arm64/a.out.go
@@ -911,4 +911,4 @@ const (
ARNG_H
ARNG_S
ARNG_D
-) \ No newline at end of file
+)
diff --git a/src/cmd/internal/obj/arm64/asm7.go b/src/cmd/internal/obj/arm64/asm7.go
index a719bd0a74..98d6141632 100644
--- a/src/cmd/internal/obj/arm64/asm7.go
+++ b/src/cmd/internal/obj/arm64/asm7.go
@@ -1668,13 +1668,13 @@ func cmp(a int, b int) bool {
case C_PSOREG_4:
switch b {
- case C_ZOREG, C_PSOREG_8:
+ case C_ZOREG, C_PSOREG_8:
return true
}
case C_PSOREG:
switch b {
- case C_ZOREG, C_PSOREG_8, C_PSOREG_4:
+ case C_ZOREG, C_PSOREG_8, C_PSOREG_4:
return true
}
@@ -4014,7 +4014,7 @@ func (c *ctxt7) asmout(p *obj.Prog, o *Optab, out []uint32) {
rt := int((p.To.Reg) & 31)
r := int((p.Reg) & 31)
- o1 |= ((Q&1) << 30) | ((size&3) << 22) | (uint32(rf&31) << 16) | (uint32(r&31) << 5) | uint32(rt&31)
+ o1 |= ((Q & 1) << 30) | ((size & 3) << 22) | (uint32(rf&31) << 16) | (uint32(r&31) << 5) | uint32(rt&31)
case 94: /* vext $imm4, Vm.<T>, Vn.<T>, Vd.<T> */
if p.From3Type() != obj.TYPE_REG {
@@ -4053,7 +4053,7 @@ func (c *ctxt7) asmout(p *obj.Prog, o *Optab, out []uint32) {
rt := int((p.To.Reg) & 31)
r := int((p.Reg) & 31)
- o1 |= ((Q&1) << 30) | (uint32(r&31) << 16) | (uint32(index&15) << 11) | (uint32(rf&31) << 5) | uint32(rt&31)
+ o1 |= ((Q & 1) << 30) | (uint32(r&31) << 16) | (uint32(index&15) << 11) | (uint32(rf&31) << 5) | uint32(rt&31)
case 95: /* vushr $shift, Vn.<T>, Vd.<T> */
at := int((p.To.Reg >> 5) & 15)
@@ -4111,7 +4111,7 @@ func (c *ctxt7) asmout(p *obj.Prog, o *Optab, out []uint32) {
rt := int((p.To.Reg) & 31)
rf := int((p.Reg) & 31)
- o1 |= ((Q&1) << 30) | (uint32(imm&127) << 16) | (uint32(rf&31) << 5) | uint32(rt&31)
+ o1 |= ((Q & 1) << 30) | (uint32(imm&127) << 16) | (uint32(rf&31) << 5) | uint32(rt&31)
case 96: /* vst1 Vt1.<T>[index], offset(Rn) */
af := int((p.From.Reg >> 5) & 15)
@@ -4183,7 +4183,7 @@ func (c *ctxt7) asmout(p *obj.Prog, o *Optab, out []uint32) {
o1 |= 26 << 23
}
- o1 |= (uint32(Q&1) << 30) | (uint32(r&31) << 16) | ((opcode&7) << 13) | (uint32(S&1) << 12) | (uint32(size&3) << 10) | (uint32(rf&31) << 5) | uint32(rt&31)
+ o1 |= (uint32(Q&1) << 30) | (uint32(r&31) << 16) | ((opcode & 7) << 13) | (uint32(S&1) << 12) | (uint32(size&3) << 10) | (uint32(rf&31) << 5) | uint32(rt&31)
case 97: /* vld1 offset(Rn), vt.<T>[index] */
at := int((p.To.Reg >> 5) & 15)
@@ -4257,7 +4257,7 @@ func (c *ctxt7) asmout(p *obj.Prog, o *Optab, out []uint32) {
o1 |= 106 << 21
}
- o1 |= (uint32(Q&1) << 30) | (uint32(r&31) << 16) | ((opcode&7) << 13) | (uint32(S&1) << 12) | (uint32(size&3) << 10) | (uint32(rf&31) << 5) | uint32(rt&31)
+ o1 |= (uint32(Q&1) << 30) | (uint32(r&31) << 16) | ((opcode & 7) << 13) | (uint32(S&1) << 12) | (uint32(size&3) << 10) | (uint32(rf&31) << 5) | uint32(rt&31)
}
out[0] = o1
out[1] = o2
diff --git a/src/cmd/pprof/pprof.go b/src/cmd/pprof/pprof.go
index 2f3268cbc4..1c325b5329 100644
--- a/src/cmd/pprof/pprof.go
+++ b/src/cmd/pprof/pprof.go
@@ -75,8 +75,8 @@ func getProfile(source string, timeout time.Duration) (*profile.Profile, error)
client := &http.Client{
Transport: &http.Transport{
ResponseHeaderTimeout: timeout + 5*time.Second,
- Proxy: http.ProxyFromEnvironment,
- TLSClientConfig: tlsConfig,
+ Proxy: http.ProxyFromEnvironment,
+ TLSClientConfig: tlsConfig,
},
}
resp, err := client.Get(source)
diff --git a/src/cmd/vendor/github.com/google/pprof/internal/driver/fetch.go b/src/cmd/vendor/github.com/google/pprof/internal/driver/fetch.go
index 2b1d90dafd..a929b0f790 100644
--- a/src/cmd/vendor/github.com/google/pprof/internal/driver/fetch.go
+++ b/src/cmd/vendor/github.com/google/pprof/internal/driver/fetch.go
@@ -598,8 +598,8 @@ var httpGet = func(source string, timeout time.Duration) (*http.Response, error)
client := &http.Client{
Transport: &http.Transport{
ResponseHeaderTimeout: timeout + 5*time.Second,
- Proxy: http.ProxyFromEnvironment,
- TLSClientConfig: tlsConfig,
+ Proxy: http.ProxyFromEnvironment,
+ TLSClientConfig: tlsConfig,
},
}
return client.Get(source)
diff --git a/src/cmd/vet/testdata/composite.go b/src/cmd/vet/testdata/composite.go
index ce9bc78e49..3fe3eac78c 100644
--- a/src/cmd/vet/testdata/composite.go
+++ b/src/cmd/vet/testdata/composite.go
@@ -115,6 +115,6 @@ var goodNamedPointerSliceLiteral = []*unicode.CaseRange{
&unicode.CaseRange{Lo: 1, Hi: 2},
}
var badNamedPointerSliceLiteral = []*unicode.CaseRange{
- {1, 2}, // ERROR "unkeyed fields"
+ {1, 2}, // ERROR "unkeyed fields"
&unicode.CaseRange{1, 2}, // ERROR "unkeyed fields"
}