]> Cypherpunks repositories - gostls13.git/commitdiff
gofmt: indent multi-line signatures
authorRobert Griesemer <gri@golang.org>
Tue, 6 Sep 2011 18:27:36 +0000 (11:27 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 6 Sep 2011 18:27:36 +0000 (11:27 -0700)
There may be more fine-tuning down the line,
but this CL fixes the most pressing issue at
hand.

Also: gofmt -w src misc

Fixes #1524.

R=rsc, bradfitz
CC=golang-dev
https://golang.org/cl/4975053

misc/cgo/test/helpers.go
src/pkg/go/printer/nodes.go
src/pkg/go/printer/testdata/declarations.golden
src/pkg/go/printer/testdata/declarations.input

index 3a4f014225dace94a4f2e6e5761d9d77e7363eb5..de14d19abf4189d70d3aacd7c655da1154f54920 100644 (file)
@@ -16,7 +16,7 @@ import (
 const greeting = "hello, world"
 
 type testPair struct {
-       Name string
+       Name      string
        Got, Want interface{}
 }
 
index 9cd975ec1be783a01705e2f26a627605f4c0f7c5..364530634aaeb21763155d63cdede2bdcd902893 100644 (file)
@@ -269,6 +269,7 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp
 func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
        p.print(fields.Opening, token.LPAREN)
        if len(fields.List) > 0 {
+               ws := indent
                var prevLine, line int
                for i, par := range fields.List {
                        if i > 0 {
@@ -278,19 +279,30 @@ func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
                                } else {
                                        line = p.fset.Position(par.Type.Pos()).Line
                                }
-                               if 0 < prevLine && prevLine < line && p.linebreak(line, 0, ignore, true) {
+                               if 0 < prevLine && prevLine < line && p.linebreak(line, 0, ws, true) {
+                                       ws = ignore
                                        *multiLine = true
                                } else {
                                        p.print(blank)
                                }
                        }
                        if len(par.Names) > 0 {
-                               p.identList(par.Names, false, multiLine)
+                               // Very subtle: If we indented before (ws == ignore), identList
+                               // won't indent again. If we didn't (ws == indent), identList will
+                               // indent if the identList spans multiple lines, and it will outdent
+                               // again at the end (and still ws == indent). Thus, a subsequent indent
+                               // by a linebreak call after a type, or in the next multi-line identList
+                               // will do the right thing.
+                               p.identList(par.Names, ws == indent, multiLine)
                                p.print(blank)
                        }
                        p.expr(par.Type, multiLine)
                        prevLine = p.fset.Position(par.Type.Pos()).Line
                }
+               if ws == ignore {
+                       // unindent if we indented
+                       p.print(unindent)
+               }
        }
        p.print(fields.Closing, token.RPAREN)
 }
index 970533e8cfbe1a3a3ad2ac557b907d4b90a39819..bfa2568c21fd29ea3f8ea5697f4591be112d01f0 100644 (file)
@@ -692,56 +692,119 @@ func _(x ...chan int)
 
 // these parameter lists must remain multi-line since they are multi-line in the source
 func _(bool,
-int) {
+       int) {
 }
 func _(x bool,
-y int) {
+       y int) {
 }
 func _(x,
-y bool) {
+       y bool) {
 }
 func _(bool,   // comment
-int) {
+       int) {
 }
 func _(x bool, // comment
-y int) {
+       y int) {
 }
 func _(x,      // comment
-y bool) {
+       y bool) {
 }
 func _(bool,   // comment
-// comment
-int) {
+       // comment
+       int) {
 }
 func _(x bool, // comment
-// comment
-y int) {
+       // comment
+       y int) {
 }
 func _(x,      // comment
-// comment
-y bool) {
+       // comment
+       y bool) {
 }
 func _(bool,
-// comment
-int) {
+       // comment
+       int) {
 }
 func _(x bool,
-// comment
-y int) {
+       // comment
+       y int) {
 }
 func _(x,
-// comment
-y bool) {
+       // comment
+       y bool) {
 }
 func _(x,      // comment
-y,     // comment
-z bool) {
+       y,      // comment
+       z bool) {
 }
 func _(x,      // comment
-y,     // comment
-z bool) {
+       y,      // comment
+       z bool) {
 }
 func _(x int,  // comment
-y float,       // comment
-z bool) {
+       y float,        // comment
+       z bool) {
+}
+
+// properly indent multi-line signatures
+func ManageStatus(in <-chan *Status, req <-chan Request,
+       stat chan<- *TargetInfo,
+       TargetHistorySize int) {
+}
+
+func MultiLineSignature0(a, b, c int) {
+}
+
+func MultiLineSignature1(a, b, c int,
+       u, v, w float) {
+}
+
+func MultiLineSignature2(a, b,
+       c int) {
+}
+
+func MultiLineSignature3(a, b,
+       c int, u, v,
+       w float,
+       x ...int) {
+}
+
+func MultiLineSignature4(a, b, c int,
+       u, v,
+       w float,
+       x ...int) {
+}
+
+func MultiLineSignature5(a, b, c int,
+       u, v, w float,
+       p, q,
+       r string,
+       x ...int) {
+}
+
+// make sure it also works for methods in interfaces
+type _ interface {
+       MultiLineSignature0(a, b, c int)
+
+       MultiLineSignature1(a, b, c int,
+               u, v, w float)
+
+       MultiLineSignature2(a, b,
+               c int)
+
+       MultiLineSignature3(a, b,
+               c int, u, v,
+               w float,
+               x ...int)
+
+       MultiLineSignature4(a, b, c int,
+               u, v,
+               w float,
+               x ...int)
+
+       MultiLineSignature5(a, b, c int,
+               u, v, w float,
+               p, q,
+               r string,
+               x ...int)
 }
index c6134096bf131e15427a8f3be0f83b4574fe4e87..1d69c57b517466994fde21dc225b58261239147b 100644 (file)
@@ -755,3 +755,79 @@ func _(x int,      // comment
        y float,        // comment
        z bool) {
 }
+
+
+// properly indent multi-line signatures
+func ManageStatus(in <-chan *Status, req <-chan Request,
+stat chan<- *TargetInfo,
+TargetHistorySize int) {
+}
+
+func MultiLineSignature0(
+a, b, c int,
+) {}
+
+func MultiLineSignature1(
+a, b, c int,
+u, v, w float,
+) {}
+
+func MultiLineSignature2(
+a, b,
+c int,
+) {}
+
+func MultiLineSignature3(
+a, b,
+c int, u, v,
+w float,
+               x ...int) {}
+
+func MultiLineSignature4(
+a, b, c int,
+u, v,
+w float,
+               x ...int) {}
+
+func MultiLineSignature5(
+a, b, c int,
+u, v, w float,
+p, q,
+r string,
+               x ...int) {}
+
+// make sure it also works for methods in interfaces
+type _ interface {
+MultiLineSignature0(
+a, b, c int,
+)
+
+MultiLineSignature1(
+a, b, c int,
+u, v, w float,
+)
+
+MultiLineSignature2(
+a, b,
+c int,
+)
+
+MultiLineSignature3(
+a, b,
+c int, u, v,
+w float,
+               x ...int)
+
+MultiLineSignature4(
+a, b, c int,
+u, v,
+w float,
+               x ...int)
+
+MultiLineSignature5(
+a, b, c int,
+u, v, w float,
+p, q,
+r string,
+               x ...int)
+}