Skip to content

Commit acf11b6

Browse files
Oiyoomvdan
authored andcommitted
rework multiline func signatures
we were checking the column position but that doesn't work for nested/scoped funcs
1 parent 99c3b4c commit acf11b6

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

format/format.go

+34-23
Original file line numberDiff line numberDiff line change
@@ -511,29 +511,40 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
511511
if sign != nil {
512512
endLine := f.Line(sign.End())
513513

514-
paramClosingIsFirstCharOnEndLine := sign.Params != nil &&
515-
f.Position(sign.Params.Closing).Column == 1 &&
516-
f.Line(sign.Params.Closing) == endLine
517-
518-
resultClosingIsFirstCharOnEndLine := sign.Results != nil &&
519-
f.Position(sign.Results.Closing).Column == 1 &&
520-
f.Line(sign.Results.Closing) == endLine
521-
522-
endLineIsIndented := !(paramClosingIsFirstCharOnEndLine || resultClosingIsFirstCharOnEndLine)
523-
524-
if f.Line(sign.Pos()) != endLine && endLineIsIndented {
525-
// The body is preceded by a multi-line function
526-
// signature, we move the `) {` to avoid the empty line.
527-
switch {
528-
case sign.Results != nil &&
529-
!resultClosingIsFirstCharOnEndLine &&
530-
sign.Results.Closing.IsValid(): // there may be no ")"
531-
sign.Results.Closing += 1
532-
f.addNewline(sign.Results.Closing)
533-
534-
case sign.Params != nil && !paramClosingIsFirstCharOnEndLine:
535-
sign.Params.Closing += 1
536-
f.addNewline(sign.Params.Closing)
514+
if f.Line(sign.Pos()) != endLine {
515+
handleMultiLine := func(fl *ast.FieldList) {
516+
if fl == nil || len(fl.List) == 0 {
517+
return
518+
}
519+
lastFieldEnd := fl.List[len(fl.List)-1].End()
520+
lastFieldLine := f.Line(lastFieldEnd)
521+
fieldClosingLine := f.Line(fl.Closing)
522+
isLastFieldOnFieldClosingLine := lastFieldLine == fieldClosingLine
523+
isLastFieldOnSigClosingLine := lastFieldLine == endLine
524+
525+
var isLastCommentGrpOnFieldClosingLine, isLastCommentGrpOnSigClosingLine bool
526+
if comments := f.commentsBetween(lastFieldEnd, fl.Closing); len(comments) > 0 {
527+
lastCommentGrp := comments[len(comments)-1]
528+
lastCommentGrpLine := f.Line(lastCommentGrp.End())
529+
530+
isLastCommentGrpOnFieldClosingLine = lastCommentGrpLine == fieldClosingLine
531+
isLastCommentGrpOnSigClosingLine = lastCommentGrpLine == endLine
532+
}
533+
534+
// is there a comment grp/last field, field closing and sig closing on the same line?
535+
if (isLastFieldOnFieldClosingLine && isLastFieldOnSigClosingLine) ||
536+
(isLastCommentGrpOnFieldClosingLine && isLastCommentGrpOnSigClosingLine) {
537+
fl.Closing += 1
538+
f.addNewline(fl.Closing)
539+
}
540+
}
541+
handleMultiLine(sign.Params)
542+
if sign.Results != nil {
543+
lastResultLine := f.Line(sign.Results.List[len(sign.Results.List)-1].End())
544+
isLastResultOnParamClosingLine := sign.Params != nil && lastResultLine == f.Line(sign.Params.Closing)
545+
if !isLastResultOnParamClosingLine {
546+
handleMultiLine(sign.Results)
547+
}
537548
}
538549
}
539550
}

testdata/scripts/func-newlines.txt

+32
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,22 @@ func f(p1 string,
258258
println("body")
259259
return 0
260260
}
261+
262+
func a() {
263+
f := func(s string,
264+
b bool,
265+
) {
266+
// foo
267+
}
268+
}
269+
270+
func f(p1 string,
271+
p2 string) (int, string,
272+
/* baz */) {
273+
274+
println("body")
275+
return 0, ""
276+
}
261277
-- foo.go.golden --
262278
package p
263279

@@ -469,3 +485,19 @@ func f(p1 string,
469485
println("body")
470486
return 0
471487
}
488+
489+
func a() {
490+
f := func(s string,
491+
b bool,
492+
) {
493+
// foo
494+
}
495+
}
496+
497+
func f(p1 string,
498+
p2 string) (int, string,
499+
500+
/* baz */) {
501+
println("body")
502+
return 0, ""
503+
}

0 commit comments

Comments
 (0)