mirror of
https://github.com/golang/go
synced 2024-11-24 23:17:57 -07:00
Various cleanups:
- no need to replace comments for stand-alone blocks - always print string concatenations with interspersed "+" (remove option) - minor cleanups R=rsc https://golang.org/cl/174076
This commit is contained in:
parent
3997495dc3
commit
222462ed4f
@ -74,7 +74,7 @@ func initParserMode() {
|
|||||||
|
|
||||||
|
|
||||||
func initPrinterMode() {
|
func initPrinterMode() {
|
||||||
printerMode = printer.NoStringConcat;
|
printerMode = uint(0);
|
||||||
if *tabIndent {
|
if *tabIndent {
|
||||||
printerMode |= printer.TabIndent
|
printerMode |= printer.TabIndent
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,7 @@ import (
|
|||||||
|
|
||||||
// Disabled formatting - enable eventually and remove the flag.
|
// Disabled formatting - enable eventually and remove the flag.
|
||||||
const (
|
const (
|
||||||
compositeLitBlank = false;
|
compositeLitBlank = false;
|
||||||
fewerSemis = true;
|
|
||||||
stringListMode = exprListMode(0); // previously: noIndent
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -28,7 +26,7 @@ const (
|
|||||||
// - better comment formatting for /*-style comments at the end of a line (e.g. a declaration)
|
// - better comment formatting for /*-style comments at the end of a line (e.g. a declaration)
|
||||||
// when the comment spans multiple lines; if such a comment is just two lines, formatting is
|
// when the comment spans multiple lines; if such a comment is just two lines, formatting is
|
||||||
// not idempotent
|
// not idempotent
|
||||||
// - formatting of expression lists; especially for string lists (stringListMode)
|
// - formatting of expression lists; especially for string lists
|
||||||
// - blank after { and before } in one-line composite literals probably looks better
|
// - blank after { and before } in one-line composite literals probably looks better
|
||||||
// - should use blank instead of tab to separate one-line function bodies from
|
// - should use blank instead of tab to separate one-line function bodies from
|
||||||
// the function header unless there is a group of consecutive one-liners
|
// the function header unless there is a group of consecutive one-liners
|
||||||
@ -127,11 +125,7 @@ func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) {
|
|||||||
for i, x := range list {
|
for i, x := range list {
|
||||||
xlist[i] = x
|
xlist[i] = x
|
||||||
}
|
}
|
||||||
mode := stringListMode;
|
p.exprList(noPos, xlist, 1, plusSep, multiLine);
|
||||||
if p.Mode&NoStringConcat != 0 {
|
|
||||||
mode |= plusSep
|
|
||||||
}
|
|
||||||
p.exprList(noPos, xlist, 1, mode, multiLine);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -833,7 +827,7 @@ func (p *printer) stmtList(list []ast.Stmt, _indent int) {
|
|||||||
// in those cases each clause is a new section
|
// in those cases each clause is a new section
|
||||||
p.linebreak(s.Pos().Line, 1, maxStmtNewlines, ignore, i == 0 || _indent == 0 || multiLine);
|
p.linebreak(s.Pos().Line, 1, maxStmtNewlines, ignore, i == 0 || _indent == 0 || multiLine);
|
||||||
multiLine = false;
|
multiLine = false;
|
||||||
if !p.stmt(s, &multiLine) && (!fewerSemis || len(list) > 1) && p.Mode&NoSemis == 0 {
|
if !p.stmt(s, &multiLine) && len(list) > 1 && p.Mode&NoSemis == 0 {
|
||||||
p.print(token.SEMICOLON)
|
p.print(token.SEMICOLON)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -855,8 +849,10 @@ func (p *printer) moveCommentsAfter(pos token.Position) {
|
|||||||
|
|
||||||
|
|
||||||
// block prints an *ast.BlockStmt; it always spans at least two lines.
|
// block prints an *ast.BlockStmt; it always spans at least two lines.
|
||||||
func (p *printer) block(s *ast.BlockStmt, indent int) {
|
func (p *printer) block(s *ast.BlockStmt, indent int, moveComments bool) {
|
||||||
p.moveCommentsAfter(s.Pos());
|
if moveComments {
|
||||||
|
p.moveCommentsAfter(s.Pos())
|
||||||
|
}
|
||||||
p.print(s.Pos(), token.LBRACE);
|
p.print(s.Pos(), token.LBRACE);
|
||||||
p.stmtList(s.List, indent);
|
p.stmtList(s.List, indent);
|
||||||
p.linebreak(s.Rbrace.Line, 1, maxStmtNewlines, ignore, true);
|
p.linebreak(s.Rbrace.Line, 1, maxStmtNewlines, ignore, true);
|
||||||
@ -976,14 +972,14 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
case *ast.BlockStmt:
|
case *ast.BlockStmt:
|
||||||
p.block(s, 1);
|
p.block(s, 1, false);
|
||||||
*multiLine = true;
|
*multiLine = true;
|
||||||
optSemi = true;
|
optSemi = true;
|
||||||
|
|
||||||
case *ast.IfStmt:
|
case *ast.IfStmt:
|
||||||
p.print(token.IF);
|
p.print(token.IF);
|
||||||
p.controlClause(false, s.Init, s.Cond, nil);
|
p.controlClause(false, s.Init, s.Cond, nil);
|
||||||
p.block(s.Body, 1);
|
p.block(s.Body, 1, true);
|
||||||
*multiLine = true;
|
*multiLine = true;
|
||||||
optSemi = true;
|
optSemi = true;
|
||||||
if s.Else != nil {
|
if s.Else != nil {
|
||||||
@ -1012,7 +1008,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
|
|||||||
case *ast.SwitchStmt:
|
case *ast.SwitchStmt:
|
||||||
p.print(token.SWITCH);
|
p.print(token.SWITCH);
|
||||||
p.controlClause(false, s.Init, s.Tag, nil);
|
p.controlClause(false, s.Init, s.Tag, nil);
|
||||||
p.block(s.Body, 0);
|
p.block(s.Body, 0, true);
|
||||||
*multiLine = true;
|
*multiLine = true;
|
||||||
optSemi = true;
|
optSemi = true;
|
||||||
|
|
||||||
@ -1037,7 +1033,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
|
|||||||
p.print(blank);
|
p.print(blank);
|
||||||
p.stmt(s.Assign, ignoreMultiLine);
|
p.stmt(s.Assign, ignoreMultiLine);
|
||||||
p.print(blank);
|
p.print(blank);
|
||||||
p.block(s.Body, 0);
|
p.block(s.Body, 0, true);
|
||||||
*multiLine = true;
|
*multiLine = true;
|
||||||
optSemi = true;
|
optSemi = true;
|
||||||
|
|
||||||
@ -1058,14 +1054,14 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
|
|||||||
|
|
||||||
case *ast.SelectStmt:
|
case *ast.SelectStmt:
|
||||||
p.print(token.SELECT, blank);
|
p.print(token.SELECT, blank);
|
||||||
p.block(s.Body, 0);
|
p.block(s.Body, 0, false);
|
||||||
*multiLine = true;
|
*multiLine = true;
|
||||||
optSemi = true;
|
optSemi = true;
|
||||||
|
|
||||||
case *ast.ForStmt:
|
case *ast.ForStmt:
|
||||||
p.print(token.FOR);
|
p.print(token.FOR);
|
||||||
p.controlClause(true, s.Init, s.Cond, s.Post);
|
p.controlClause(true, s.Init, s.Cond, s.Post);
|
||||||
p.block(s.Body, 1);
|
p.block(s.Body, 1, true);
|
||||||
*multiLine = true;
|
*multiLine = true;
|
||||||
optSemi = true;
|
optSemi = true;
|
||||||
|
|
||||||
@ -1079,7 +1075,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
|
|||||||
p.print(blank, s.TokPos, s.Tok, blank, token.RANGE, blank);
|
p.print(blank, s.TokPos, s.Tok, blank, token.RANGE, blank);
|
||||||
p.expr(s.X, multiLine);
|
p.expr(s.X, multiLine);
|
||||||
p.print(blank);
|
p.print(blank);
|
||||||
p.block(s.Body, 1);
|
p.block(s.Body, 1, true);
|
||||||
*multiLine = true;
|
*multiLine = true;
|
||||||
optSemi = true;
|
optSemi = true;
|
||||||
|
|
||||||
@ -1284,7 +1280,7 @@ func (p *printer) funcBody(b *ast.BlockStmt, headerSize int, isLit bool, multiLi
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.print(blank);
|
p.print(blank);
|
||||||
p.block(b, 1);
|
p.block(b, 1, true);
|
||||||
*multiLine = true;
|
*multiLine = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -895,7 +895,6 @@ const (
|
|||||||
TabIndent; // use tabs for indentation independent of UseSpaces
|
TabIndent; // use tabs for indentation independent of UseSpaces
|
||||||
UseSpaces; // use spaces instead of tabs for alignment
|
UseSpaces; // use spaces instead of tabs for alignment
|
||||||
NoSemis; // don't print semicolons at the end of a line
|
NoSemis; // don't print semicolons at the end of a line
|
||||||
NoStringConcat; // don't print string lists without "+"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -76,8 +76,7 @@ type Regexp struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
_START = // beginning of program
|
_START = iota; // beginning of program
|
||||||
iota;
|
|
||||||
_END; // end of program: success
|
_END; // end of program: success
|
||||||
_BOT; // '^' beginning of text
|
_BOT; // '^' beginning of text
|
||||||
_EOT; // '$' end of text
|
_EOT; // '$' end of text
|
||||||
|
Loading…
Reference in New Issue
Block a user