}
-// Sets multiLine to true if the block spans multiple lines.
-func (p *printer) block(s *ast.BlockStmt, indent int, multiLine *bool) {
+// block prints an *ast.BlockStmt; it always spans at least two lines.
+func (p *printer) block(s *ast.BlockStmt, indent int) {
p.print(s.Pos(), token.LBRACE);
- if len(s.List) > 0 || p.commentBefore(s.Rbrace) {
- p.stmtList(s.List, indent);
- p.linebreak(s.Rbrace.Line, 1, maxStmtNewlines, ignore, true);
- }
+ p.stmtList(s.List, indent);
+ p.linebreak(s.Rbrace.Line, 1, maxStmtNewlines, ignore, true);
p.print(s.Rbrace, token.RBRACE);
}
}
case *ast.BlockStmt:
- p.block(s, 1, multiLine);
+ p.block(s, 1);
+ *multiLine = true;
optSemi = true;
case *ast.IfStmt:
p.print(token.IF);
p.controlClause(false, s.Init, s.Cond, nil);
- p.block(s.Body, 1, multiLine);
+ p.block(s.Body, 1);
+ *multiLine = true;
optSemi = true;
if s.Else != nil {
p.print(blank, token.ELSE, blank);
switch s.Else.(type) {
case *ast.BlockStmt, *ast.IfStmt:
- optSemi = p.stmt(s.Else, multiLine);
+ optSemi = p.stmt(s.Else, ignoreMultiLine);
default:
p.print(token.LBRACE, indent, formfeed);
p.stmt(s.Else, ignoreMultiLine);
p.print(unindent, formfeed, token.RBRACE);
- *multiLine = true;
}
}
case *ast.SwitchStmt:
p.print(token.SWITCH);
p.controlClause(false, s.Init, s.Tag, nil);
- p.block(s.Body, 0, multiLine);
+ p.block(s.Body, 0);
+ *multiLine = true;
optSemi = true;
case *ast.TypeCaseClause:
p.print(token.SWITCH);
if s.Init != nil {
p.print(blank);
- p.stmt(s.Init, multiLine);
+ p.stmt(s.Init, ignoreMultiLine);
p.print(token.SEMICOLON);
}
p.print(blank);
- p.stmt(s.Assign, multiLine);
+ p.stmt(s.Assign, ignoreMultiLine);
p.print(blank);
- p.block(s.Body, 0, multiLine);
+ p.block(s.Body, 0);
+ *multiLine = true;
optSemi = true;
case *ast.CommClause:
case *ast.SelectStmt:
p.print(token.SELECT, blank);
- p.block(s.Body, 0, multiLine);
+ p.block(s.Body, 0);
+ *multiLine = true;
optSemi = true;
case *ast.ForStmt:
p.print(token.FOR);
p.controlClause(true, s.Init, s.Cond, s.Post);
- p.block(s.Body, 1, multiLine);
+ p.block(s.Body, 1);
+ *multiLine = true;
optSemi = true;
case *ast.RangeStmt:
p.print(blank, s.TokPos, s.Tok, blank, token.RANGE, blank);
p.expr(s.X, multiLine);
p.print(blank);
- p.block(s.Body, 1, multiLine);
+ p.block(s.Body, 1);
+ *multiLine = true;
optSemi = true;
default:
func (p *printer) isOneLiner(b *ast.BlockStmt) bool {
- if len(b.List) != 1 || p.commentBefore(b.Rbrace) {
- // too many statements or there is a comment - all bets are off
- return false;
+ switch {
+ case len(b.List) > 1 || p.commentBefore(b.Rbrace):
+ return false; // too many statements or there is a comment - all bets are off
+ case len(b.List) == 0:
+ return true; // empty block and no comments
}
// test-print the statement and see if it would fit
if isLit {
sep = blank;
}
- p.print(sep, b.Pos(), token.LBRACE, blank);
- p.stmt(b.List[0], ignoreMultiLine);
- p.print(blank, b.Rbrace, token.RBRACE);
+ if len(b.List) > 0 {
+ p.print(sep, b.Pos(), token.LBRACE, blank);
+ p.stmt(b.List[0], ignoreMultiLine);
+ p.print(blank, b.Rbrace, token.RBRACE);
+ } else {
+ p.print(sep, b.Pos(), token.LBRACE, b.Rbrace, token.RBRACE);
+ }
return;
}
p.print(blank);
- p.block(b, 1, multiLine);
+ p.block(b, 1);
+ *multiLine = true;
}
var expr bool
-func use(x interface{}) {}
+func use(x interface{}) {
+}
// Formatting of if-statement headers.
func _() {
- if {}
- if {} // no semicolon printed
- if expr {}
- if expr {} // no semicolon printed
- if expr {} // no parens printed
- if expr {} // no semicolon and parens printed
+ if {
+ }
+ if {
+ } // no semicolon printed
+ if expr {
+ }
+ if expr {
+ } // no semicolon printed
+ if expr {
+ } // no parens printed
+ if expr {
+ } // no semicolon and parens printed
if x := expr; {
use(x);
}
// Formatting of switch-statement headers.
func _() {
- switch {}
- switch {} // no semicolon printed
- switch expr {}
- switch expr {} // no semicolon printed
- switch expr {} // no parens printed
- switch expr {} // no semicolon and parens printed
+ switch {
+ }
+ switch {
+ } // no semicolon printed
+ switch expr {
+ }
+ switch expr {
+ } // no semicolon printed
+ switch expr {
+ } // no parens printed
+ switch expr {
+ } // no semicolon and parens printed
switch x := expr; {
default:
use(
// Formatting of switch statement bodies.
func _() {
- switch {}
+ switch {
+ }
switch x := 0; x {
case 1:
// Formatting of for-statement headers.
func _() {
- for {}
- for expr {}
- for expr {} // no parens printed
- for {} // no semicolons printed
+ for {
+ }
+ for expr {
+ }
+ for expr {
+ } // no parens printed
+ for {
+ } // no semicolons printed
for x := expr; ; {
use(x);
}
- for expr {} // no semicolons printed
- for expr {} // no semicolons and parens printed
- for ; ; expr = false {}
+ for expr {
+ } // no semicolons printed
+ for expr {
+ } // no semicolons and parens printed
+ for ; ; expr = false {
+ }
for x := expr; expr; {
use(x);
}
for x := expr; ; expr = false {
use(x);
}
- for ; expr; expr = false {}
+ for ; expr; expr = false {
+ }
for x := expr; expr; expr = false {
use(x);
}