1
0
mirror of https://github.com/golang/go synced 2024-11-22 01:44:40 -07:00

go/printer, gofmt: align comments in multi-line expression lists

- gofmt -w src misc
- improves several lists and fixes minor degradation introduced
  with the fix for issue 628
- removed some dead code (stringList)

R=rsc
CC=golang-dev
https://golang.org/cl/223058
This commit is contained in:
Robert Griesemer 2010-02-25 16:07:55 -08:00
parent 9750adbbad
commit d177539877
11 changed files with 115 additions and 67 deletions

View File

@ -92,29 +92,24 @@ func (p *printer) identList(list []*ast.Ident, multiLine *bool) {
} }
// Sets multiLine to true if the string list spans multiple lines.
func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) {
// convert into an expression list so we can re-use exprList formatting
xlist := make([]ast.Expr, len(list))
for i, x := range list {
xlist[i] = x
}
p.exprList(noPos, xlist, 1, plusSep, multiLine, noPos)
}
type exprListMode uint type exprListMode uint
const ( const (
blankStart exprListMode = 1 << iota // print a blank before a non-empty list blankStart exprListMode = 1 << iota // print a blank before a non-empty list
blankEnd // print a blank after a non-empty list blankEnd // print a blank after a non-empty list
plusSep // elements are separared by + operators
commaSep // elements are separated by commas commaSep // elements are separated by commas
commaTerm // list is optionally terminated by a comma commaTerm // list is optionally terminated by a comma
noIndent // no extra indentation in multi-line lists noIndent // no extra indentation in multi-line lists
) )
// isOneLineExpr returns true if x is "small enough" to fit onto a single line.
func (p *printer) isOneLineExpr(x ast.Expr) bool {
const maxSize = 60 // aproximate value, excluding space for comments
return p.nodeSize(x, maxSize) <= maxSize
}
// Print a list of expressions. If the list spans multiple // Print a list of expressions. If the list spans multiple
// source lines, the original line breaks are respected between // source lines, the original line breaks are respected between
// expressions. Sets multiLine to true if the list spans multiple // expressions. Sets multiLine to true if the list spans multiple
@ -141,9 +136,6 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode
// 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&plusSep != 0 {
p.print(blank, token.ADD)
}
if mode&commaSep != 0 { if mode&commaSep != 0 {
p.print(token.COMMA) p.print(token.COMMA)
} }
@ -167,31 +159,42 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode
ws = indent ws = indent
} }
// the first linebreak is always a formfeed since this section must not
// depend on any previous formatting
if prev.IsValid() && prev.Line < line && p.linebreak(line, 1, 2, ws, true) { if prev.IsValid() && prev.Line < line && p.linebreak(line, 1, 2, ws, true) {
ws = ignore ws = ignore
*multiLine = true *multiLine = true
} }
oneLiner := false // true if the previous expression fit on a single line
prevBreak := 0 // index of last expression that was followed by a linebreak
for i, x := range list { for i, x := range list {
prev := line prev := line
line = x.Pos().Line line = x.Pos().Line
if i > 0 { if i > 0 {
if mode&plusSep != 0 {
p.print(blank, token.ADD)
}
if mode&commaSep != 0 { if mode&commaSep != 0 {
p.print(token.COMMA) p.print(token.COMMA)
} }
if prev < line && prev > 0 && line > 0 { if prev < line && prev > 0 && line > 0 {
if p.linebreak(line, 1, 2, ws, true) { // lines are broken using newlines so comments remain aligned,
// but if an expression is not a "one-line" expression, or if
// multiple expressions are on the same line, the section is
// broken with a formfeed
if p.linebreak(line, 1, 2, ws, !oneLiner || prevBreak+1 < i) {
ws = ignore ws = ignore
*multiLine = true *multiLine = true
prevBreak = i
} }
} else { } else {
p.print(blank) p.print(blank)
} }
} }
p.expr0(x, depth, multiLine) p.expr0(x, depth, multiLine)
// determine if x satisfies the "one-liner" criteria
// TODO(gri): determine if the multiline information returned
// from p.expr0 is precise enough so it could be
// used instead
oneLiner = p.isOneLineExpr(x)
} }
if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line { if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line {

View File

@ -40,9 +40,9 @@ const (
var ( var (
esc = []byte{tabwriter.Escape} esc = []byte{tabwriter.Escape}
htab = []byte{'\t'} htab = []byte{'\t'}
htabs = [...]byte{'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t'} htabs = []byte("\t\t\t\t\t\t\t\t")
newlines = [...]byte{'\n', '\n', '\n', '\n', '\n', '\n', '\n', '\n'} // more than maxNewlines newlines = []byte("\n\n\n\n\n\n\n\n") // more than maxNewlines
formfeeds = [...]byte{'\f', '\f', '\f', '\f', '\f', '\f', '\f', '\f'} // more than maxNewlines formfeeds = []byte("\f\f\f\f\f\f\f\f") // more than maxNewlines
esc_quot = []byte("&#34;") // shorter than "&quot;" esc_quot = []byte("&#34;") // shorter than "&quot;"
esc_apos = []byte("&#39;") // shorter than "&apos;" esc_apos = []byte("&#39;") // shorter than "&apos;"
@ -147,7 +147,7 @@ func (p *printer) write(data []byte) {
// must not be discarded by the tabwriter // must not be discarded by the tabwriter
j := p.indent j := p.indent
for ; j > len(htabs); j -= len(htabs) { for ; j > len(htabs); j -= len(htabs) {
p.write0(&htabs) p.write0(htabs)
} }
p.write0(htabs[0:j]) p.write0(htabs[0:j])
@ -526,7 +526,7 @@ func stripCommonPrefix(lines [][]byte) {
// with the opening /*, otherwise align the text with the other // with the opening /*, otherwise align the text with the other
// lines. // lines.
last := lines[len(lines)-1] last := lines[len(lines)-1]
closing := []byte{'*', '/'} closing := []byte("*/")
i := bytes.Index(last, closing) i := bytes.Index(last, closing)
if isBlank(last[0:i]) { if isBlank(last[0:i]) {
// last line only contains closing */ // last line only contains closing */

View File

@ -337,6 +337,21 @@ func _() {
} }
// Align comments in multi-line lists of single-line expressions.
var txpix = [NCOL]draw.Color{
draw.Yellow, // yellow
draw.Cyan, // cyan
draw.Green, // lime green
draw.GreyBlue, // slate
draw.Red, /* red */
draw.GreyGreen, /* olive green */
draw.Blue, /* blue */
draw.Color(0xFF55AAFF), /* pink */
draw.Color(0xFFAAFFFF), /* lavender */
draw.Color(0xBB005DFF), /* maroon */
}
func same(t, u *Time) bool { func same(t, u *Time) bool {
// respect source lines in multi-line expressions // respect source lines in multi-line expressions
return t.Year == u.Year && return t.Year == u.Year &&

View File

@ -341,6 +341,21 @@ func _() {
} }
// Align comments in multi-line lists of single-line expressions.
var txpix = [NCOL]draw.Color{
draw.Yellow, // yellow
draw.Cyan, // cyan
draw.Green, // lime green
draw.GreyBlue, // slate
draw.Red, /* red */
draw.GreyGreen, /* olive green */
draw.Blue, /* blue */
draw.Color(0xFF55AAFF), /* pink */
draw.Color(0xFFAAFFFF), /* lavender */
draw.Color(0xBB005DFF), /* maroon */
}
func same(t, u *Time) bool { func same(t, u *Time) bool {
// respect source lines in multi-line expressions // respect source lines in multi-line expressions
return t.Year == u.Year && return t.Year == u.Year &&

View File

@ -337,6 +337,21 @@ func _() {
} }
// Align comments in multi-line lists of single-line expressions.
var txpix = [NCOL]draw.Color{
draw.Yellow, // yellow
draw.Cyan, // cyan
draw.Green, // lime green
draw.GreyBlue, // slate
draw.Red, /* red */
draw.GreyGreen, /* olive green */
draw.Blue, /* blue */
draw.Color(0xFF55AAFF), /* pink */
draw.Color(0xFFAAFFFF), /* lavender */
draw.Color(0xBB005DFF), /* maroon */
}
func same(t, u *Time) bool { func same(t, u *Time) bool {
// respect source lines in multi-line expressions // respect source lines in multi-line expressions
return t.Year == u.Year && return t.Year == u.Year &&