// Disabled formatting - enable eventually and remove the flag.
const (
compositeLitBlank = false;
+ fewerSemis = false;
stringListMode = exprListMode(0); // previously: noIndent
)
type exprListMode uint
const (
- blankStart exprListMode = 1<<iota; // print a blank before the list
+ blankStart exprListMode = 1<<iota; // print a blank before a non-empty list
+ blankEnd; // print a blank after a non-empty list
commaSep; // elements are separated by commas
commaTerm; // elements are terminated by comma
noIndent; // no extra indentation in multi-line lists
}
p.expr(x, multiLine);
}
+ if mode&blankEnd != 0 {
+ p.print(blank);
+ }
return;
}
}
p.expr(x, multiLine);
}
+
if mode & commaTerm != 0 {
p.print(token.COMMA);
if ws == ignore && mode&noIndent == 0 {
- // should always be indented here since we have a multi-line
- // expression list - be conservative and check anyway
+ // unindent if we indented
p.print(unindent);
}
p.print(formfeed); // terminating comma needs a line break to look good
- } else if ws == ignore && mode&noIndent == 0 {
+ return;
+ }
+
+ if mode&blankEnd != 0 {
+ p.print(blank);
+ }
+
+ if ws == ignore && mode&noIndent == 0 {
+ // unindent if we indented
p.print(unindent);
}
}
func identListSize(list []*ast.Ident, maxSize int) (size int) {
for i, x := range list {
if i > 0 {
- size += 2; // ", "
+ size += 2 // ", "
+ ;
+
}
size += len(x.Value);
if size >= maxSize {
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;
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:
// 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);
}
}