Commit d665ea98 authored by Robert Griesemer's avatar Robert Griesemer

go/printer, gofmt: respect line breaks in signatures

No changes when applying gofmt to src, misc.

Fixes #2597.

R=r
CC=golang-dev
https://golang.org/cl/5564056
parent 57af5429
...@@ -272,23 +272,32 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp ...@@ -272,23 +272,32 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp
func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) { func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
p.print(fields.Opening, token.LPAREN) p.print(fields.Opening, token.LPAREN)
if len(fields.List) > 0 { if len(fields.List) > 0 {
prevLine := p.fset.Position(fields.Opening).Line
ws := indent ws := indent
var prevLine, line int
for i, par := range fields.List { for i, par := range fields.List {
// determine par begin and end line (may be different
// if there are multiple parameter names for this par
// or the type is on a separate line)
var parLineBeg int
var parLineEnd = p.fset.Position(par.Type.Pos()).Line
if len(par.Names) > 0 {
parLineBeg = p.fset.Position(par.Names[0].Pos()).Line
} else {
parLineBeg = parLineEnd
}
// separating "," if needed
if i > 0 { if i > 0 {
p.print(token.COMMA) p.print(token.COMMA)
if len(par.Names) > 0 {
line = p.fset.Position(par.Names[0].Pos()).Line
} else {
line = p.fset.Position(par.Type.Pos()).Line
}
if 0 < prevLine && prevLine < line && p.linebreak(line, 0, ws, true) {
ws = ignore
*multiLine = true
} else {
p.print(blank)
}
} }
// separator if needed (linebreak or blank)
if 0 < prevLine && prevLine < parLineBeg && p.linebreak(parLineBeg, 0, ws, true) {
// break line if the opening "(" or previous parameter ended on a different line
ws = ignore
*multiLine = true
} else if i > 0 {
p.print(blank)
}
// parameter names
if len(par.Names) > 0 { if len(par.Names) > 0 {
// Very subtle: If we indented before (ws == ignore), identList // Very subtle: If we indented before (ws == ignore), identList
// won't indent again. If we didn't (ws == indent), identList will // won't indent again. If we didn't (ws == indent), identList will
...@@ -299,11 +308,18 @@ func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) { ...@@ -299,11 +308,18 @@ func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
p.identList(par.Names, ws == indent, multiLine) p.identList(par.Names, ws == indent, multiLine)
p.print(blank) p.print(blank)
} }
// parameter type
p.expr(par.Type, multiLine) p.expr(par.Type, multiLine)
prevLine = p.fset.Position(par.Type.Pos()).Line prevLine = parLineEnd
} }
// if the closing ")" is on a separate line from the last parameter,
// print an additional "," and line break
if closing := p.fset.Position(fields.Closing).Line; 0 < prevLine && prevLine < closing {
p.print(",")
p.linebreak(closing, 0, ignore, true)
}
// unindent if we indented
if ws == ignore { if ws == ignore {
// unindent if we indented
p.print(unindent) p.print(unindent)
} }
} }
......
...@@ -773,30 +773,39 @@ func ManageStatus(in <-chan *Status, req <-chan Request, ...@@ -773,30 +773,39 @@ func ManageStatus(in <-chan *Status, req <-chan Request,
TargetHistorySize int) { TargetHistorySize int) {
} }
func MultiLineSignature0(a, b, c int) { func MultiLineSignature0(
a, b, c int,
) {
} }
func MultiLineSignature1(a, b, c int, func MultiLineSignature1(
u, v, w float) { a, b, c int,
u, v, w float,
) {
} }
func MultiLineSignature2(a, b, func MultiLineSignature2(
c int) { a, b,
c int,
) {
} }
func MultiLineSignature3(a, b, func MultiLineSignature3(
a, b,
c int, u, v, c int, u, v,
w float, w float,
x ...int) { x ...int) {
} }
func MultiLineSignature4(a, b, c int, func MultiLineSignature4(
a, b, c int,
u, v, u, v,
w float, w float,
x ...int) { x ...int) {
} }
func MultiLineSignature5(a, b, c int, func MultiLineSignature5(
a, b, c int,
u, v, w float, u, v, w float,
p, q, p, q,
r string, r string,
...@@ -805,25 +814,34 @@ func MultiLineSignature5(a, b, c int, ...@@ -805,25 +814,34 @@ func MultiLineSignature5(a, b, c int,
// make sure it also works for methods in interfaces // make sure it also works for methods in interfaces
type _ interface { type _ interface {
MultiLineSignature0(a, b, c int) MultiLineSignature0(
a, b, c int,
)
MultiLineSignature1(a, b, c int, MultiLineSignature1(
u, v, w float) a, b, c int,
u, v, w float,
)
MultiLineSignature2(a, b, MultiLineSignature2(
c int) a, b,
c int,
)
MultiLineSignature3(a, b, MultiLineSignature3(
a, b,
c int, u, v, c int, u, v,
w float, w float,
x ...int) x ...int)
MultiLineSignature4(a, b, c int, MultiLineSignature4(
a, b, c int,
u, v, u, v,
w float, w float,
x ...int) x ...int)
MultiLineSignature5(a, b, c int, MultiLineSignature5(
a, b, c int,
u, v, w float, u, v, w float,
p, q, p, q,
r string, r string,
......
...@@ -220,4 +220,56 @@ testLoop: ...@@ -220,4 +220,56 @@ testLoop:
} }
} }
// Respect line breaks in function calls.
func _() {
f(x)
f(x,
x)
f(x,
x,
)
f(
x,
x)
f(
x,
x,
)
}
// Respect line breaks in function declarations.
func _(x T) {}
func _(x T,
y T) {
}
func _(x T,
y T,
) {
}
func _(
x T,
y T) {
}
func _(
x T,
y T,
) {
}
// Example from issue 2597.
func ManageStatus0(
in <-chan *Status,
req <-chan Request,
stat chan<- *TargetInfo,
TargetHistorySize int) {
}
func ManageStatus1(
in <-chan *Status,
req <-chan Request,
stat chan<- *TargetInfo,
TargetHistorySize int,
) {
}
// There should be exactly one linebreak after this comment. // There should be exactly one linebreak after this comment.
...@@ -220,4 +220,52 @@ testLoop: ...@@ -220,4 +220,52 @@ testLoop:
} }
} }
// Respect line breaks in function calls.
func _() {
f(x)
f(x,
x)
f(x,
x,
)
f(
x,
x)
f(
x,
x,
)
}
// Respect line breaks in function declarations.
func _(x T) {}
func _(x T,
y T) {}
func _(x T,
y T,
) {}
func _(
x T,
y T) {}
func _(
x T,
y T,
) {}
// Example from issue 2597.
func ManageStatus0(
in <-chan *Status,
req <-chan Request,
stat chan<- *TargetInfo,
TargetHistorySize int) {
}
func ManageStatus1(
in <-chan *Status,
req <-chan Request,
stat chan<- *TargetInfo,
TargetHistorySize int,
) {
}
// There should be exactly one linebreak after this comment. // There should be exactly one linebreak after this comment.
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