mirror of
https://github.com/golang/go
synced 2024-11-25 04:57:56 -07:00
gofmt: fix alignment of multi-line var declarations
- gofmt -w src misc R=rsc, r CC=golang-dev https://golang.org/cl/223101
This commit is contained in:
parent
e678afa891
commit
305f5433f3
@ -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
|
type exprListMode uint
|
||||||
|
|
||||||
const (
|
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.
|
// isOneLineExpr returns true if x is "small enough" to fit onto a single line.
|
||||||
func (p *printer) isOneLineExpr(x ast.Expr) bool {
|
func (p *printer) isOneLineExpr(x ast.Expr) bool {
|
||||||
const maxSize = 60 // aproximate value, excluding space for comments
|
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)
|
p.print(token.COMMA, blank)
|
||||||
}
|
}
|
||||||
if len(par.Names) > 0 {
|
if len(par.Names) > 0 {
|
||||||
p.identList(par.Names, multiLine)
|
p.identList(par.Names, false, multiLine)
|
||||||
p.print(blank)
|
p.print(blank)
|
||||||
}
|
}
|
||||||
p.expr(par.Type, multiLine)
|
p.expr(par.Type, multiLine)
|
||||||
@ -352,7 +358,7 @@ func (p *printer) fieldList(fields *ast.FieldList, isIncomplete bool, ctxt exprC
|
|||||||
p.setComment(f.Doc)
|
p.setComment(f.Doc)
|
||||||
if len(f.Names) > 0 {
|
if len(f.Names) > 0 {
|
||||||
// named fields
|
// named fields
|
||||||
p.identList(f.Names, &ml)
|
p.identList(f.Names, false, &ml)
|
||||||
p.print(sep)
|
p.print(sep)
|
||||||
p.expr(f.Type, &ml)
|
p.expr(f.Type, &ml)
|
||||||
extraTabs = 1
|
extraTabs = 1
|
||||||
@ -1040,10 +1046,11 @@ const (
|
|||||||
|
|
||||||
// The parameter n is the number of specs in the group; context specifies
|
// The parameter n is the number of specs in the group; context specifies
|
||||||
// the surroundings of the declaration. Separating semicolons are printed
|
// the surroundings of the declaration. Separating semicolons are printed
|
||||||
// depending on the context. Sets multiLine to true if the spec spans
|
// depending on the context. If indent is set, a multi-line identifier lists
|
||||||
// multiple lines.
|
// 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 (
|
var (
|
||||||
comment *ast.CommentGroup // a line comment, if any
|
comment *ast.CommentGroup // a line comment, if any
|
||||||
extraTabs int // number of extra tabs before 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:
|
case *ast.ValueSpec:
|
||||||
p.setComment(s.Doc)
|
p.setComment(s.Doc)
|
||||||
p.identList(s.Names, multiLine) // always present
|
p.identList(s.Names, indent, multiLine) // always present
|
||||||
if n == 1 {
|
if n == 1 {
|
||||||
if s.Type != nil {
|
if s.Type != nil {
|
||||||
p.print(blank)
|
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)
|
p.linebreak(s.Pos().Line, 1, 2, ignore, ml)
|
||||||
}
|
}
|
||||||
ml = false
|
ml = false
|
||||||
p.spec(s, len(d.Specs), inGroup, &ml)
|
p.spec(s, len(d.Specs), inGroup, false, &ml)
|
||||||
}
|
}
|
||||||
p.print(unindent, formfeed)
|
p.print(unindent, formfeed)
|
||||||
*multiLine = true
|
*multiLine = true
|
||||||
@ -1138,7 +1145,7 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool)
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
// single declaration
|
// single declaration
|
||||||
p.spec(d.Specs[0], 1, context, multiLine)
|
p.spec(d.Specs[0], 1, context, true, multiLine)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
22
src/pkg/go/printer/testdata/declarations.golden
vendored
22
src/pkg/go/printer/testdata/declarations.golden
vendored
@ -411,6 +411,14 @@ type _ struct {
|
|||||||
h float "tag" // comment
|
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
|
// difficult cases
|
||||||
type _ struct {
|
type _ struct {
|
||||||
@ -444,6 +452,7 @@ type _ interface { // this comment must not change indentation
|
|||||||
gggggggggggg(x, y, z int) // hurray
|
gggggggggggg(x, y, z int) // hurray
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// formatting of variable declarations
|
// formatting of variable declarations
|
||||||
func _() {
|
func _() {
|
||||||
type day struct {
|
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 _() {
|
func _() {
|
||||||
var privateKey2 = &Block{Type: "RSA PRIVATE KEY",
|
var privateKey2 = &Block{Type: "RSA PRIVATE KEY",
|
||||||
Headers: map[string]string{},
|
Headers: map[string]string{},
|
||||||
|
21
src/pkg/go/printer/testdata/declarations.input
vendored
21
src/pkg/go/printer/testdata/declarations.input
vendored
@ -410,6 +410,13 @@ type _ struct {
|
|||||||
h float "tag" // comment
|
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
|
// difficult cases
|
||||||
type _ struct {
|
type _ struct {
|
||||||
@ -418,7 +425,6 @@ type _ struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// formatting of interfaces
|
// formatting of interfaces
|
||||||
type EI interface{}
|
type EI interface{}
|
||||||
|
|
||||||
@ -444,6 +450,7 @@ type _ interface { // this comment must not change indentation
|
|||||||
gggggggggggg(x, y, z int) () // hurray
|
gggggggggggg(x, y, z int) () // hurray
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// formatting of variable declarations
|
// formatting of variable declarations
|
||||||
func _() {
|
func _() {
|
||||||
type day struct { n int; short, long string }
|
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 _() {
|
func _() {
|
||||||
var privateKey2 = &Block{Type: "RSA PRIVATE KEY",
|
var privateKey2 = &Block{Type: "RSA PRIVATE KEY",
|
||||||
Headers: map[string]string{},
|
Headers: map[string]string{},
|
||||||
|
Loading…
Reference in New Issue
Block a user