diff --git a/format/format.go b/format/format.go index 36da9fb..b51f75e 100644 --- a/format/format.go +++ b/format/format.go @@ -546,12 +546,19 @@ func (f *fumpter) applyPre(c *astutil.Cursor) { if f.Line(sign.Pos()) != endLine { handleMultiLine := func(fl *ast.FieldList) { + // Refuse to insert a newline before the closing token + // if the list is empty or all in one line. if fl == nil || len(fl.List) == 0 { return } + fieldOpeningLine := f.Line(fl.Opening) + fieldClosingLine := f.Line(fl.Closing) + if fieldOpeningLine == fieldClosingLine { + return + } + lastFieldEnd := fl.List[len(fl.List)-1].End() lastFieldLine := f.Line(lastFieldEnd) - fieldClosingLine := f.Line(fl.Closing) isLastFieldOnFieldClosingLine := lastFieldLine == fieldClosingLine isLastFieldOnSigClosingLine := lastFieldLine == endLine diff --git a/testdata/script/typeparams.txtar b/testdata/script/typeparams.txtar index 26c2f6d..2552004 100644 --- a/testdata/script/typeparams.txtar +++ b/testdata/script/typeparams.txtar @@ -36,6 +36,22 @@ type CombineEmbeds interface { func Caller() { Foo[int,int](1,2) } + +func Issue235[K interface { + comparable + constraints.Ordered +}, V any](m map[K]V) []K { + keys := maps.Keys(m) + slices.Sort(keys) + return keys +} + +func multilineParams[V any](p1 V, + p2 V) { + + println("body") + +} -- foo.go.golden -- package p @@ -65,3 +81,18 @@ type CombineEmbeds interface { func Caller() { Foo[int, int](1, 2) } + +func Issue235[K interface { + comparable + constraints.Ordered +}, V any](m map[K]V) []K { + keys := maps.Keys(m) + slices.Sort(keys) + return keys +} + +func multilineParams[V any](p1 V, + p2 V, +) { + println("body") +}