]> Cypherpunks repositories - gostls13.git/commitdiff
Partial work-around for gofmt bug.
authorRobert Griesemer <gri@golang.org>
Mon, 11 Jan 2010 18:38:06 +0000 (10:38 -0800)
committerRobert Griesemer <gri@golang.org>
Mon, 11 Jan 2010 18:38:06 +0000 (10:38 -0800)
Addresses some cases and respects position of
closing parenthesis; but doesn't work yet in
all cases. Heuristic; will need to be replaced
by a real solution eventually. Does not cause
existing code to be reformatted.

R=r
CC=golang-dev, rsc
https://golang.org/cl/186043

src/pkg/go/printer/nodes.go
src/pkg/go/printer/printer.go
src/pkg/go/printer/testdata/expressions.golden
src/pkg/go/printer/testdata/expressions.input
src/pkg/go/printer/testdata/expressions.raw

index b082c2e404d101a14386b771dfd9992825c34d5e..d5a3be9b0d8123483468752ecf520b5fbe502523 100644 (file)
@@ -114,7 +114,7 @@ func (p *printer) identList(list []*ast.Ident, multiLine *bool) {
        for i, x := range list {
                xlist[i] = x
        }
-       p.exprList(noPos, xlist, 1, commaSep, multiLine)
+       p.exprList(noPos, xlist, 1, commaSep, multiLine, noPos)
 }
 
 
@@ -125,7 +125,7 @@ func (p *printer) stringList(list []*ast.BasicLit, multiLine *bool) {
        for i, x := range list {
                xlist[i] = x
        }
-       p.exprList(noPos, xlist, 1, plusSep, multiLine)
+       p.exprList(noPos, xlist, 1, plusSep, multiLine, noPos)
 }
 
 
@@ -136,7 +136,7 @@ const (
        blankEnd                // print a blank after a non-empty list
        plusSep                 // elements are separared by + operators
        commaSep                // elements are separated by commas
-       commaTerm               // elements are terminated by comma
+       commaTerm               // list is optionally terminated by a comma
        noIndent                // no extra indentation in multi-line lists
 )
 
@@ -166,7 +166,7 @@ func (p *printer) beforeComment(pos token.Position) token.Position {
 // source lines, the original line breaks are respected between
 // expressions. Sets multiLine to true if the list spans multiple
 // lines.
-func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode exprListMode, multiLine *bool) {
+func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode exprListMode, multiLine *bool, next token.Position) {
        if len(list) == 0 {
                return
        }
@@ -175,11 +175,14 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode
                p.print(blank)
        }
 
-       // TODO(gri): endLine may be incorrect as it is really the beginning
-       //            of the last list entry. There may be only one, very long
-       //            entry in which case line == endLine.
        line := list[0].Pos().Line
-       endLine := list[len(list)-1].Pos().Line
+       endLine := next.Line
+       if endLine == 0 {
+               // TODO(gri): endLine may be incorrect as it is really the beginning
+               //            of the last list entry. There may be only one, very long
+               //            entry in which case line == endLine.
+               endLine = list[len(list)-1].Pos().Line
+       }
 
        if prev.IsValid() && prev.Line == line && line == endLine {
                // all list entries on a single line
@@ -238,7 +241,8 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode
                p.expr0(x, depth, multiLine)
        }
 
-       if mode&commaTerm != 0 {
+       if mode&commaTerm != 0 && next.IsValid() && p.pos.Line < next.Line {
+               // print a terminating comma if the next token is on a new line
                p.print(token.COMMA)
                if ws == ignore && mode&noIndent == 0 {
                        // unindent if we indented
@@ -304,8 +308,6 @@ func identListSize(list []*ast.Ident, maxSize int) (size int) {
        for i, x := range list {
                if i > 0 {
                        size += 2 // ", "
-
-
                }
                size += len(x.Value)
                if size >= maxSize {
@@ -329,8 +331,6 @@ func (p *printer) isOneLineFieldList(list []*ast.Field) bool {
        namesSize := identListSize(f.Names, maxSize)
        if namesSize > 0 {
                namesSize = 1 // blank between names and types
-
-
        }
        typeSize := p.nodeSize(f.Type, maxSize)
        return namesSize+typeSize <= maxSize
@@ -743,7 +743,11 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
                }
                p.expr1(x.Fun, token.HighestPrec, depth, 0, multiLine)
                p.print(x.Lparen, token.LPAREN)
-               p.exprList(x.Lparen, x.Args, depth, commaSep, multiLine)
+               mode := commaSep
+               if p.Mode&NoSemis != 0 {
+                       mode |= commaTerm
+               }
+               p.exprList(x.Lparen, x.Args, depth, mode, multiLine, x.Rparen)
                p.print(x.Rparen, token.RPAREN)
 
        case *ast.CompositeLit:
@@ -762,7 +766,7 @@ func (p *printer) expr1(expr ast.Expr, prec1, depth int, ctxt exprContext, multi
                        }
                }
                p.print(x.Lbrace, token.LBRACE)
-               p.exprList(x.Lbrace, x.Elts, 1, mode, multiLine)
+               p.exprList(x.Lbrace, x.Elts, 1, mode, multiLine, x.Rbrace)
                p.print(x.Rbrace, token.RBRACE)
 
        case *ast.Ellipsis:
@@ -969,9 +973,9 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
                if len(s.Lhs) > 1 && len(s.Rhs) > 1 {
                        depth++
                }
-               p.exprList(s.Pos(), s.Lhs, depth, commaSep, multiLine)
+               p.exprList(s.Pos(), s.Lhs, depth, commaSep, multiLine, s.TokPos)
                p.print(blank, s.TokPos, s.Tok)
-               p.exprList(s.TokPos, s.Rhs, depth, blankStart|commaSep, multiLine)
+               p.exprList(s.TokPos, s.Rhs, depth, blankStart|commaSep, multiLine, noPos)
 
        case *ast.GoStmt:
                p.print(token.GO, blank)
@@ -984,7 +988,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
        case *ast.ReturnStmt:
                p.print(token.RETURN)
                if s.Results != nil {
-                       p.exprList(s.Pos(), s.Results, 1, blankStart|commaSep, multiLine)
+                       p.exprList(s.Pos(), s.Results, 1, blankStart|commaSep, multiLine, noPos)
                }
 
        case *ast.BranchStmt:
@@ -1020,7 +1024,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
        case *ast.CaseClause:
                if s.Values != nil {
                        p.print(token.CASE)
-                       p.exprList(s.Pos(), s.Values, 1, blankStart|commaSep, multiLine)
+                       p.exprList(s.Pos(), s.Values, 1, blankStart|commaSep, multiLine, s.Colon)
                } else {
                        p.print(token.DEFAULT)
                }
@@ -1038,7 +1042,7 @@ func (p *printer) stmt(stmt ast.Stmt, multiLine *bool) (optSemi bool) {
        case *ast.TypeCaseClause:
                if s.Types != nil {
                        p.print(token.CASE)
-                       p.exprList(s.Pos(), s.Types, 1, blankStart|commaSep, multiLine)
+                       p.exprList(s.Pos(), s.Types, 1, blankStart|commaSep, multiLine, s.Colon)
                } else {
                        p.print(token.DEFAULT)
                }
@@ -1159,7 +1163,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo
                        }
                        if s.Values != nil {
                                p.print(blank, token.ASSIGN)
-                               p.exprList(noPos, s.Values, 1, blankStart|commaSep, multiLine)
+                               p.exprList(noPos, s.Values, 1, blankStart|commaSep, multiLine, noPos)
                                optSemi = false
                        }
                } else {
@@ -1174,7 +1178,7 @@ func (p *printer) spec(spec ast.Spec, n int, context declContext, multiLine *boo
                        if s.Values != nil {
                                p.print(vtab)
                                p.print(token.ASSIGN)
-                               p.exprList(noPos, s.Values, 1, blankStart|commaSep, multiLine)
+                               p.exprList(noPos, s.Values, 1, blankStart|commaSep, multiLine, noPos)
                                optSemi = false
                                extraTabs = 0
                        }
index 994dabaa82a309f0a80c8ca7ef49904fb6f1c449..16f61cd0806325f6f56145a80d04b11e2c7a84f1 100644 (file)
@@ -745,7 +745,7 @@ func (p *printer) print(args ...) {
                        // escape all literals so they pass through unchanged
                        // (note that valid Go programs cannot contain esc ('\xff')
                        // bytes since they do not appear in legal UTF-8 sequences)
-                       // TODO(gri): this this more efficiently.
+                       // TODO(gri): do this more efficiently.
                        data = strings.Bytes("\xff" + string(data) + "\xff")
                case token.Token:
                        if p.Styler != nil {
index abc66c738af37ce7214cf307c1487835bd7db473..5701c6df094a024151c797cd6c932bc95192c6b7 100644 (file)
@@ -221,7 +221,7 @@ func _() {
 
 
 func _() {
-       // not not add extra indentation to multi-line string lists
+       // do not add extra indentation to multi-line string lists
        _ = "foo" + "bar";
        _ = "foo" +
                "bar" +
@@ -309,6 +309,32 @@ func _() {
 }
 
 
+// Correct placement of terminating comma/closing parentheses in multi-line calls.
+func _() {
+       f(1,
+               2,
+               3);
+       f(1,
+               2,
+               3);
+       // TODO(gri) the cases below are not correct yet
+       f(1,
+               2,
+               3);     // comment
+       f(1,
+               2,
+               3       // comment
+       );
+       f(1,
+               2,
+               3);     // comment
+       f(1,
+               2,
+               3       // comment
+       );
+}
+
+
 func same(t, u *Time) bool {
        // respect source lines in multi-line expressions
        return t.Year == u.Year &&
index 87e05ba1eb91055bd883ff32ffb572c537207305..136e046a730e075210b58ffb65a5b223fde48e3f 100644 (file)
@@ -225,7 +225,7 @@ func _() {
 
 
 func _() {
-       // not not add extra indentation to multi-line string lists
+       // do not add extra indentation to multi-line string lists
        _ = "foo" + "bar";
        _ = "foo" +
        "bar" +
@@ -314,6 +314,33 @@ func _() {
 }
 
 
+// Correct placement of terminating comma/closing parentheses in multi-line calls.
+func _() {
+       f(1,
+               2,
+               3);
+       f(1,
+               2,
+               3
+       );
+       // TODO(gri) the cases below are not correct yet
+       f(1,
+               2,
+               3);  // comment
+       f(1,
+               2,
+               3  // comment
+       );
+       f(1,
+               2,
+               3);// comment
+       f(1,
+               2,
+               3// comment
+       );
+}
+
+
 func same(t, u *Time) bool {
        // respect source lines in multi-line expressions
        return t.Year == u.Year &&
index 366a639afa29a8982f2a45f0c0442b8e6f79f2d8..c273932bdd47c22f8e52f93a679978cc6d1f0b99 100644 (file)
@@ -221,7 +221,7 @@ func _() {
 
 
 func _() {
-       // not not add extra indentation to multi-line string lists
+       // do not add extra indentation to multi-line string lists
        _ = "foo" + "bar";
        _ = "foo" +
                "bar" +
@@ -309,6 +309,32 @@ func _() {
 }
 
 
+// Correct placement of terminating comma/closing parentheses in multi-line calls.
+func _() {
+       f(1,
+               2,
+               3);
+       f(1,
+               2,
+               3);
+       // TODO(gri) the cases below are not correct yet
+       f(1,
+               2,
+               3);     // comment
+       f(1,
+               2,
+               3       // comment
+       );
+       f(1,
+               2,
+               3);     // comment
+       f(1,
+               2,
+               3       // comment
+       );
+}
+
+
 func same(t, u *Time) bool {
        // respect source lines in multi-line expressions
        return t.Year == u.Year &&