if i > 0 {
// use position of expression following the comma as
// comma position for correct comment placement
- p.printPos(x.Pos())
+ p.setPos(x.Pos())
p.print(token.COMMA, blank)
}
p.expr0(x, depth)
// comma position for correct comment placement, but
// only if the expression is on the same line.
if !needsLinebreak {
- p.printPos(x.Pos())
+ p.setPos(x.Pos())
}
p.print(token.COMMA)
needsBlank := true
// can align if possible.
// (needsLinebreak is set if we started a new line before)
p.expr(pair.Key)
- p.printPos(pair.Colon)
+ p.setPos(pair.Colon)
p.print(token.COLON, vtab)
p.expr(pair.Value)
} else {
if mode != funcParam {
openTok, closeTok = token.LBRACK, token.RBRACK
}
- p.printPos(fields.Opening)
+ p.setPos(fields.Opening)
p.print(openTok)
if len(fields.List) > 0 {
prevLine := p.lineFor(fields.Opening)
// comma position for correct comma placement, but
// only if the next parameter is on the same line
if !needsLinebreak {
- p.printPos(par.Pos())
+ p.setPos(par.Pos())
}
p.print(token.COMMA)
}
}
}
- p.printPos(fields.Closing)
+ p.setPos(fields.Closing)
p.print(closeTok)
}
// possibly a one-line struct/interface
if len(list) == 0 {
// no blank between keyword and {} in this case
- p.printPos(lbrace)
+ p.setPos(lbrace)
p.print(token.LBRACE)
- p.printPos(rbrace)
+ p.setPos(rbrace)
p.print(token.RBRACE)
return
} else if p.isOneLineFieldList(list) {
// small enough - print on one line
// (don't use identList and ignore source line breaks)
- p.printPos(lbrace)
+ p.setPos(lbrace)
p.print(token.LBRACE, blank)
f := list[0]
if isStruct {
}
}
p.print(blank)
- p.printPos(rbrace)
+ p.setPos(rbrace)
p.print(token.RBRACE)
return
}
// hasComments || !srcIsOneLine
p.print(blank)
- p.printPos(lbrace)
+ p.setPos(lbrace)
p.print(token.LBRACE, indent)
if hasComments || len(list) > 0 {
p.print(formfeed)
}
p.print(unindent, formfeed)
- p.printPos(rbrace)
+ p.setPos(rbrace)
p.print(token.RBRACE)
}
}
xline := p.pos.Line // before the operator (it may be on the next line!)
yline := p.lineFor(x.Y.Pos())
- p.printPos(x.OpPos)
+ p.setPos(x.OpPos)
p.print(x.Op)
if xline != yline && xline > 0 && yline > 0 {
// at least one line break, but respect an extra empty line
}
func (p *printer) expr1(expr ast.Expr, prec1, depth int) {
- p.printPos(expr.Pos())
+ p.setPos(expr.Pos())
switch x := expr.(type) {
case *ast.BadExpr:
case *ast.KeyValueExpr:
p.expr(x.Key)
- p.printPos(x.Colon)
+ p.setPos(x.Colon)
p.print(token.COLON, blank)
p.expr(x.Value)
p.print(x)
case *ast.FuncLit:
- p.printPos(x.Type.Pos())
+ p.setPos(x.Type.Pos())
p.print(token.FUNC)
// See the comment in funcDecl about how the header size is computed.
startCol := p.out.Column - len("func")
} else {
p.print(token.LPAREN)
p.expr0(x.X, reduceDepth(depth)) // parentheses undo one level of depth
- p.printPos(x.Rparen)
+ p.setPos(x.Rparen)
p.print(token.RPAREN)
}
case *ast.TypeAssertExpr:
p.expr1(x.X, token.HighestPrec, depth)
p.print(token.PERIOD)
- p.printPos(x.Lparen)
+ p.setPos(x.Lparen)
p.print(token.LPAREN)
if x.Type != nil {
p.expr(x.Type)
} else {
p.print(token.TYPE)
}
- p.printPos(x.Rparen)
+ p.setPos(x.Rparen)
p.print(token.RPAREN)
case *ast.IndexExpr:
// TODO(gri): should treat[] like parentheses and undo one level of depth
p.expr1(x.X, token.HighestPrec, 1)
- p.printPos(x.Lbrack)
+ p.setPos(x.Lbrack)
p.print(token.LBRACK)
p.expr0(x.Index, depth+1)
- p.printPos(x.Rbrack)
+ p.setPos(x.Rbrack)
p.print(token.RBRACK)
case *ast.IndexListExpr:
// TODO(gri): as for IndexExpr, should treat [] like parentheses and undo
// one level of depth
p.expr1(x.X, token.HighestPrec, 1)
- p.printPos(x.Lbrack)
+ p.setPos(x.Lbrack)
p.print(token.LBRACK)
p.exprList(x.Lbrack, x.Indices, depth+1, commaTerm, x.Rbrack, false)
- p.printPos(x.Rbrack)
+ p.setPos(x.Rbrack)
p.print(token.RBRACK)
case *ast.SliceExpr:
// TODO(gri): should treat[] like parentheses and undo one level of depth
p.expr1(x.X, token.HighestPrec, 1)
- p.printPos(x.Lbrack)
+ p.setPos(x.Lbrack)
p.print(token.LBRACK)
indices := []ast.Expr{x.Low, x.High}
if x.Max != nil {
p.expr0(x, depth+1)
}
}
- p.printPos(x.Rbrack)
+ p.setPos(x.Rbrack)
p.print(token.RBRACK)
case *ast.CallExpr:
} else {
wasIndented = p.possibleSelectorExpr(x.Fun, token.HighestPrec, depth)
}
- p.printPos(x.Lparen)
+ p.setPos(x.Lparen)
p.print(token.LPAREN)
if x.Ellipsis.IsValid() {
p.exprList(x.Lparen, x.Args, depth, 0, x.Ellipsis, false)
- p.printPos(x.Ellipsis)
+ p.setPos(x.Ellipsis)
p.print(token.ELLIPSIS)
if x.Rparen.IsValid() && p.lineFor(x.Ellipsis) < p.lineFor(x.Rparen) {
p.print(token.COMMA, formfeed)
} else {
p.exprList(x.Lparen, x.Args, depth, commaTerm, x.Rparen, false)
}
- p.printPos(x.Rparen)
+ p.setPos(x.Rparen)
p.print(token.RPAREN)
if wasIndented {
p.print(unindent)
p.expr1(x.Type, token.HighestPrec, depth)
}
p.level++
- p.printPos(x.Lbrace)
+ p.setPos(x.Lbrace)
p.print(token.LBRACE)
p.exprList(x.Lbrace, x.Elts, 1, commaTerm, x.Rbrace, x.Incomplete)
// do not insert extra line break following a /*-style comment
// need the initial indent to print lone comments with
// the proper level of indentation
p.print(indent, unindent, mode)
- p.printPos(x.Rbrace)
+ p.setPos(x.Rbrace)
p.print(token.RBRACE, mode)
p.level--
p.print(token.ARROW, token.CHAN) // x.Arrow and x.Pos() are the same
case ast.SEND:
p.print(token.CHAN)
- p.printPos(x.Arrow)
+ p.setPos(x.Arrow)
p.print(token.ARROW)
}
p.print(blank)
p.print(token.PERIOD)
if line := p.lineFor(x.Sel.Pos()); p.pos.IsValid() && p.pos.Line < line {
p.print(indent, newline)
- p.printPos(x.Sel.Pos())
+ p.setPos(x.Sel.Pos())
p.print(x.Sel)
if !isMethod {
p.print(unindent)
}
return true
}
- p.printPos(x.Sel.Pos())
+ p.setPos(x.Sel.Pos())
p.print(x.Sel)
return false
}
// block prints an *ast.BlockStmt; it always spans at least two lines.
func (p *printer) block(b *ast.BlockStmt, nindent int) {
- p.printPos(b.Lbrace)
+ p.setPos(b.Lbrace)
p.print(token.LBRACE)
p.stmtList(b.List, nindent, true)
p.linebreak(p.lineFor(b.Rbrace), 1, ignore, true)
- p.printPos(b.Rbrace)
+ p.setPos(b.Rbrace)
p.print(token.RBRACE)
}
}
func (p *printer) stmt(stmt ast.Stmt, nextIsRBrace bool) {
- p.printPos(stmt.Pos())
+ p.setPos(stmt.Pos())
switch s := stmt.(type) {
case *ast.BadStmt:
// between (see writeWhitespace)
p.print(unindent)
p.expr(s.Label)
- p.printPos(s.Colon)
+ p.setPos(s.Colon)
p.print(token.COLON, indent)
if e, isEmpty := s.Stmt.(*ast.EmptyStmt); isEmpty {
if !nextIsRBrace {
p.print(newline)
- p.printPos(e.Pos())
+ p.setPos(e.Pos())
p.print(token.SEMICOLON)
break
}
const depth = 1
p.expr0(s.Chan, depth)
p.print(blank)
- p.printPos(s.Arrow)
+ p.setPos(s.Arrow)
p.print(token.ARROW, blank)
p.expr0(s.Value, depth)
case *ast.IncDecStmt:
const depth = 1
p.expr0(s.X, depth+1)
- p.printPos(s.TokPos)
+ p.setPos(s.TokPos)
p.print(s.Tok)
case *ast.AssignStmt:
}
p.exprList(s.Pos(), s.Lhs, depth, 0, s.TokPos, false)
p.print(blank)
- p.printPos(s.TokPos)
+ p.setPos(s.TokPos)
p.print(s.Tok, blank)
p.exprList(s.TokPos, s.Rhs, depth, 0, token.NoPos, false)
} else {
p.print(token.DEFAULT)
}
- p.printPos(s.Colon)
+ p.setPos(s.Colon)
p.print(token.COLON)
p.stmtList(s.Body, 1, nextIsRBrace)
} else {
p.print(token.DEFAULT)
}
- p.printPos(s.Colon)
+ p.setPos(s.Colon)
p.print(token.COLON)
p.stmtList(s.Body, 1, nextIsRBrace)
body := s.Body
if len(body.List) == 0 && !p.commentBefore(p.posFor(body.Rbrace)) {
// print empty select statement w/o comments on one line
- p.printPos(body.Lbrace)
+ p.setPos(body.Lbrace)
p.print(token.LBRACE)
- p.printPos(body.Rbrace)
+ p.setPos(body.Rbrace)
p.print(token.RBRACE)
} else {
p.block(body, 0)
if s.Value != nil {
// use position of value following the comma as
// comma position for correct comment placement
- p.printPos(s.Value.Pos())
+ p.setPos(s.Value.Pos())
p.print(token.COMMA, blank)
p.expr(s.Value)
}
p.print(blank)
- p.printPos(s.TokPos)
+ p.setPos(s.TokPos)
p.print(s.Tok, blank)
}
p.print(token.RANGE, blank)
}
p.expr(sanitizeImportPath(s.Path))
p.setComment(s.Comment)
- p.printPos(s.EndPos)
+ p.setPos(s.EndPos)
case *ast.ValueSpec:
if n != 1 {
func (p *printer) genDecl(d *ast.GenDecl) {
p.setComment(d.Doc)
- p.printPos(d.Pos())
+ p.setPos(d.Pos())
p.print(d.Tok, blank)
if d.Lparen.IsValid() || len(d.Specs) > 1 {
// group of parenthesized declarations
- p.printPos(d.Lparen)
+ p.setPos(d.Lparen)
p.print(token.LPAREN)
if n := len(d.Specs); n > 0 {
p.print(indent, formfeed)
}
p.print(unindent, formfeed)
}
- p.printPos(d.Rparen)
+ p.setPos(d.Rparen)
p.print(token.RPAREN)
} else if len(d.Specs) > 0 {
const maxSize = 100
if headerSize+p.bodySize(b, maxSize) <= maxSize {
p.print(sep)
- p.printPos(b.Lbrace)
+ p.setPos(b.Lbrace)
p.print(token.LBRACE)
if len(b.List) > 0 {
p.print(blank)
p.print(blank)
}
p.print(noExtraLinebreak)
- p.printPos(b.Rbrace)
+ p.setPos(b.Rbrace)
p.print(token.RBRACE, noExtraLinebreak)
return
}
func (p *printer) funcDecl(d *ast.FuncDecl) {
p.setComment(d.Doc)
- p.printPos(d.Pos())
+ p.setPos(d.Pos())
p.print(token.FUNC, blank)
// We have to save startCol only after emitting FUNC; otherwise it can be on a
// different line (all whitespace preceding the FUNC is emitted only when the
func (p *printer) decl(decl ast.Decl) {
switch d := decl.(type) {
case *ast.BadDecl:
- p.printPos(d.Pos())
+ p.setPos(d.Pos())
p.print("BadDecl")
case *ast.GenDecl:
p.genDecl(d)
func (p *printer) file(src *ast.File) {
p.setComment(src.Doc)
- p.printPos(src.Pos())
+ p.setPos(src.Pos())
p.print(token.PACKAGE, blank)
p.expr(src.Name)
p.declList(src.Decls)