Commit f1c39c13 authored by Robert Griesemer's avatar Robert Griesemer

go/printer: follow-up on CL 802043

- more test cases
- comment fixes
- minor unrelated changes as part of investigation of issue 702

R=rsc
CC=golang-dev
https://golang.org/cl/860041
parent 78547ca1
...@@ -260,7 +260,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode ...@@ -260,7 +260,7 @@ func (p *printer) exprList(prev token.Position, list []ast.Expr, depth int, mode
} else if mode&periodSep == 0 { } else if mode&periodSep == 0 {
p.print(blank) p.print(blank)
} }
// period-separadet list elements don't need a blank // period-separated list elements don't need a blank
} }
if isPair && size > 0 && len(list) > 1 { if isPair && size > 0 && len(list) > 1 {
...@@ -676,8 +676,6 @@ func isBinary(expr ast.Expr) bool { ...@@ -676,8 +676,6 @@ func isBinary(expr ast.Expr) bool {
// type assertions, all of which may be found in selector chains, to make them // type assertions, all of which may be found in selector chains, to make them
// parts of the chain. // parts of the chain.
func splitSelector(expr ast.Expr) (body, suffix ast.Expr) { func splitSelector(expr ast.Expr) (body, suffix ast.Expr) {
// Rewrite call and index expressions to be a part of the selector chain so
// that their multiline arguments get indented correctly.
switch x := expr.(type) { switch x := expr.(type) {
case *ast.SelectorExpr: case *ast.SelectorExpr:
body, suffix = x.X, x.Sel body, suffix = x.X, x.Sel
...@@ -714,7 +712,8 @@ func splitSelector(expr ast.Expr) (body, suffix ast.Expr) { ...@@ -714,7 +712,8 @@ func splitSelector(expr ast.Expr) (body, suffix ast.Expr) {
// Convert an expression into an expression list split at the periods of // Convert an expression into an expression list split at the periods of
// selector expressions. // selector expressions.
func selectorExprList(expr ast.Expr) (result []ast.Expr) { func selectorExprList(expr ast.Expr) []ast.Expr {
// split expression
var list vector.Vector var list vector.Vector
for expr != nil { for expr != nil {
var suffix ast.Expr var suffix ast.Expr
...@@ -722,13 +721,14 @@ func selectorExprList(expr ast.Expr) (result []ast.Expr) { ...@@ -722,13 +721,14 @@ func selectorExprList(expr ast.Expr) (result []ast.Expr) {
list.Push(suffix) list.Push(suffix)
} }
result = make([]ast.Expr, len(list)) // convert expression list
result := make([]ast.Expr, len(list))
i := len(result) i := len(result)
for _, x := range list { for _, x := range list {
i-- i--
result[i] = x.(ast.Expr) result[i] = x.(ast.Expr)
} }
return return result
} }
......
...@@ -472,7 +472,7 @@ func stripCommonPrefix(lines [][]byte) { ...@@ -472,7 +472,7 @@ func stripCommonPrefix(lines [][]byte) {
for i, line := range lines[1 : len(lines)-1] { for i, line := range lines[1 : len(lines)-1] {
switch { switch {
case isBlank(line): case isBlank(line):
lines[i+1] = nil lines[1+i] = nil // range starts at line 1
case prefix == nil: case prefix == nil:
prefix = commonPrefix(line, line) prefix = commonPrefix(line, line)
default: default:
...@@ -521,7 +521,7 @@ func stripCommonPrefix(lines [][]byte) { ...@@ -521,7 +521,7 @@ func stripCommonPrefix(lines [][]byte) {
} else { } else {
// comment text on the first line // comment text on the first line
suffix := make([]byte, len(first)) suffix := make([]byte, len(first))
n := 2 n := 2 // start after opening /*
for n < len(first) && first[n] <= ' ' { for n < len(first) && first[n] <= ' ' {
suffix[n] = first[n] suffix[n] = first[n]
n++ n++
...@@ -563,9 +563,9 @@ func stripCommonPrefix(lines [][]byte) { ...@@ -563,9 +563,9 @@ func stripCommonPrefix(lines [][]byte) {
} }
// Remove the common prefix from all but the first and empty lines. // Remove the common prefix from all but the first and empty lines.
for i, line := range lines { for i, line := range lines[1:] {
if i > 0 && len(line) != 0 { if len(line) != 0 {
lines[i] = line[len(prefix):] lines[1+i] = line[len(prefix):] // range starts at line 1
} }
} }
} }
......
...@@ -473,4 +473,25 @@ func _() { ...@@ -473,4 +473,25 @@ func _() {
Method(1, 2, Method(1, 2,
3). 3).
Thingy Thingy
_ = a.b.c
_ = a.
b.
c
_ = a.b().c
_ = a.
b().
c
_ = a.b[0].c
_ = a.
b[0].
c
_ = a.b[0:].c
_ = a.
b[0:].
c
_ = a.b.(T).c
_ = a.
b.(T).
c
} }
...@@ -423,19 +423,19 @@ func _() { ...@@ -423,19 +423,19 @@ func _() {
1).foo(2) 1).foo(2)
_ = Array[3 + _ = Array[3 +
4] 4]
_ = Method(1, 2, _ = Method(1, 2,
3) 3)
_ = new(T). _ = new(T).
foo(). foo().
bar().(*Type) bar() . (*Type)
_ = new(T). _ = new(T).
foo(). foo().
bar().(*Type). bar().(*Type).
baz() baz()
_ = new(T). _ = new(T).
foo(). foo().
...@@ -443,7 +443,7 @@ func _() { ...@@ -443,7 +443,7 @@ func _() {
_ = new(T). _ = new(T).
foo(). foo().
bar()["idx"]. bar()["idx"] .
baz() baz()
_ = new(T). _ = new(T).
...@@ -459,10 +459,32 @@ func _() { ...@@ -459,10 +459,32 @@ func _() {
Field. Field.
Array[3+ Array[3+
4]. 4].
Table["foo"]. Table ["foo"].
Blob.(*Type). Blob. (*Type).
Slices[1:4]. Slices[1:4].
Method(1, 2, Method(1, 2,
3). 3).
Thingy Thingy
_ = a.b.c
_ = a.
b.
c
_ = a.b().c
_ = a.
b().
c
_ = a.b[0].c
_ = a.
b[0].
c
_ = a.b[0:].c
_ = a.
b[0:].
c
_ = a.b.(T).c
_ = a.
b.
(T).
c
} }
...@@ -473,4 +473,25 @@ func _() { ...@@ -473,4 +473,25 @@ func _() {
Method(1, 2, Method(1, 2,
3). 3).
Thingy Thingy
_ = a.b.c
_ = a.
b.
c
_ = a.b().c
_ = a.
b().
c
_ = a.b[0].c
_ = a.
b[0].
c
_ = a.b[0:].c
_ = a.
b[0:].
c
_ = a.b.(T).c
_ = a.
b.(T).
c
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment