From 8b01b13b2fb53f2d9485268a6e6c2137cbb7e710 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Mart=C3=AD?= Date: Sat, 8 Apr 2023 21:32:20 +0100 Subject: [PATCH] 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. --- format/format.go | 9 ++++++++- testdata/script/typeparams.txtar | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) 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") +}