1
0
mirror of https://github.com/golang/go synced 2024-11-19 13:34:45 -07:00

go/printer: indent lone comments in composite lits

If a composite literal contains any comments on their own lines without
any elements, the printer would unindent the comments.

The comments in this edge case are written when the closing '}' is
written. Indent and outdent first so that the indentation is
interspersed before the comment is written.

Also note that the go/printer golden tests don't show the exact same
behaviour that gofmt does. Added a TODO to figure this out in a separate
CL.

While at it, ensure that the tree conforms to gofmt. The changes are
unrelated to this indentation fix, however.

Fixes #22355.

Change-Id: I5ac25ac6de95a236f1e123479127cc4dd71e93fe
Reviewed-on: https://go-review.googlesource.com/74232
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Daniel Martí 2017-10-29 21:04:01 +00:00 committed by Robert Griesemer
parent 89b7a08aea
commit a265f2e90e
4 changed files with 59 additions and 3 deletions

View File

@ -865,7 +865,9 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
if len(x.Elts) > 0 { if len(x.Elts) > 0 {
mode |= noExtraBlank mode |= noExtraBlank
} }
p.print(mode, x.Rbrace, token.RBRACE, mode) // need the initial indent to print lone comments with
// the proper level of indentation
p.print(indent, unindent, mode, x.Rbrace, token.RBRACE, mode)
p.level-- p.level--
case *ast.Ellipsis: case *ast.Ellipsis:

View File

@ -831,4 +831,32 @@ func _() {
*/ */
} }
var _ = []T{ /* lone comment */ }
var _ = []T{
/* lone comment */
}
var _ = []T{
// lone comments
// in composite lit
}
var _ = [][]T{
{
// lone comments
// in composite lit
},
}
// TODO: gofmt doesn't add these tabs; make it so that these golden
// tests run the printer in a way that it's exactly like gofmt.
var _ = []T{ // lone comment
}
var _ = []T{ // lone comments
// in composite lit
}
/* This comment is the last entry in this file. It must be printed and should be followed by a newline */ /* This comment is the last entry in this file. It must be printed and should be followed by a newline */

View File

@ -832,4 +832,32 @@ func _() {
*/ */
} }
var _ = []T{/* lone comment */}
var _ = []T{
/* lone comment */
}
var _ = []T{
// lone comments
// in composite lit
}
var _ = [][]T{
{
// lone comments
// in composite lit
},
}
// TODO: gofmt doesn't add these tabs; make it so that these golden
// tests run the printer in a way that it's exactly like gofmt.
var _ = []T{// lone comment
}
var _ = []T{// lone comments
// in composite lit
}
/* This comment is the last entry in this file. It must be printed and should be followed by a newline */ /* This comment is the last entry in this file. It must be printed and should be followed by a newline */

View File

@ -533,7 +533,6 @@ func (z *Int) GCD(x, y, a, b *Int) *Int {
// See Jebelean, "Improving the multiprecision Euclidean algorithm", // See Jebelean, "Improving the multiprecision Euclidean algorithm",
// Design and Implementation of Symbolic Computation Systems, pp 45-58. // Design and Implementation of Symbolic Computation Systems, pp 45-58.
func (z *Int) lehmerGCD(a, b *Int) *Int { func (z *Int) lehmerGCD(a, b *Int) *Int {
// ensure a >= b // ensure a >= b
if a.abs.cmp(b.abs) < 0 { if a.abs.cmp(b.abs) < 0 {
a, b = b, a a, b = b, a
@ -551,7 +550,6 @@ func (z *Int) lehmerGCD(a, b *Int) *Int {
// loop invariant A >= B // loop invariant A >= B
for len(B.abs) > 1 { for len(B.abs) > 1 {
// initialize the digits // initialize the digits
var a1, a2, u0, u1, u2, v0, v1, v2 Word var a1, a2, u0, u1, u2, v0, v1, v2 Word