From: Robert Griesemer Date: Sat, 7 Nov 2009 21:01:52 +0000 (-0800) Subject: - blank before opening { for multi-line composite literals (as preferred by r) X-Git-Tag: weekly.2009-11-10~74 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=3317697df4ae2d0e97dc06fb08adad7fd7e90adf;p=gostls13.git - blank before opening { for multi-line composite literals (as preferred by r) - blank padding around composite literal contents for a less dense look (most if not all composite literals were written in that style before gofmt ran through them) - corresponding (internal) flag: compositeLitBlank - don't print a semi after a one-statement statement list (as preferred by r) - corresponding (internal flag): fewerSemis - the number of changes in nodes.go is huge because of the removed semis; hg mail requires that I gofmt the file before With both flags set to false, this CL causes no gofmt formatting change. We can turn them on if we like it (and then remove the flags). Will submit with flags disabled. R=rsc, r http://go/go-review/1025015 --- diff --git a/src/pkg/go/printer/nodes.go b/src/pkg/go/printer/nodes.go index 7ffb73e695..1f863c24b3 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); } }