mirror of
https://github.com/golang/go
synced 2024-11-25 06:07:58 -07:00
gofmt: experiment: align values in map composites where possible
- gofmt -w src misc - looking for feedback R=rsc, r CC=golang-dev https://golang.org/cl/223076
This commit is contained in:
parent
36ea8bb7bf
commit
f44fa9b4c7
@ -159,15 +159,17 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode
|
|||||||
ws = indent
|
ws = indent
|
||||||
}
|
}
|
||||||
|
|
||||||
|
oneLiner := false // true if the previous expression fit on a single line
|
||||||
|
prevBreak := -1 // index of last expression that was followed by a linebreak
|
||||||
|
|
||||||
// the first linebreak is always a formfeed since this section must not
|
// the first linebreak is always a formfeed since this section must not
|
||||||
// depend on any previous formatting
|
// depend on any previous formatting
|
||||||
if prev.IsValid() && prev.Line < line && p.linebreak(line, 1, 2, ws, true) {
|
if prev.IsValid() && prev.Line < line && p.linebreak(line, 1, 2, ws, true) {
|
||||||
ws = ignore
|
ws = ignore
|
||||||
*multiLine = true
|
*multiLine = true
|
||||||
|
prevBreak = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
oneLiner := false // true if the previous expression fit on a single line
|
|
||||||
prevBreak := 0 // index of last expression that was followed by a linebreak
|
|
||||||
for i, x := range list {
|
for i, x := range list {
|
||||||
prev := line
|
prev := line
|
||||||
line = x.Pos().Line
|
line = x.Pos().Line
|
||||||
@ -189,12 +191,20 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode
|
|||||||
p.print(blank)
|
p.print(blank)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p.expr0(x, depth, multiLine)
|
|
||||||
// determine if x satisfies the "one-liner" criteria
|
// determine if x satisfies the "one-liner" criteria
|
||||||
// TODO(gri): determine if the multiline information returned
|
// TODO(gri): determine if the multiline information returned
|
||||||
// from p.expr0 is precise enough so it could be
|
// from p.expr0 is precise enough so it could be
|
||||||
// used instead
|
// used instead
|
||||||
oneLiner = p.isOneLineExpr(x)
|
oneLiner = p.isOneLineExpr(x)
|
||||||
|
if t, isPair := x.(*ast.KeyValueExpr); isPair && oneLiner && len(list) > 1 {
|
||||||
|
// we have a key:value expression that fits onto one line, and
|
||||||
|
// is a list with more then one entry: align all the values
|
||||||
|
p.expr(t.Key, multiLine)
|
||||||
|
p.print(t.Colon, token.COLON, vtab)
|
||||||
|
p.expr(t.Value, multiLine)
|
||||||
|
} else {
|
||||||
|
p.expr0(x, depth, multiLine)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line {
|
if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line {
|
||||||
|
12
src/pkg/go/printer/testdata/declarations.golden
vendored
12
src/pkg/go/printer/testdata/declarations.golden
vendored
@ -462,6 +462,18 @@ func _() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
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,
|
||||||
|
0xd4, 0x0, 0xae, 0x6a, 0x4d, 0x1b, 0x8a, 0x3b, 0x6a, 0x13,
|
||||||
|
0x64, 0x2b, 0x23, 0xf2, 0x8b, 0x0, 0x3b, 0xfb, 0x97, 0x79,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func _() {
|
func _() {
|
||||||
var Universe = Scope{
|
var Universe = Scope{
|
||||||
Names: map[string]*Ident{
|
Names: map[string]*Ident{
|
||||||
|
12
src/pkg/go/printer/testdata/declarations.input
vendored
12
src/pkg/go/printer/testdata/declarations.input
vendored
@ -459,6 +459,18 @@ func _() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
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,
|
||||||
|
0xd4, 0x0, 0xae, 0x6a, 0x4d, 0x1b, 0x8a, 0x3b, 0x6a, 0x13,
|
||||||
|
0x64, 0x2b, 0x23, 0xf2, 0x8b, 0x0, 0x3b, 0xfb, 0x97, 0x79,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
func _() {
|
func _() {
|
||||||
var Universe = Scope {
|
var Universe = Scope {
|
||||||
Names: map[string]*Ident {
|
Names: map[string]*Ident {
|
||||||
|
Loading…
Reference in New Issue
Block a user