Skip to content

Commit

Permalink
go/printer: fix alignment of comments in labeled statements
Browse files Browse the repository at this point in the history
Does not change src, misc formatting.

Fixes #5623.

LGTM=r
R=golang-codereviews, r
CC=golang-codereviews
https://golang.org/cl/68400043
  • Loading branch information
griesemer committed Feb 25, 2014
1 parent 49beb23 commit c738591
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 3 deletions.
17 changes: 15 additions & 2 deletions src/pkg/go/printer/nodes.go
Expand Up @@ -906,15 +906,19 @@ func (p *printer) stmtList(list []ast.Stmt, nindent int, nextIsRBrace bool) {
for _, s := range list {
// ignore empty statements (was issue 3466)
if _, isEmpty := s.(*ast.EmptyStmt); !isEmpty {
// _indent == 0 only for lists of switch/select case clauses;
// nindent == 0 only for lists of switch/select case clauses;
// in those cases each clause is a new section
if len(p.output) > 0 {
// only print line break if we are not at the beginning of the output
// (i.e., we are not printing only a partial program)
p.linebreak(p.lineFor(s.Pos()), 1, ignore, i == 0 || nindent == 0 || multiLine)
}
p.stmt(s, nextIsRBrace && i == len(list)-1)
multiLine = p.isMultiLine(s)
// labeled statements put labels on a separate line, but here
// we only care about whether the actual statement w/o label
// is a multi-line statement - remove the label first
// (was issue 5623)
multiLine = p.isMultiLine(unlabeledStmt(s))
i++
}
}
Expand All @@ -923,6 +927,15 @@ func (p *printer) stmtList(list []ast.Stmt, nindent int, nextIsRBrace bool) {
}
}

// unlabeledStmt returns the statement of a labeled statement s;
// otherwise it return s.
func unlabeledStmt(s ast.Stmt) ast.Stmt {
if s, _ := s.(*ast.LabeledStmt); s != nil {
return unlabeledStmt(s.Stmt)
}
return s
}

// block prints an *ast.BlockStmt; it always spans at least two lines.
func (p *printer) block(b *ast.BlockStmt, nindent int) {
p.print(b.Lbrace, token.LBRACE)
Expand Down
25 changes: 25 additions & 0 deletions src/pkg/go/printer/testdata/comments2.golden
Expand Up @@ -77,3 +77,28 @@ func main() {
println("test")
}
}

func issue5623() {
L:
_ = yyyyyyyyyyyyyyyy // comment - should be aligned
_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx /* comment */

_ = yyyyyyyyyyyyyyyy /* comment - should be aligned */
_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx // comment

LLLLLLL:
_ = yyyyyyyyyyyyyyyy // comment - should be aligned
_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx // comment

LL:
_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx /* comment */
_ = yyyyyyyyyyyyyyyy /* comment - should be aligned */

_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx // comment
_ = yyyyyyyyyyyyyyyy // comment - should be aligned

// test case from issue
label:
mask := uint64(1)<<c - 1 // Allocation mask
used := atomic.LoadUint64(&h.used) // Current allocations
}
27 changes: 26 additions & 1 deletion src/pkg/go/printer/testdata/comments2.input
Expand Up @@ -76,4 +76,29 @@ prints test 5 times
for i := 0; i < 5; i++ {
println("test")
}
}
}

func issue5623() {
L:
_ = yyyyyyyyyyyyyyyy // comment - should be aligned
_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx /* comment */

_ = yyyyyyyyyyyyyyyy /* comment - should be aligned */
_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx // comment

LLLLLLL:
_ = yyyyyyyyyyyyyyyy // comment - should be aligned
_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx // comment

LL:
_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx /* comment */
_ = yyyyyyyyyyyyyyyy /* comment - should be aligned */

_ = xxxxxxxxxxxxxxxxxxxxxxxxxxxx // comment
_ = yyyyyyyyyyyyyyyy // comment - should be aligned

// test case from issue
label:
mask := uint64(1)<<c - 1 // Allocation mask
used := atomic.LoadUint64(&h.used) // Current allocations
}

0 comments on commit c738591

Please sign in to comment.