Commit 305f5433 authored by Robert Griesemer's avatar Robert Griesemer

gofmt: fix alignment of multi-line var declarations

- gofmt -w src misc

R=rsc, r
CC=golang-dev
https://golang.org/cl/223101
parent e678afa8
......@@ -81,17 +81,6 @@ func (p *printer) setComment(g *ast.CommentGroup) {
}
// Sets multiLine to true if the identifier list spans multiple lines.
func (p *printer) identList(list []*ast.Ident, multiLine *bool) {
// convert into an expression list so we can re-use exprList formatting
xlist := make([]ast.Expr, len(list))
for i, x := range list {
xlist[i] = x
}
p.exprList(noPos, xlist, 1, commaSep, multiLine, noPos)
}
type exprListMode uint
const (
......@@ -103,6 +92,23 @@ const (
)
// Sets multiLine to true if the identifier list spans multiple lines.
// If ident is set, a multi-line identifier list is indented after the
// first linebreak encountered.
func (p *printer) identList(list []*ast.Ident, indent bool, multiLine *bool) {
// convert into an expression list so we can re-use exprList formatting
xlist := make([]ast.Expr, len(list))
for i, x := range list {
xlist[i] = x
}
mode := commaSep
if !indent {
mode |= noIndent
}
p.exprList(noPos, xlist, 1, mode, multiLine, noPos)
}
// isOneLineExpr returns true if x is "small enough" to fit onto a single line.
func (p *printer) isOneLineExpr(x ast.Expr) bool {
const maxSize = 60 // aproximate value, excluding space for comments
......@@ -238,7 +244,7 @@ func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
p.print(token.COMMA, blank)
}
if len(par.Names) > 0 {
p.identList(par.Names, multiLine)
p.identList(par.Names, false, multiLine)
p.print(blank)
}
p.expr(par.Type, multiLine)
......@@ -352,7 +358,7 @@ func (p *printer) fieldList(fields *ast.FieldList, isIncomplete bool, ctxt exprC
p.setComment(f.Doc)
if len(f.Names) > 0 {
// named fields
p.identList(f.Names, &ml)
p.identList(f.Names, false, &ml)
p.print(sep)
p.expr(f.Type, &ml)
extraTabs = 1
......@@ -1040,10 +1046,11 @@ const (
// The parameter n is the number of specs in the group; context specifies
// the surroundings of the declaration. Separating semicolons are printed
// depending on the context. Sets multiLine to true if the spec spans
// multiple lines.
// depending on the context. If indent is set, a multi-line identifier lists
// in the spec are indented when the first linebreak is encountered. Sets
// multiLine to true if the spec spans multiple lines.
//
func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *bool) {
func (p *printer) spec(spec ast.Spec, n int, context declContext, indent bool, multiLine *bool) {
var (
comment *ast.CommentGroup // a line comment, if any
extraTabs int // number of extra tabs before comment, if any
......@@ -1061,7 +1068,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo
case *ast.ValueSpec:
p.setComment(s.Doc)
p.identList(s.Names, multiLine) // always present
p.identList(s.Names, indent, multiLine) // always present
if n == 1 {
if s.Type != nil {
p.print(blank)
......@@ -1129,7 +1136,7 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool)
p.linebreak(s.Pos().Line, 1, 2, ignore, ml)
}
ml = false
p.spec(s, len(d.Specs), inGroup, &ml)
p.spec(s, len(d.Specs), inGroup, false, &ml)
}
p.print(unindent, formfeed)
*multiLine = true
......@@ -1138,7 +1145,7 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool)
} else {
// single declaration
p.spec(d.Specs[0], 1, context, multiLine)
p.spec(d.Specs[0], 1, context, true, multiLine)
}
}
......
......@@ -411,6 +411,14 @@ type _ struct {
h float "tag" // comment
}
type _ struct {
a, b,
c, d int // this line should be indented
u, v, w, x float // this line should be indented
p, q,
r, s float // this line should be indented
}
// difficult cases
type _ struct {
......@@ -444,6 +452,7 @@ type _ interface { // this comment must not change indentation
gggggggggggg(x, y, z int) // hurray
}
// formatting of variable declarations
func _() {
type day struct {
......@@ -462,6 +471,19 @@ func _() {
}
// formatting of multi-line variable declarations
var a1, b1, c1 int // all on one line
var a2, b2,
c2 int // this line should be indented
var (
a3, b3,
c3, d3 int // this line should be indented
a4, b4, c4 int // this line should be indented
)
func _() {
var privateKey2 = &Block{Type: "RSA PRIVATE KEY",
Headers: map[string]string{},
......
......@@ -410,6 +410,13 @@ type _ struct {
h float "tag" // comment
}
type _ struct { a, b,
c, d int // this line should be indented
u, v, w, x float // this line should be indented
p, q,
r, s float // this line should be indented
}
// difficult cases
type _ struct {
......@@ -418,7 +425,6 @@ type _ struct {
}
// formatting of interfaces
type EI interface{}
......@@ -444,6 +450,7 @@ type _ interface { // this comment must not change indentation
gggggggggggg(x, y, z int) () // hurray
}
// formatting of variable declarations
func _() {
type day struct { n int; short, long string }
......@@ -459,6 +466,18 @@ func _() {
}
// formatting of multi-line variable declarations
var a1, b1, c1 int // all on one line
var a2, b2,
c2 int // this line should be indented
var (a3, b3,
c3, d3 int // this line should be indented
a4, b4, c4 int // this line should be indented
)
func _() {
var privateKey2 = &Block{Type: "RSA PRIVATE KEY",
Headers: map[string]string{},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment