]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/gofmt, go/printer: fix mis-alignment of comment on one-line function
authorRobert Griesemer <gri@golang.org>
Fri, 16 Jun 2017 23:13:28 +0000 (16:13 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 19 Jun 2017 17:51:46 +0000 (17:51 +0000)
Fixes #19544.

Change-Id: I5df67383e9471f030ddafabadf2bc19ce6816f0f
Reviewed-on: https://go-review.googlesource.com/46002
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/go/printer/nodes.go
src/go/printer/testdata/declarations.golden
src/go/printer/testdata/declarations.input

index bea4ff2a3adb6fa164070d28e76119d571f95720..4eaadeb448f6854e29ddf8020c437a7f28d940a1 100644 (file)
@@ -1532,6 +1532,16 @@ func (p *printer) nodeSize(n ast.Node, maxSize int) (size int) {
        return
 }
 
+// numLines returns the number of lines spanned by node n in the original source.
+func (p *printer) numLines(n ast.Node) int {
+       if from := n.Pos(); from.IsValid() {
+               if to := n.End(); to.IsValid() {
+                       return p.lineFor(to) - p.lineFor(from) + 1
+               }
+       }
+       return infinity
+}
+
 // bodySize is like nodeSize but it is specialized for *ast.BlockStmt's.
 func (p *printer) bodySize(b *ast.BlockStmt, maxSize int) int {
        pos1 := b.Pos()
@@ -1668,7 +1678,9 @@ func (p *printer) declList(list []ast.Decl) {
                        if prev != tok || getDoc(d) != nil {
                                min = 2
                        }
-                       p.linebreak(p.lineFor(d.Pos()), min, ignore, false)
+                       // start a new section if the next declaration is a function
+                       // that spans multiple lines (see also issue #19544)
+                       p.linebreak(p.lineFor(d.Pos()), min, ignore, tok == token.FUNC && p.numLines(d) > 1)
                }
                p.decl(d)
        }
index d4ea545658cee9a633baab8b00b9b4b2cb1c88d7..bebc0eaa637e2c83d3a7f148fdf97af71e571565 100644 (file)
@@ -778,6 +778,12 @@ func _() {
        /* multi-line func because block is on multiple lines */
 }
 
+// test case for issue #19544
+func _()       {}
+func _longer_name_() { // this comment must not force the {} from above to alignment
+       // multiple lines
+}
+
 // ellipsis parameters
 func _(...int)
 func _(...*int)
index 50386eb8d5deb7d84571f735794cb8cfd41a72fe..a858051ef01a650d69a9d4a4039d53e601513cac 100644 (file)
@@ -795,6 +795,11 @@ func _() { /* multi-line function because of "long-ish" comment - much more comm
 func _() {
 /* multi-line func because block is on multiple lines */ }
 
+// test case for issue #19544
+func _() {}
+func _longer_name_() { // this comment must not force the {} from above to alignment
+       // multiple lines
+}
 
 // ellipsis parameters
 func _(...int)