func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
p.print(fields.Opening, token.LPAREN)
if len(fields.List) > 0 {
+ prevLine := p.fset.Position(fields.Opening).Line
ws := indent
- var prevLine, line int
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 {
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 {
// Very subtle: If we indented before (ws == ignore), identList
// won't indent again. If we didn't (ws == indent), identList will
p.identList(par.Names, ws == indent, multiLine)
p.print(blank)
}
+ // parameter type
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 {
- // unindent if we indented
p.print(unindent)
}
}
TargetHistorySize int) {
}
-func MultiLineSignature0(a, b, c int) {
+func MultiLineSignature0(
+ a, b, c int,
+) {
}
-func MultiLineSignature1(a, b, c int,
- u, v, w float) {
+func MultiLineSignature1(
+ a, b, c int,
+ u, v, w float,
+) {
}
-func MultiLineSignature2(a, b,
- c int) {
+func MultiLineSignature2(
+ a, b,
+ c int,
+) {
}
-func MultiLineSignature3(a, b,
+func MultiLineSignature3(
+ a, b,
c int, u, v,
w float,
x ...int) {
}
-func MultiLineSignature4(a, b, c int,
+func MultiLineSignature4(
+ a, b, c int,
u, v,
w float,
x ...int) {
}
-func MultiLineSignature5(a, b, c int,
+func MultiLineSignature5(
+ a, b, c int,
u, v, w float,
p, q,
r string,
// make sure it also works for methods in interfaces
type _ interface {
- MultiLineSignature0(a, b, c int)
+ MultiLineSignature0(
+ a, b, c int,
+ )
- MultiLineSignature1(a, b, c int,
- u, v, w float)
+ MultiLineSignature1(
+ a, b, c int,
+ u, v, w float,
+ )
- MultiLineSignature2(a, b,
- c int)
+ MultiLineSignature2(
+ a, b,
+ c int,
+ )
- MultiLineSignature3(a, b,
+ MultiLineSignature3(
+ a, b,
c int, u, v,
w float,
x ...int)
- MultiLineSignature4(a, b, c int,
+ MultiLineSignature4(
+ a, b, c int,
u, v,
w float,
x ...int)
- MultiLineSignature5(a, b, c int,
+ MultiLineSignature5(
+ a, b, c int,
u, v, w float,
p, q,
r string,
}
}
+// 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.
}
}
+// 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.