]> Cypherpunks repositories - gostls13.git/commitdiff
go/printer, gofmt: print "select {}" on one line
authorRobert Griesemer <gri@golang.org>
Thu, 16 Jun 2011 04:47:29 +0000 (21:47 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 16 Jun 2011 04:47:29 +0000 (21:47 -0700)
No impact on existing sources under src, misc.

Fixes #1959.

R=r, rsc, r
CC=golang-dev
https://golang.org/cl/4589056

src/pkg/go/printer/nodes.go
src/pkg/go/printer/testdata/statements.golden
src/pkg/go/printer/testdata/statements.input

index 0fca8a161bb833a710898839d57ba8baf75c2414..f2b79d8103c243293b891007273e91378eb12df2 100644 (file)
@@ -1158,8 +1158,14 @@ func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool, multiLine *bool) {
 
        case *ast.SelectStmt:
                p.print(token.SELECT, blank)
-               p.block(s.Body, 0)
-               *multiLine = true
+               body := s.Body
+               if len(body.List) == 0 && !p.commentBefore(p.fset.Position(body.Rbrace)) {
+                       // print empty select statement w/o comments on one line
+                       p.print(body.Lbrace, token.LBRACE, body.Rbrace, token.RBRACE)
+               } else {
+                       p.block(body, 0)
+                       *multiLine = true
+               }
 
        case *ast.ForStmt:
                p.print(token.FOR)
index 2900602699ffbd0450b6cb100fb8059e59708974..0e484044137d80604b0e66d8a705043427e03b13 100644 (file)
@@ -111,6 +111,21 @@ func _() {
 }
 
 
+// Formatting of selected select statements.
+func _() {
+       select {}
+       select { /* this comment should not be tab-aligned because the closing } is on the same line */
+       }
+       select {        /* this comment should be tab-aligned */
+       }
+       select {        // this comment should be tab-aligned
+       }
+       select {
+       case <-c:
+       }
+}
+
+
 // Formatting of for-statement headers.
 func _() {
        for {
index 21e61efc4f8f308d38bde6d6eb60f8fd59212217..86a753c5ad9eca33cefe7ec6f220c9f70586562e 100644 (file)
@@ -91,6 +91,19 @@ func _() {
 }
 
 
+// Formatting of selected select statements.
+func _() {
+       select {
+       }
+       select { /* this comment should not be tab-aligned because the closing } is on the same line */ }
+       select { /* this comment should be tab-aligned */
+       }
+       select { // this comment should be tab-aligned
+       }
+       select { case <-c: }
+}
+
+
 // Formatting of for-statement headers.
 func _() {
        for{}