Skip to content

Commit 8b01b13

Browse files
committed
relax rule to insert newlines in func signatures
In particular, in cases where a field list fits all in one line, adding a newline at the end of the field list is clearly wrong. Fixes #235.
1 parent 4af70f1 commit 8b01b13

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

format/format.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,19 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
546546

547547
if f.Line(sign.Pos()) != endLine {
548548
handleMultiLine := func(fl *ast.FieldList) {
549+
// Refuse to insert a newline before the closing token
550+
// if the list is empty or all in one line.
549551
if fl == nil || len(fl.List) == 0 {
550552
return
551553
}
554+
fieldOpeningLine := f.Line(fl.Opening)
555+
fieldClosingLine := f.Line(fl.Closing)
556+
if fieldOpeningLine == fieldClosingLine {
557+
return
558+
}
559+
552560
lastFieldEnd := fl.List[len(fl.List)-1].End()
553561
lastFieldLine := f.Line(lastFieldEnd)
554-
fieldClosingLine := f.Line(fl.Closing)
555562
isLastFieldOnFieldClosingLine := lastFieldLine == fieldClosingLine
556563
isLastFieldOnSigClosingLine := lastFieldLine == endLine
557564

testdata/script/typeparams.txtar

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,22 @@ type CombineEmbeds interface {
3636
func Caller() {
3737
Foo[int,int](1,2)
3838
}
39+
40+
func Issue235[K interface {
41+
comparable
42+
constraints.Ordered
43+
}, V any](m map[K]V) []K {
44+
keys := maps.Keys(m)
45+
slices.Sort(keys)
46+
return keys
47+
}
48+
49+
func multilineParams[V any](p1 V,
50+
p2 V) {
51+
52+
println("body")
53+
54+
}
3955
-- foo.go.golden --
4056
package p
4157

@@ -65,3 +81,18 @@ type CombineEmbeds interface {
6581
func Caller() {
6682
Foo[int, int](1, 2)
6783
}
84+
85+
func Issue235[K interface {
86+
comparable
87+
constraints.Ordered
88+
}, V any](m map[K]V) []K {
89+
keys := maps.Keys(m)
90+
slices.Sort(keys)
91+
return keys
92+
}
93+
94+
func multilineParams[V any](p1 V,
95+
p2 V,
96+
) {
97+
println("body")
98+
}

0 commit comments

Comments
 (0)