diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go index 7ffb73e695b..1f863c24b36 100644 --- a/src/pkg/go/printer/nodes.go +++ b/src/pkg/go/printer/nodes.go @@ -19,6 +19,7 @@ import ( // Disabled formatting - enable eventually and remove the flag. const ( compositeLitBlank = false; + fewerSemis = false; stringListMode = exprListMode(0); // previously: noIndent ) @@ -133,7 +134,8 @@ func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) { type exprListMode uint const ( - blankStart exprListMode = 1< 0 { - size += 2; // ", " + size += 2 // ", " + ; + } size += len(x.Value); if size >= maxSize { @@ -287,7 +302,9 @@ func (p *printer) isOneLineFieldList(list []*ast.Field) bool { const maxSize = 30; // adjust as appropriate, this is an approximate value namesSize := identListSize(f.Names, maxSize); if namesSize > 0 { - namesSize = 1; // blank between names and types + namesSize = 1 // blank between names and types + ; + } typeSize := p.nodeSize(f.Type, maxSize); return namesSize + typeSize <= maxSize; @@ -609,15 +626,21 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, ctxt Context, multiLine *bool) case *ast.CompositeLit: p.expr1(x.Type, token.HighestPrec, compositeLit, multiLine); - if compositeLitBlank && x.Lbrace.Line < x.Rbrace.Line { - // add a blank before the opening { for multi-line composites - // TODO(gri): for now this decision is made by looking at the - // source code - it may not be correct if the source - // code was badly misformatted in the first place - p.print(blank); + mode := commaSep | commaTerm; + if compositeLitBlank { + // add blank padding around composite literal + // contents for a less dense look + mode |= blankStart | blankEnd; + if x.Lbrace.Line < x.Rbrace.Line { + // add a blank before the opening { for multi-line composites + // TODO(gri): for now this decision is made by looking at the + // source code - it may not be correct if the source + // code was badly misformatted in the first place + p.print(blank); + } } p.print(x.Lbrace, token.LBRACE); - p.exprList(x.Lbrace, x.Elts, commaSep | commaTerm, multiLine); + p.exprList(x.Lbrace, x.Elts, mode, multiLine); p.print(x.Rbrace, token.RBRACE); case *ast.Ellipsis: @@ -697,7 +720,7 @@ func (p *printer) stmtList(list []ast.Stmt, _indent int) { // in those cases each clause is a new section p.linebreak(s.Pos().Line, 1, maxStmtNewlines, ignore, i == 0 || _indent == 0 || multiLine); multiLine = false; - if !p.stmt(s, &multiLine) { + if !p.stmt(s, &multiLine) && (!fewerSemis || len(list) > 1) { p.print(token.SEMICOLON); } }