From 2f792d32be161f61063e84edb3bb83182371cc68 Mon Sep 17 00:00:00 2001 From: Robert Griesemer Date: Wed, 4 Nov 2009 13:31:20 -0800 Subject: [PATCH] - always format blocks with closing } on a new line, even if empty - 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 | 59 ++++++++++------- src/pkg/go/printer/testdata/comments.golden | 6 +- .../go/printer/testdata/declarations.golden | 18 +++-- src/pkg/go/printer/testdata/statements.golden | 66 ++++++++++++------- 4 files changed, 95 insertions(+), 54 deletions(-) diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go index 2aa0a5fbab1..23b27402ba7 100644 --- a/src/pkg/go/printer/nodes.go +++ b/src/pkg/go/printer/nodes.go @@ -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; } diff --git a/src/pkg/go/printer/testdata/comments.golden b/src/pkg/go/printer/testdata/comments.golden index 6719f199baf..2f4fb24071b 100644 --- a/src/pkg/go/printer/testdata/comments.golden +++ b/src/pkg/go/printer/testdata/comments.golden @@ -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: diff --git a/src/pkg/go/printer/testdata/declarations.golden b/src/pkg/go/printer/testdata/declarations.golden index 2adf7b5fe4a..2071543c52b 100644 --- a/src/pkg/go/printer/testdata/declarations.golden +++ b/src/pkg/go/printer/testdata/declarations.golden @@ -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); diff --git a/src/pkg/go/printer/testdata/statements.golden b/src/pkg/go/printer/testdata/statements.golden index 732be8683bd..a1839aa1864 100644 --- a/src/pkg/go/printer/testdata/statements.golden +++ b/src/pkg/go/printer/testdata/statements.golden @@ -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); }