mirror of
https://github.com/golang/go
synced 2024-11-12 09:20:22 -07:00
improved spacing around if, switch, and for control clauses
R=r DELTA=89 (82 added, 5 deleted, 2 changed) OCL=34870 CL=34870
This commit is contained in:
parent
eae0906bed
commit
681f86a87f
@ -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;
|
||||
|
||||
|
@ -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 },
|
||||
}
|
||||
|
||||
|
||||
|
37
src/pkg/go/printer/testdata/statements.go
vendored
Normal file
37
src/pkg/go/printer/testdata/statements.go
vendored
Normal file
@ -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{} {}
|
||||
}
|
35
src/pkg/go/printer/testdata/statements.golden
vendored
Normal file
35
src/pkg/go/printer/testdata/statements.golden
vendored
Normal file
@ -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{} {}
|
||||
}
|
Loading…
Reference in New Issue
Block a user