mirror of
https://github.com/golang/go
synced 2024-11-23 16:10:05 -07:00
gofmt: indent multi-line signatures
There may be more fine-tuning down the line, but this CL fixes the most pressing issue at hand. Also: gofmt -w src misc Fixes #1524. R=rsc, bradfitz CC=golang-dev https://golang.org/cl/4975053
This commit is contained in:
parent
686181edfe
commit
c10679009a
@ -16,7 +16,7 @@ import (
|
||||
const greeting = "hello, world"
|
||||
|
||||
type testPair struct {
|
||||
Name string
|
||||
Name string
|
||||
Got, Want interface{}
|
||||
}
|
||||
|
||||
|
@ -269,6 +269,7 @@ func (p *printer) exprList(prev0 token.Pos, list []ast.Expr, depth int, mode exp
|
||||
func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
|
||||
p.print(fields.Opening, token.LPAREN)
|
||||
if len(fields.List) > 0 {
|
||||
ws := indent
|
||||
var prevLine, line int
|
||||
for i, par := range fields.List {
|
||||
if i > 0 {
|
||||
@ -278,19 +279,30 @@ func (p *printer) parameters(fields *ast.FieldList, multiLine *bool) {
|
||||
} else {
|
||||
line = p.fset.Position(par.Type.Pos()).Line
|
||||
}
|
||||
if 0 < prevLine && prevLine < line && p.linebreak(line, 0, ignore, true) {
|
||||
if 0 < prevLine && prevLine < line && p.linebreak(line, 0, ws, true) {
|
||||
ws = ignore
|
||||
*multiLine = true
|
||||
} else {
|
||||
p.print(blank)
|
||||
}
|
||||
}
|
||||
if len(par.Names) > 0 {
|
||||
p.identList(par.Names, false, multiLine)
|
||||
// Very subtle: If we indented before (ws == ignore), identList
|
||||
// won't indent again. If we didn't (ws == indent), identList will
|
||||
// indent if the identList spans multiple lines, and it will outdent
|
||||
// again at the end (and still ws == indent). Thus, a subsequent indent
|
||||
// by a linebreak call after a type, or in the next multi-line identList
|
||||
// will do the right thing.
|
||||
p.identList(par.Names, ws == indent, multiLine)
|
||||
p.print(blank)
|
||||
}
|
||||
p.expr(par.Type, multiLine)
|
||||
prevLine = p.fset.Position(par.Type.Pos()).Line
|
||||
}
|
||||
if ws == ignore {
|
||||
// unindent if we indented
|
||||
p.print(unindent)
|
||||
}
|
||||
}
|
||||
p.print(fields.Closing, token.RPAREN)
|
||||
}
|
||||
|
111
src/pkg/go/printer/testdata/declarations.golden
vendored
111
src/pkg/go/printer/testdata/declarations.golden
vendored
@ -692,56 +692,119 @@ func _(x ...chan int)
|
||||
|
||||
// these parameter lists must remain multi-line since they are multi-line in the source
|
||||
func _(bool,
|
||||
int) {
|
||||
int) {
|
||||
}
|
||||
func _(x bool,
|
||||
y int) {
|
||||
y int) {
|
||||
}
|
||||
func _(x,
|
||||
y bool) {
|
||||
y bool) {
|
||||
}
|
||||
func _(bool, // comment
|
||||
int) {
|
||||
int) {
|
||||
}
|
||||
func _(x bool, // comment
|
||||
y int) {
|
||||
y int) {
|
||||
}
|
||||
func _(x, // comment
|
||||
y bool) {
|
||||
y bool) {
|
||||
}
|
||||
func _(bool, // comment
|
||||
// comment
|
||||
int) {
|
||||
// comment
|
||||
int) {
|
||||
}
|
||||
func _(x bool, // comment
|
||||
// comment
|
||||
y int) {
|
||||
// comment
|
||||
y int) {
|
||||
}
|
||||
func _(x, // comment
|
||||
// comment
|
||||
y bool) {
|
||||
// comment
|
||||
y bool) {
|
||||
}
|
||||
func _(bool,
|
||||
// comment
|
||||
int) {
|
||||
// comment
|
||||
int) {
|
||||
}
|
||||
func _(x bool,
|
||||
// comment
|
||||
y int) {
|
||||
// comment
|
||||
y int) {
|
||||
}
|
||||
func _(x,
|
||||
// comment
|
||||
y bool) {
|
||||
// comment
|
||||
y bool) {
|
||||
}
|
||||
func _(x, // comment
|
||||
y, // comment
|
||||
z bool) {
|
||||
y, // comment
|
||||
z bool) {
|
||||
}
|
||||
func _(x, // comment
|
||||
y, // comment
|
||||
z bool) {
|
||||
y, // comment
|
||||
z bool) {
|
||||
}
|
||||
func _(x int, // comment
|
||||
y float, // comment
|
||||
z bool) {
|
||||
y float, // comment
|
||||
z bool) {
|
||||
}
|
||||
|
||||
// properly indent multi-line signatures
|
||||
func ManageStatus(in <-chan *Status, req <-chan Request,
|
||||
stat chan<- *TargetInfo,
|
||||
TargetHistorySize int) {
|
||||
}
|
||||
|
||||
func MultiLineSignature0(a, b, c int) {
|
||||
}
|
||||
|
||||
func MultiLineSignature1(a, b, c int,
|
||||
u, v, w float) {
|
||||
}
|
||||
|
||||
func MultiLineSignature2(a, b,
|
||||
c int) {
|
||||
}
|
||||
|
||||
func MultiLineSignature3(a, b,
|
||||
c int, u, v,
|
||||
w float,
|
||||
x ...int) {
|
||||
}
|
||||
|
||||
func MultiLineSignature4(a, b, c int,
|
||||
u, v,
|
||||
w float,
|
||||
x ...int) {
|
||||
}
|
||||
|
||||
func MultiLineSignature5(a, b, c int,
|
||||
u, v, w float,
|
||||
p, q,
|
||||
r string,
|
||||
x ...int) {
|
||||
}
|
||||
|
||||
// make sure it also works for methods in interfaces
|
||||
type _ interface {
|
||||
MultiLineSignature0(a, b, c int)
|
||||
|
||||
MultiLineSignature1(a, b, c int,
|
||||
u, v, w float)
|
||||
|
||||
MultiLineSignature2(a, b,
|
||||
c int)
|
||||
|
||||
MultiLineSignature3(a, b,
|
||||
c int, u, v,
|
||||
w float,
|
||||
x ...int)
|
||||
|
||||
MultiLineSignature4(a, b, c int,
|
||||
u, v,
|
||||
w float,
|
||||
x ...int)
|
||||
|
||||
MultiLineSignature5(a, b, c int,
|
||||
u, v, w float,
|
||||
p, q,
|
||||
r string,
|
||||
x ...int)
|
||||
}
|
||||
|
76
src/pkg/go/printer/testdata/declarations.input
vendored
76
src/pkg/go/printer/testdata/declarations.input
vendored
@ -755,3 +755,79 @@ func _(x int, // comment
|
||||
y float, // comment
|
||||
z bool) {
|
||||
}
|
||||
|
||||
|
||||
// properly indent multi-line signatures
|
||||
func ManageStatus(in <-chan *Status, req <-chan Request,
|
||||
stat chan<- *TargetInfo,
|
||||
TargetHistorySize int) {
|
||||
}
|
||||
|
||||
func MultiLineSignature0(
|
||||
a, b, c int,
|
||||
) {}
|
||||
|
||||
func MultiLineSignature1(
|
||||
a, b, c int,
|
||||
u, v, w float,
|
||||
) {}
|
||||
|
||||
func MultiLineSignature2(
|
||||
a, b,
|
||||
c int,
|
||||
) {}
|
||||
|
||||
func MultiLineSignature3(
|
||||
a, b,
|
||||
c int, u, v,
|
||||
w float,
|
||||
x ...int) {}
|
||||
|
||||
func MultiLineSignature4(
|
||||
a, b, c int,
|
||||
u, v,
|
||||
w float,
|
||||
x ...int) {}
|
||||
|
||||
func MultiLineSignature5(
|
||||
a, b, c int,
|
||||
u, v, w float,
|
||||
p, q,
|
||||
r string,
|
||||
x ...int) {}
|
||||
|
||||
// make sure it also works for methods in interfaces
|
||||
type _ interface {
|
||||
MultiLineSignature0(
|
||||
a, b, c int,
|
||||
)
|
||||
|
||||
MultiLineSignature1(
|
||||
a, b, c int,
|
||||
u, v, w float,
|
||||
)
|
||||
|
||||
MultiLineSignature2(
|
||||
a, b,
|
||||
c int,
|
||||
)
|
||||
|
||||
MultiLineSignature3(
|
||||
a, b,
|
||||
c int, u, v,
|
||||
w float,
|
||||
x ...int)
|
||||
|
||||
MultiLineSignature4(
|
||||
a, b, c int,
|
||||
u, v,
|
||||
w float,
|
||||
x ...int)
|
||||
|
||||
MultiLineSignature5(
|
||||
a, b, c int,
|
||||
u, v, w float,
|
||||
p, q,
|
||||
r string,
|
||||
x ...int)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user