]> Cypherpunks repositories - gostls13.git/commitdiff
improved spacing around if, switch, and for control clauses
authorRobert Griesemer <gri@golang.org>
Tue, 22 Sep 2009 01:07:20 +0000 (18:07 -0700)
committerRobert Griesemer <gri@golang.org>
Tue, 22 Sep 2009 01:07:20 +0000 (18:07 -0700)
R=r
DELTA=89  (82 added, 5 deleted, 2 changed)
OCL=34870
CL=34870

src/pkg/go/printer/printer.go
src/pkg/go/printer/printer_test.go
src/pkg/go/printer/testdata/statements.go [new file with mode: 0644]
src/pkg/go/printer/testdata/statements.golden [new file with mode: 0644]

index 37bdc2349f1ead4a26ae447f09fea0aea874e6f1..4fdb2af342b1d9a836e13b7a10b954f1b406ec82 100644 (file)
@@ -911,30 +911,37 @@ func (p *printer) switchBlock(s *ast.BlockStmt) {
 
 
 func (p *printer) controlClause(isForStmt bool, init ast.Stmt, expr ast.Expr, post ast.Stmt) {
+       p.print(blank);
+       needsBlank := false;
        if init == nil && post == nil {
                // no semicolons required
                if expr != nil {
-                       p.print(blank);
                        p.expr(expr);
+                       needsBlank = true;
                }
        } else {
                // all semicolons required
                // (they are not separators, print them explicitly)
-               p.print(blank);
                if init != nil {
                        p.stmt(init);
                }
                p.print(token.SEMICOLON, blank);
                if expr != nil {
                        p.expr(expr);
+                       needsBlank = true;
                }
                if isForStmt {
                        p.print(token.SEMICOLON, blank);
+                       needsBlank = false;
                        if post != nil {
                                p.stmt(post);
+                               needsBlank = true;
                        }
                }
        }
+       if needsBlank {
+               p.print(blank);
+       }
 }
 
 
@@ -1007,7 +1014,6 @@ func (p *printer) stmt(stmt ast.Stmt) (optSemi bool) {
        case *ast.IfStmt:
                p.print(token.IF);
                p.controlClause(false, s.Init, s.Cond, nil);
-               p.print(blank);
                p.block(s.Body);
                optSemi = true;
                if s.Else != nil {
@@ -1028,7 +1034,6 @@ func (p *printer) stmt(stmt ast.Stmt) (optSemi bool) {
        case *ast.SwitchStmt:
                p.print(token.SWITCH);
                p.controlClause(false, s.Init, s.Tag, nil);
-               p.print(blank);
                p.switchBlock(s.Body);
                optSemi = true;
 
@@ -1077,7 +1082,6 @@ func (p *printer) stmt(stmt ast.Stmt) (optSemi bool) {
        case *ast.ForStmt:
                p.print(token.FOR);
                p.controlClause(true, s.Init, s.Cond, s.Post);
-               p.print(blank);
                p.block(s.Body);
                optSemi = true;
 
index b71c79124d4725f54911b2ff9c3b2397dba26041..91e3f2ec35381030fdea20d61b2be7cfcb1c70c4 100644 (file)
@@ -103,6 +103,7 @@ var data = []entry{
        entry{ "linebreaks.go", "linebreaks.golden", false },
        entry{ "expressions.go", "expressions.golden", false },
        entry{ "declarations.go", "declarations.golden", false },
+       entry{ "statements.go", "statements.golden", false },
 }
 
 
diff --git a/src/pkg/go/printer/testdata/statements.go b/src/pkg/go/printer/testdata/statements.go
new file mode 100644 (file)
index 0000000..b568bbf
--- /dev/null
@@ -0,0 +1,37 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package statements
+
+var expr bool;
+
+func _() {
+       if {}
+       if expr{}
+       if _:=expr;{}
+       if _:=expr; expr {}
+}
+
+
+func _() {
+       switch {}
+       switch expr {}
+       switch _ := expr; {}
+       switch _ := expr; expr {}
+}
+
+
+func _() {
+       for{}
+       for expr {}
+       for;;{}  // TODO ok to lose the semicolons here?
+       for _ :=expr;; {}
+       for; expr;{}  // TODO ok to lose the semicolons here?
+       for; ; expr = false {}
+       for _ :=expr; expr; {}
+       for _ := expr;; expr=false {}
+       for;expr;expr =false {}
+       for _ := expr;expr;expr = false {}
+       for _ := range []int{} {}
+}
diff --git a/src/pkg/go/printer/testdata/statements.golden b/src/pkg/go/printer/testdata/statements.golden
new file mode 100644 (file)
index 0000000..93f1064
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright 2009 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package statements
+
+var expr bool
+
+func _() {
+       if {}
+       if expr {}
+       if _ := expr; {}
+       if _ := expr; expr {}
+}
+
+func _() {
+       switch {}
+       switch expr {}
+       switch _ := expr; {}
+       switch _ := expr; expr {}
+}
+
+func _() {
+       for {}
+       for expr {}
+       for {}  // TODO ok to lose the semicolons here?
+       for _ := expr; ; {}
+       for expr {}     // TODO ok to lose the semicolons here?
+       for ; ; expr = false {}
+       for _ := expr; expr; {}
+       for _ := expr; ; expr = false {}
+       for ; expr; expr = false {}
+       for _ := expr; expr; expr = false {}
+       for _ := range []int{} {}
+}