]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer: indent lone comments in composite lits
authorDaniel Martí <mvdan@mvdan.cc>
Sun, 29 Oct 2017 21:04:01 +0000 (21:04 +0000)
committerRobert Griesemer <gri@golang.org>
Wed, 15 Nov 2017 18:48:48 +0000 (18:48 +0000)
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>
src/go/printer/nodes.go
src/go/printer/testdata/comments.golden
src/go/printer/testdata/comments.input
src/math/big/int.go

index 83c17fef5e64f05a5a7edcab9143ebc7d07c05ff..5b5b6063fbfade7c7d13d0c149225e7b45346a36 100644 (file)
@@ -865,7 +865,9 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
                if len(x.Elts) > 0 {
                        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--
 
        case *ast.Ellipsis:
index d14d8d6cbb37fb09a0a6b07d3f8ea6ae680d18f2..3fc29ccf9133f11971bcc1b120dcb7c223cba6bd 100644 (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 */
index df98056a65b61748e748a44a43fd7caf6e5192ab..985e33479561507117fd72cc0612864553ffa6c5 100644 (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 */
index 135ebd083ffd39dff148860922e69b3c3a4c7e2d..0eda9cd4e12356c1f9b8aaf25a993d99dc7ac1c4 100644 (file)
@@ -533,7 +533,6 @@ func (z *Int) GCD(x, y, a, b *Int) *Int {
 // See Jebelean, "Improving the multiprecision Euclidean algorithm",
 // Design and Implementation of Symbolic Computation Systems, pp 45-58.
 func (z *Int) lehmerGCD(a, b *Int) *Int {
-
        // ensure a >= b
        if a.abs.cmp(b.abs) < 0 {
                a, b = b, a
@@ -551,7 +550,6 @@ func (z *Int) lehmerGCD(a, b *Int) *Int {
 
        // loop invariant A >= B
        for len(B.abs) > 1 {
-
                // initialize the digits
                var a1, a2, u0, u1, u2, v0, v1, v2 Word