1
0
mirror of https://github.com/golang/go synced 2024-11-24 21:20:05 -07:00

go/printer, gofmt: simplify struct formatting and respect line breaks

Also: gofmt src misc

Fixes #1627.

R=rsc
CC=golang-dev
https://golang.org/cl/4303042
This commit is contained in:
Robert Griesemer 2011-03-22 11:05:26 -07:00
parent 169e6d40e3
commit 6684d5503a
7 changed files with 28 additions and 46 deletions

View File

@ -29,7 +29,7 @@ func goCallback(p unsafe.Pointer) {
func TestCallback(t *testing.T) { func TestCallback(t *testing.T) {
var x = false var x = false
nestedCall(func(){x = true}) nestedCall(func() { x = true })
if !x { if !x {
t.Fatal("nestedCall did not call func") t.Fatal("nestedCall did not call func")
} }
@ -39,7 +39,7 @@ func TestCallbackGC(t *testing.T) {
nestedCall(runtime.GC) nestedCall(runtime.GC)
} }
func lockedOSThread() bool // in runtime.c func lockedOSThread() bool // in runtime.c
func TestCallbackPanic(t *testing.T) { func TestCallbackPanic(t *testing.T) {
// Make sure panic during callback unwinds properly. // Make sure panic during callback unwinds properly.
@ -58,7 +58,7 @@ func TestCallbackPanic(t *testing.T) {
t.Fatal("locked OS thread on exit from TestCallbackPanic") t.Fatal("locked OS thread on exit from TestCallbackPanic")
} }
}() }()
nestedCall(func(){panic("callback panic")}) nestedCall(func() { panic("callback panic") })
panic("nestedCall returned") panic("nestedCall returned")
} }
@ -88,7 +88,7 @@ func TestCallbackPanicLocked(t *testing.T) {
t.Fatal("lost lock on OS thread after panic") t.Fatal("lost lock on OS thread after panic")
} }
}() }()
nestedCall(func(){panic("callback panic")}) nestedCall(func() { panic("callback panic") })
panic("nestedCall returned") panic("nestedCall returned")
} }
@ -125,7 +125,7 @@ func TestBlocking(t *testing.T) {
c <- <-c c <- <-c
} }
}() }()
nestedCall(func(){ nestedCall(func() {
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
c <- i c <- i
if j := <-c; j != i { if j := <-c; j != i {

View File

@ -3,4 +3,3 @@ package cgotest
// dummy file so gotest thinks there are tests. // dummy file so gotest thinks there are tests.
// the actual tests are in the main go files, next // the actual tests are in the main go files, next
// to the code they test. // to the code they test.

View File

@ -28,8 +28,8 @@ func parallelSleep(n int) {
} }
//export BackgroundSleep //export BackgroundSleep
func BackgroundSleep(n int){ func BackgroundSleep(n int) {
go func(){ go func() {
C.sleep(C.uint(n)) C.sleep(C.uint(n))
sleepDone <- true sleepDone <- true
}() }()

View File

@ -367,7 +367,7 @@ func (p *printer) setLineComment(text string) {
} }
func (p *printer) fieldList(fields *ast.FieldList, isIncomplete bool, ctxt exprContext) { func (p *printer) fieldList(fields *ast.FieldList, isStruct, isIncomplete bool) {
p.nesting++ p.nesting++
defer func() { defer func() {
p.nesting-- p.nesting--
@ -376,15 +376,15 @@ func (p *printer) fieldList(fields *ast.FieldList, isIncomplete bool, ctxt exprC
lbrace := fields.Opening lbrace := fields.Opening
list := fields.List list := fields.List
rbrace := fields.Closing rbrace := fields.Closing
srcIsOneLine := lbrace.IsValid() && rbrace.IsValid() && p.fset.Position(lbrace).Line == p.fset.Position(rbrace).Line
if !isIncomplete && !p.commentBefore(p.fset.Position(rbrace)) { if !isIncomplete && !p.commentBefore(p.fset.Position(rbrace)) && srcIsOneLine {
// possibly a one-line struct/interface // possibly a one-line struct/interface
if len(list) == 0 { if len(list) == 0 {
// no blank between keyword and {} in this case // no blank between keyword and {} in this case
p.print(lbrace, token.LBRACE, rbrace, token.RBRACE) p.print(lbrace, token.LBRACE, rbrace, token.RBRACE)
return return
} else if ctxt&(compositeLit|structType) == compositeLit|structType && } else if isStruct && p.isOneLineFieldList(list) { // for now ignore interfaces
p.isOneLineFieldList(list) { // for now ignore interfaces
// small enough - print on one line // small enough - print on one line
// (don't use identList and ignore source line breaks) // (don't use identList and ignore source line breaks)
p.print(lbrace, token.LBRACE, blank) p.print(lbrace, token.LBRACE, blank)
@ -406,7 +406,7 @@ func (p *printer) fieldList(fields *ast.FieldList, isIncomplete bool, ctxt exprC
// at least one entry or incomplete // at least one entry or incomplete
p.print(blank, lbrace, token.LBRACE, indent, formfeed) p.print(blank, lbrace, token.LBRACE, indent, formfeed)
if ctxt&structType != 0 { if isStruct {
sep := vtab sep := vtab
if len(list) == 1 { if len(list) == 1 {
@ -489,15 +489,6 @@ func (p *printer) fieldList(fields *ast.FieldList, isIncomplete bool, ctxt exprC
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// Expressions // Expressions
// exprContext describes the syntactic environment in which an expression node is printed.
type exprContext uint
const (
compositeLit exprContext = 1 << iota
structType
)
func walkBinary(e *ast.BinaryExpr) (has4, has5 bool, maxProblem int) { func walkBinary(e *ast.BinaryExpr) (has4, has5 bool, maxProblem int) {
switch e.Op.Precedence() { switch e.Op.Precedence() {
case 4: case 4:
@ -642,7 +633,7 @@ func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int, multiL
printBlank := prec < cutoff printBlank := prec < cutoff
ws := indent ws := indent
p.expr1(x.X, prec, depth+diffPrec(x.X, prec), 0, multiLine) p.expr1(x.X, prec, depth+diffPrec(x.X, prec), multiLine)
if printBlank { if printBlank {
p.print(blank) p.print(blank)
} }
@ -661,7 +652,7 @@ func (p *printer) binaryExpr(x *ast.BinaryExpr, prec1, cutoff, depth int, multiL
if printBlank { if printBlank {
p.print(blank) p.print(blank)
} }
p.expr1(x.Y, prec+1, depth+1, 0, multiLine) p.expr1(x.Y, prec+1, depth+1, multiLine)
if ws == ignore { if ws == ignore {
p.print(unindent) p.print(unindent)
} }
@ -734,7 +725,7 @@ func selectorExprList(expr ast.Expr) (list []ast.Expr) {
// Sets multiLine to true if the expression spans multiple lines. // Sets multiLine to true if the expression spans multiple lines.
func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multiLine *bool) { func (p *printer) expr1(expr ast.Expr, prec1, depth int, multiLine *bool) {
p.print(expr.Pos()) p.print(expr.Pos())
switch x := expr.(type) { switch x := expr.(type) {
@ -784,7 +775,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
// TODO(gri) Remove this code if it cannot be reached. // TODO(gri) Remove this code if it cannot be reached.
p.print(blank) p.print(blank)
} }
p.expr1(x.X, prec, depth, 0, multiLine) p.expr1(x.X, prec, depth, multiLine)
} }
case *ast.BasicLit: case *ast.BasicLit:
@ -810,7 +801,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
p.exprList(token.NoPos, parts, depth, periodSep, multiLine, token.NoPos) p.exprList(token.NoPos, parts, depth, periodSep, multiLine, token.NoPos)
case *ast.TypeAssertExpr: case *ast.TypeAssertExpr:
p.expr1(x.X, token.HighestPrec, depth, 0, multiLine) p.expr1(x.X, token.HighestPrec, depth, multiLine)
p.print(token.PERIOD, token.LPAREN) p.print(token.PERIOD, token.LPAREN)
if x.Type != nil { if x.Type != nil {
p.expr(x.Type, multiLine) p.expr(x.Type, multiLine)
@ -821,14 +812,14 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
case *ast.IndexExpr: case *ast.IndexExpr:
// TODO(gri): should treat[] like parentheses and undo one level of depth // TODO(gri): should treat[] like parentheses and undo one level of depth
p.expr1(x.X, token.HighestPrec, 1, 0, multiLine) p.expr1(x.X, token.HighestPrec, 1, multiLine)
p.print(x.Lbrack, token.LBRACK) p.print(x.Lbrack, token.LBRACK)
p.expr0(x.Index, depth+1, multiLine) p.expr0(x.Index, depth+1, multiLine)
p.print(x.Rbrack, token.RBRACK) p.print(x.Rbrack, token.RBRACK)
case *ast.SliceExpr: case *ast.SliceExpr:
// TODO(gri): should treat[] like parentheses and undo one level of depth // TODO(gri): should treat[] like parentheses and undo one level of depth
p.expr1(x.X, token.HighestPrec, 1, 0, multiLine) p.expr1(x.X, token.HighestPrec, 1, multiLine)
p.print(x.Lbrack, token.LBRACK) p.print(x.Lbrack, token.LBRACK)
if x.Low != nil { if x.Low != nil {
p.expr0(x.Low, depth+1, multiLine) p.expr0(x.Low, depth+1, multiLine)
@ -848,7 +839,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
if len(x.Args) > 1 { if len(x.Args) > 1 {
depth++ depth++
} }
p.expr1(x.Fun, token.HighestPrec, depth, 0, multiLine) p.expr1(x.Fun, token.HighestPrec, depth, multiLine)
p.print(x.Lparen, token.LPAREN) p.print(x.Lparen, token.LPAREN)
p.exprList(x.Lparen, x.Args, depth, commaSep|commaTerm, multiLine, x.Rparen) p.exprList(x.Lparen, x.Args, depth, commaSep|commaTerm, multiLine, x.Rparen)
if x.Ellipsis.IsValid() { if x.Ellipsis.IsValid() {
@ -859,7 +850,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
case *ast.CompositeLit: case *ast.CompositeLit:
// composite literal elements that are composite literals themselves may have the type omitted // composite literal elements that are composite literals themselves may have the type omitted
if x.Type != nil { if x.Type != nil {
p.expr1(x.Type, token.HighestPrec, depth, compositeLit, multiLine) p.expr1(x.Type, token.HighestPrec, depth, multiLine)
} }
p.print(x.Lbrace, token.LBRACE) p.print(x.Lbrace, token.LBRACE)
p.exprList(x.Lbrace, x.Elts, 1, commaSep|commaTerm, multiLine, x.Rbrace) p.exprList(x.Lbrace, x.Elts, 1, commaSep|commaTerm, multiLine, x.Rbrace)
@ -884,7 +875,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
case *ast.StructType: case *ast.StructType:
p.print(token.STRUCT) p.print(token.STRUCT)
p.fieldList(x.Fields, x.Incomplete, ctxt|structType) p.fieldList(x.Fields, true, x.Incomplete)
case *ast.FuncType: case *ast.FuncType:
p.print(token.FUNC) p.print(token.FUNC)
@ -892,7 +883,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
case *ast.InterfaceType: case *ast.InterfaceType:
p.print(token.INTERFACE) p.print(token.INTERFACE)
p.fieldList(x.Methods, x.Incomplete, ctxt) p.fieldList(x.Methods, false, x.Incomplete)
case *ast.MapType: case *ast.MapType:
p.print(token.MAP, token.LBRACK) p.print(token.MAP, token.LBRACK)
@ -921,14 +912,14 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
func (p *printer) expr0(x ast.Expr, depth int, multiLine *bool) { func (p *printer) expr0(x ast.Expr, depth int, multiLine *bool) {
p.expr1(x, token.LowestPrec, depth, 0, multiLine) p.expr1(x, token.LowestPrec, depth, multiLine)
} }
// Sets multiLine to true if the expression spans multiple lines. // Sets multiLine to true if the expression spans multiple lines.
func (p *printer) expr(x ast.Expr, multiLine *bool) { func (p *printer) expr(x ast.Expr, multiLine *bool) {
const depth = 1 const depth = 1
p.expr1(x, token.LowestPrec, depth, 0, multiLine) p.expr1(x, token.LowestPrec, depth, multiLine)
} }

View File

@ -224,11 +224,7 @@ func _() {
_ = struct{ x int }{0} _ = struct{ x int }{0}
_ = struct{ x, y, z int }{0, 1, 2} _ = struct{ x, y, z int }{0, 1, 2}
_ = struct{ int }{0} _ = struct{ int }{0}
_ = struct { _ = struct{ s struct{ int } }{struct{ int }{0}}
s struct {
int
}
}{struct{ int }{0}} // compositeLit context not propagated => multiLine result
} }

View File

@ -224,7 +224,7 @@ func _() {
_ = struct{ x int }{0} _ = struct{ x int }{0}
_ = struct{ x, y, z int }{0, 1, 2} _ = struct{ x, y, z int }{0, 1, 2}
_ = struct{ int }{0} _ = struct{ int }{0}
_ = struct{ s struct { int } }{struct{ int}{0}} // compositeLit context not propagated => multiLine result _ = struct{ s struct { int } }{struct{ int}{0} }
} }

View File

@ -224,11 +224,7 @@ func _() {
_ = struct{ x int }{0} _ = struct{ x int }{0}
_ = struct{ x, y, z int }{0, 1, 2} _ = struct{ x, y, z int }{0, 1, 2}
_ = struct{ int }{0} _ = struct{ int }{0}
_ = struct { _ = struct{ s struct{ int } }{struct{ int }{0}}
s struct {
int
}
}{struct{ int }{0}} // compositeLit context not propagated => multiLine result
} }