summaryrefslogtreecommitdiff
path: root/src/pkg/go
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2010-03-02 17:23:07 -0800
committerRobert Griesemer <gri@golang.org>2010-03-02 17:23:07 -0800
commit9410f2b194ecc7605f3a09cd50f44239ddee0ee0 (patch)
tree302ff5a647bcb02e8275973370c72ce8a31e1ba0 /src/pkg/go
parentdaae984c2784aa31de9f3d3d1dae208c0c789901 (diff)
downloadgolang-9410f2b194ecc7605f3a09cd50f44239ddee0ee0.tar.gz
gofmt: fix alignment of multi-line var declarations
- gofmt -w src misc R=rsc, r CC=golang-dev http://codereview.appspot.com/223101
Diffstat (limited to 'src/pkg/go')
-rw-r--r--src/pkg/go/printer/nodes.go45
-rw-r--r--src/pkg/go/printer/testdata/declarations.golden22
-rw-r--r--src/pkg/go/printer/testdata/declarations.input21
3 files changed, 68 insertions, 20 deletions
diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go
index 04b961026..3045300aa 100644
--- a/src/pkg/go/printer/nodes.go
+++ b/src/pkg/go/printer/nodes.go
@@ -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)
}
}
diff --git a/src/pkg/go/printer/testdata/declarations.golden b/src/pkg/go/printer/testdata/declarations.golden
index ef93eb965..c19b90c20 100644
--- a/src/pkg/go/printer/testdata/declarations.golden
+++ b/src/pkg/go/printer/testdata/declarations.golden
@@ -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{},
diff --git a/src/pkg/go/printer/testdata/declarations.input b/src/pkg/go/printer/testdata/declarations.input
index 6c3e1682b..67dac0da6 100644
--- a/src/pkg/go/printer/testdata/declarations.input
+++ b/src/pkg/go/printer/testdata/declarations.input
@@ -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{},