mirror of
https://github.com/golang/go
synced 2024-11-21 19:34:46 -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:
parent
9750adbbad
commit
d177539877
@ -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
|
||||
|
||||
const (
|
||||
blankStart exprListMode = 1 << iota // print a blank before 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
|
||||
commaTerm // list is optionally terminated by a comma
|
||||
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
|
||||
// source lines, the original line breaks are respected between
|
||||
// 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
|
||||
for i, x := range list {
|
||||
if i > 0 {
|
||||
if mode&plusSep != 0 {
|
||||
p.print(blank, token.ADD)
|
||||
}
|
||||
if mode&commaSep != 0 {
|
||||
p.print(token.COMMA)
|
||||
}
|
||||
@ -167,31 +159,42 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode
|
||||
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) {
|
||||
ws = ignore
|
||||
*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 {
|
||||
prev := line
|
||||
line = x.Pos().Line
|
||||
if i > 0 {
|
||||
if mode&plusSep != 0 {
|
||||
p.print(blank, token.ADD)
|
||||
}
|
||||
if mode&commaSep != 0 {
|
||||
p.print(token.COMMA)
|
||||
}
|
||||
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
|
||||
*multiLine = true
|
||||
prevBreak = i
|
||||
}
|
||||
} else {
|
||||
p.print(blank)
|
||||
}
|
||||
}
|
||||
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 {
|
||||
|
@ -40,9 +40,9 @@ const (
|
||||
var (
|
||||
esc = []byte{tabwriter.Escape}
|
||||
htab = []byte{'\t'}
|
||||
htabs = [...]byte{'\t', '\t', '\t', '\t', '\t', '\t', '\t', '\t'}
|
||||
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
|
||||
htabs = []byte("\t\t\t\t\t\t\t\t")
|
||||
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
|
||||
|
||||
esc_quot = []byte(""") // shorter than """
|
||||
esc_apos = []byte("'") // shorter than "'"
|
||||
@ -147,7 +147,7 @@ func (p *printer) write(data []byte) {
|
||||
// must not be discarded by the tabwriter
|
||||
j := p.indent
|
||||
for ; j > len(htabs); j -= len(htabs) {
|
||||
p.write0(&htabs)
|
||||
p.write0(htabs)
|
||||
}
|
||||
p.write0(htabs[0:j])
|
||||
|
||||
@ -526,7 +526,7 @@ func stripCommonPrefix(lines [][]byte) {
|
||||
// with the opening /*, otherwise align the text with the other
|
||||
// lines.
|
||||
last := lines[len(lines)-1]
|
||||
closing := []byte{'*', '/'}
|
||||
closing := []byte("*/")
|
||||
i := bytes.Index(last, closing)
|
||||
if isBlank(last[0:i]) {
|
||||
// last line only contains closing */
|
||||
|
15
src/pkg/go/printer/testdata/expressions.golden
vendored
15
src/pkg/go/printer/testdata/expressions.golden
vendored
@ -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 {
|
||||
// respect source lines in multi-line expressions
|
||||
return t.Year == u.Year &&
|
||||
|
15
src/pkg/go/printer/testdata/expressions.input
vendored
15
src/pkg/go/printer/testdata/expressions.input
vendored
@ -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 {
|
||||
// respect source lines in multi-line expressions
|
||||
return t.Year == u.Year &&
|
||||
|
15
src/pkg/go/printer/testdata/expressions.raw
vendored
15
src/pkg/go/printer/testdata/expressions.raw
vendored
@ -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 {
|
||||
// respect source lines in multi-line expressions
|
||||
return t.Year == u.Year &&
|
||||
|
Loading…
Reference in New Issue
Block a user