@@ -511,29 +511,40 @@ func (f *fumpter) applyPre(c *astutil.Cursor) {
511
511
if sign != nil {
512
512
endLine := f .Line (sign .End ())
513
513
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
+ }
537
548
}
538
549
}
539
550
}
0 commit comments