Skip to content

Commit

Permalink
relax rule to insert newlines in func signatures
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
mvdan committed Apr 8, 2023
1 parent 4af70f1 commit 8b01b13
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
9 changes: 8 additions & 1 deletion format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 31 additions & 0 deletions testdata/script/typeparams.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
}

0 comments on commit 8b01b13

Please sign in to comment.