]> Cypherpunks repositories - gostls13.git/commitdiff
- always format blocks with closing } on a new line, even if empty
authorRobert Griesemer <gri@golang.org>
Wed, 4 Nov 2009 21:31:20 +0000 (13:31 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 4 Nov 2009 21:31:20 +0000 (13:31 -0800)
- at the moment this also affects single-line function declarations
  because we have disabled them at the moment (but not single-line
  function literals)

R=rsc
http://go/go-review/1016040

src/pkg/go/printer/nodes.go
src/pkg/go/printer/testdata/comments.golden
src/pkg/go/printer/testdata/declarations.golden
src/pkg/go/printer/testdata/statements.golden

index 2aa0a5fbab1b6d3e3f77739db0021521dd51544d..23b27402ba7479d85618321d8c8fc104a11558ad 100644 (file)
@@ -614,13 +614,11 @@ func (p *printer) stmtList(list []ast.Stmt, _indent int) {
 }
 
 
-// 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);
 }
 
@@ -731,24 +729,25 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
                }
 
        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;
                        }
                }
 
@@ -766,7 +765,8 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
        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:
@@ -784,13 +784,14 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
                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:
@@ -810,13 +811,15 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
 
        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:
@@ -829,7 +832,8 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
                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:
@@ -963,9 +967,11 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool)
 
 
 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
@@ -1001,14 +1007,19 @@ func (p *printer) funcBody(b *ast.BlockStmt, isLit bool, multiLine *bool) {
                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;
 }
 
 
index 6719f199baf40e38e81f13f63a4bc7bcedffd093..2f4fb24071baee5a55d8426fc0beba36d3b7a875 100644 (file)
@@ -111,9 +111,11 @@ func typeswitch(x interface{}) {
        default:
        }
 
-       switch x.(type) {}
+       switch x.(type) {
+       }
 
-       switch v0, ok := x.(int); v := x.(type) {}
+       switch v0, ok := x.(int); v := x.(type) {
+       }
 
        switch v0, ok := x.(int); x.(type) {
        case bool, int, float:
index 2adf7b5fe4ac73c657e49ff848d86eaa65be350f..2071543c52b6f836d93e12439f64bb1d8366a77f 100644 (file)
@@ -445,13 +445,19 @@ func _() {
 
 
 // formatting of consecutive single-line functions
-func _() {}
-func _() {}
-func _() {}
+func _() {
+}
+func _() {
+}
+func _() {
+}
 
-func _() {}    // an empty line before this function
-func _() {}
-func _() {}
+func _() {
+}      // an empty line before this function
+func _() {
+}
+func _() {
+}
 
 func _() {
        f(1, 2, 3);
index 732be8683bd5a2b552354beebcbe61355b0fd024..a1839aa186412f911f34c2bf690b36d34aaf044f 100644 (file)
@@ -6,16 +6,23 @@ package statements
 
 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);
        }
@@ -27,12 +34,18 @@ func _() {
 
 // 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(
@@ -47,7 +60,8 @@ func _() {
 
 // Formatting of switch statement bodies.
 func _() {
-       switch {}
+       switch {
+       }
 
        switch x := 0; x {
        case 1:
@@ -100,23 +114,31 @@ func _() {
 
 // 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);
        }