Commit 10242e80 authored by Robert Griesemer's avatar Robert Griesemer

gofmt'ed parts of go

R=rsc
http://go/go-review/1023001
parent 65063bc6
...@@ -18,9 +18,9 @@ import ( ...@@ -18,9 +18,9 @@ import (
// Disabled formatting - enable eventually and remove the flag. // Disabled formatting - enable eventually and remove the flag.
const ( const (
oneLineFuncDecls = false; oneLineFuncDecls = false;
compositeLitBlank = false; compositeLitBlank = false;
stringListMode = exprListMode(0); // previously: noIndent stringListMode = exprListMode(0); // previously: noIndent
) )
...@@ -37,8 +37,10 @@ const ( ...@@ -37,8 +37,10 @@ const (
func (p *printer) linebreak(line, min, max int, ws whiteSpace, newSection bool) (printedBreak bool) { func (p *printer) linebreak(line, min, max int, ws whiteSpace, newSection bool) (printedBreak bool) {
n := line - p.pos.Line; n := line - p.pos.Line;
switch { switch {
case n < min: n = min; case n < min:
case n > max: n = max; n = min;
case n > max:
n = max;
} }
if n > 0 { if n > 0 {
p.print(ws); p.print(ws);
...@@ -118,12 +120,13 @@ func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) { ...@@ -118,12 +120,13 @@ func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) {
} }
type exprListMode uint; type exprListMode uint
const ( const (
blankStart exprListMode = 1 << iota; // print a blank before the list blankStart exprListMode = 1<<iota; // print a blank before the list
commaSep; // elements are separated by commas commaSep; // elements are separated by commas
commaTerm; // elements are terminated by comma commaTerm; // elements are terminated by comma
noIndent; // no extra indentation in multi-line lists noIndent; // no extra indentation in multi-line lists
) )
...@@ -150,7 +153,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo ...@@ -150,7 +153,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo
// all list entries on a single line // all list entries on a single line
for i, x := range list { for i, x := range list {
if i > 0 { if i > 0 {
if mode & commaSep != 0 { if mode&commaSep != 0 {
p.print(token.COMMA); p.print(token.COMMA);
} }
p.print(blank); p.print(blank);
...@@ -179,7 +182,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo ...@@ -179,7 +182,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo
prev := line; prev := line;
line = x.Pos().Line; line = x.Pos().Line;
if i > 0 { if i > 0 {
if mode & commaSep != 0 { if mode&commaSep != 0 {
p.print(token.COMMA); p.print(token.COMMA);
} }
if prev < line { if prev < line {
...@@ -200,7 +203,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo ...@@ -200,7 +203,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, mode exprListMo
// expression list - be conservative and check anyway // expression list - be conservative and check anyway
p.print(unindent); p.print(unindent);
} }
p.print(formfeed); // terminating comma needs a line break to look good p.print(formfeed); // terminating comma needs a line break to look good
} else if ws == ignore && mode&noIndent == 0 { } else if ws == ignore && mode&noIndent == 0 {
p.print(unindent); p.print(unindent);
} }
...@@ -304,7 +307,7 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok ...@@ -304,7 +307,7 @@ func (p *printer) fieldList(lbrace token.Position, list []*ast.Field, rbrace tok
p.print("// contains unexported fields"); p.print("// contains unexported fields");
} }
} else { // interface } else { // interface
var ml bool; var ml bool;
for i, f := range list { for i, f := range list {
...@@ -353,7 +356,7 @@ func needsBlanks(expr ast.Expr) bool { ...@@ -353,7 +356,7 @@ func needsBlanks(expr ast.Expr) bool {
return false; return false;
case *ast.IndexExpr: case *ast.IndexExpr:
// index expressions don't need blanks if the indexed expressions are simple // index expressions don't need blanks if the indexed expressions are simple
return needsBlanks(x.X) return needsBlanks(x.X);
case *ast.CallExpr: case *ast.CallExpr:
// call expressions need blanks if they have more than one // call expressions need blanks if they have more than one
// argument or if the function expression needs blanks // argument or if the function expression needs blanks
...@@ -539,7 +542,7 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, multiLine *bool) (optSemi bool ...@@ -539,7 +542,7 @@ func (p *printer) expr1(expr ast.Expr, prec1 int, multiLine *bool) (optSemi bool
p.print(blank); p.print(blank);
} }
p.print(x.Lbrace, token.LBRACE); p.print(x.Lbrace, token.LBRACE);
p.exprList(x.Lbrace, x.Elts, commaSep|commaTerm, multiLine); p.exprList(x.Lbrace, x.Elts, commaSep | commaTerm, multiLine);
p.print(x.Rbrace, token.RBRACE); p.print(x.Rbrace, token.RBRACE);
case *ast.Ellipsis: case *ast.Ellipsis:
...@@ -603,7 +606,7 @@ func (p *printer) expr(x ast.Expr, multiLine *bool) (optSemi bool) { ...@@ -603,7 +606,7 @@ func (p *printer) expr(x ast.Expr, multiLine *bool) (optSemi bool) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Statements // Statements
const maxStmtNewlines = 2 // maximum number of newlines between statements const maxStmtNewlines = 2 // maximum number of newlines between statements
// Print the statement list indented, but without a newline after the last statement. // Print the statement list indented, but without a newline after the last statement.
// Extra line breaks between statements in the source are respected but at most one // Extra line breaks between statements in the source are respected but at most one
...@@ -695,7 +698,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) { ...@@ -695,7 +698,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
case *ast.DeclStmt: case *ast.DeclStmt:
p.decl(s.Decl, inStmtList, multiLine); p.decl(s.Decl, inStmtList, multiLine);
optSemi = true; // decl prints terminating semicolon if necessary optSemi = true; // decl prints terminating semicolon if necessary
case *ast.EmptyStmt: case *ast.EmptyStmt:
// nothing to do // nothing to do
...@@ -775,7 +778,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) { ...@@ -775,7 +778,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
} }
p.print(s.Colon, token.COLON); p.print(s.Colon, token.COLON);
p.stmtList(s.Body, 1); p.stmtList(s.Body, 1);
optSemi = true; // "block" without {}'s optSemi = true; // "block" without {}'s
case *ast.SwitchStmt: case *ast.SwitchStmt:
p.print(token.SWITCH); p.print(token.SWITCH);
...@@ -793,7 +796,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) { ...@@ -793,7 +796,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
} }
p.print(s.Colon, token.COLON); p.print(s.Colon, token.COLON);
p.stmtList(s.Body, 1); p.stmtList(s.Body, 1);
optSemi = true; // "block" without {}'s optSemi = true; // "block" without {}'s
case *ast.TypeSwitchStmt: case *ast.TypeSwitchStmt:
p.print(token.SWITCH); p.print(token.SWITCH);
...@@ -822,7 +825,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) { ...@@ -822,7 +825,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
} }
p.print(s.Colon, token.COLON); p.print(s.Colon, token.COLON);
p.stmtList(s.Body, 1); p.stmtList(s.Body, 1);
optSemi = true; // "block" without {}'s optSemi = true; // "block" without {}'s
case *ast.SelectStmt: case *ast.SelectStmt:
p.print(token.SELECT, blank); p.print(token.SELECT, blank);
...@@ -862,9 +865,10 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) { ...@@ -862,9 +865,10 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Declarations // Declarations
type declContext uint; type declContext uint
const ( const (
atTop declContext = iota; atTop declContext = iota;
inGroup; inGroup;
inStmtList; inStmtList;
) )
...@@ -876,9 +880,9 @@ const ( ...@@ -876,9 +880,9 @@ const (
// //
func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *bool) { func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *bool) {
var ( var (
optSemi bool; // true if a semicolon is optional optSemi bool; // true if a semicolon is optional
comment *ast.CommentGroup; // a line comment, if any comment *ast.CommentGroup; // a line comment, if any
extraTabs int; // number of extra tabs before comment, if any extraTabs int; // number of extra tabs before comment, if any
) )
switch s := spec.(type) { switch s := spec.(type) {
...@@ -893,7 +897,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo ...@@ -893,7 +897,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo
case *ast.ValueSpec: case *ast.ValueSpec:
p.leadComment(s.Doc); p.leadComment(s.Doc);
p.identList(s.Names, multiLine); // always present p.identList(s.Names, multiLine); // always present
if n == 1 { if n == 1 {
if s.Type != nil { if s.Type != nil {
p.print(blank); p.print(blank);
...@@ -984,25 +988,25 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool) ...@@ -984,25 +988,25 @@ func (p *printer) genDecl(d *ast.GenDecl, context declContext, multiLine *bool)
func (p *printer) isOneLiner(b *ast.BlockStmt) bool { func (p *printer) isOneLiner(b *ast.BlockStmt) bool {
switch { switch {
case len(b.List) > 1 || p.commentBefore(b.Rbrace): case len(b.List) > 1 || p.commentBefore(b.Rbrace):
return false; // too many statements or there is a comment - all bets are off return false; // too many statements or there is a comment - all bets are off
case len(b.List) == 0: case len(b.List) == 0:
return true; // empty block and no comments return true; // empty block and no comments
} }
// test-print the statement and see if it would fit // test-print the statement and see if it would fit
var buf bytes.Buffer; var buf bytes.Buffer;
_, err := p.Config.Fprint(&buf, b.List[0]); _, err := p.Config.Fprint(&buf, b.List[0]);
if err != nil { if err != nil {
return false; // don't try return false; // don't try
} }
if buf.Len() > 40 { if buf.Len() > 40 {
return false; // too long return false; // too long
} }
for _, ch := range buf.Bytes() { for _, ch := range buf.Bytes() {
if ch < ' ' { if ch < ' ' {
return false; // contains control chars (tabs, newlines) return false; // contains control chars (tabs, newlines)
} }
} }
...@@ -1075,7 +1079,7 @@ func (p *printer) decl(decl ast.Decl, context declContext, multiLine *bool) { ...@@ -1075,7 +1079,7 @@ func (p *printer) decl(decl ast.Decl, context declContext, multiLine *bool) {
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Files // Files
const maxDeclNewlines = 3 // maximum number of newlines between declarations const maxDeclNewlines = 3 // maximum number of newlines between declarations
func declToken(decl ast.Decl) (tok token.Token) { func declToken(decl ast.Decl) (tok token.Token) {
tok = token.ILLEGAL; tok = token.ILLEGAL;
......
This diff is collapsed.
...@@ -16,12 +16,12 @@ import ( ...@@ -16,12 +16,12 @@ import (
const ( const (
dataDir = "testdata"; dataDir = "testdata";
tabwidth = 8; tabwidth = 8;
) )
var update = flag.Bool("update", false, "update golden files"); var update = flag.Bool("update", false, "update golden files")
func lineString(text []byte, i int) string { func lineString(text []byte, i int) string {
...@@ -29,13 +29,14 @@ func lineString(text []byte, i int) string { ...@@ -29,13 +29,14 @@ func lineString(text []byte, i int) string {
for i < len(text) && text[i] != '\n' { for i < len(text) && text[i] != '\n' {
i++; i++;
} }
return string(text[i0 : i]); return string(text[i0:i]);
} }
type checkMode uint; type checkMode uint
const ( const (
export checkMode = 1<<iota; export checkMode = 1<<iota;
rawFormat; rawFormat;
) )
...@@ -50,13 +51,13 @@ func check(t *testing.T, source, golden string, mode checkMode) { ...@@ -50,13 +51,13 @@ func check(t *testing.T, source, golden string, mode checkMode) {
// filter exports if necessary // filter exports if necessary
if mode&export != 0 { if mode&export != 0 {
ast.FileExports(prog); // ignore result ast.FileExports(prog); // ignore result
prog.Comments = nil; // don't print comments that are not in AST prog.Comments = nil; // don't print comments that are not in AST
} }
// determine printer configuration // determine printer configuration
cfg := Config{Tabwidth: tabwidth}; cfg := Config{Tabwidth: tabwidth};
if mode&rawFormat != 0 { if mode & rawFormat != 0 {
cfg.Mode |= RawFormat; cfg.Mode |= RawFormat;
} }
...@@ -105,20 +106,20 @@ func check(t *testing.T, source, golden string, mode checkMode) { ...@@ -105,20 +106,20 @@ func check(t *testing.T, source, golden string, mode checkMode) {
type entry struct { type entry struct {
source, golden string; source, golden string;
mode checkMode; mode checkMode;
} }
// Use gotest -update to create/update the respective golden files. // Use gotest -update to create/update the respective golden files.
var data = []entry{ var data = []entry{
entry{ "empty.input", "empty.golden", 0 }, entry{"empty.input", "empty.golden", 0},
entry{ "comments.input", "comments.golden", 0 }, entry{"comments.input", "comments.golden", 0},
entry{ "comments.input", "comments.x", export }, entry{"comments.input", "comments.x", export},
entry{ "linebreaks.input", "linebreaks.golden", 0 }, entry{"linebreaks.input", "linebreaks.golden", 0},
entry{ "expressions.input", "expressions.golden", 0 }, entry{"expressions.input", "expressions.golden", 0},
entry{ "expressions.input", "expressions.raw", rawFormat }, entry{"expressions.input", "expressions.raw", rawFormat},
entry{ "declarations.input", "declarations.golden", 0 }, entry{"declarations.input", "declarations.golden", 0},
entry{ "statements.input", "statements.golden", 0 }, entry{"statements.input", "statements.golden", 0},
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment