Commit 26b07c43 authored by Robert Griesemer's avatar Robert Griesemer

cmd/gofmt, go/printer: fix mis-alignment of comment on one-line function

Fixes #19544.

Change-Id: I5df67383e9471f030ddafabadf2bc19ce6816f0f
Reviewed-on: https://go-review.googlesource.com/46002Reviewed-by: 's avatarMatthew Dempsky <mdempsky@google.com>
parent 09ebbf40
...@@ -1532,6 +1532,16 @@ func (p *printer) nodeSize(n ast.Node, maxSize int) (size int) { ...@@ -1532,6 +1532,16 @@ func (p *printer) nodeSize(n ast.Node, maxSize int) (size int) {
return 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. // bodySize is like nodeSize but it is specialized for *ast.BlockStmt's.
func (p *printer) bodySize(b *ast.BlockStmt, maxSize int) int { func (p *printer) bodySize(b *ast.BlockStmt, maxSize int) int {
pos1 := b.Pos() pos1 := b.Pos()
...@@ -1668,7 +1678,9 @@ func (p *printer) declList(list []ast.Decl) { ...@@ -1668,7 +1678,9 @@ func (p *printer) declList(list []ast.Decl) {
if prev != tok || getDoc(d) != nil { if prev != tok || getDoc(d) != nil {
min = 2 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) p.decl(d)
} }
......
...@@ -778,6 +778,12 @@ func _() { ...@@ -778,6 +778,12 @@ func _() {
/* multi-line func because block is on multiple lines */ /* 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 // ellipsis parameters
func _(...int) func _(...int)
func _(...*int) func _(...*int)
......
...@@ -795,6 +795,11 @@ func _() { /* multi-line function because of "long-ish" comment - much more comm ...@@ -795,6 +795,11 @@ func _() { /* multi-line function because of "long-ish" comment - much more comm
func _() { func _() {
/* multi-line func because block is on multiple lines */ } /* 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 // ellipsis parameters
func _(...int) func _(...int)
......
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