@@ -363,6 +363,8 @@ func extractFunctionMethod(fset *token.FileSet, start, end token.Pos, src []byte
363
363
}
364
364
}
365
365
366
+ reorderParams (params , paramTypes )
367
+
366
368
// Find the function literal that encloses the selection. The enclosing function literal
367
369
// may not be the enclosing function declaration (i.e. 'outer'). For example, in the
368
370
// following block:
@@ -631,6 +633,33 @@ func extractFunctionMethod(fset *token.FileSet, start, end token.Pos, src []byte
631
633
}, nil
632
634
}
633
635
636
+ // isSelector reports if e is the selector expr <x>, <sel>.
637
+ func isSelector (e ast.Expr , x , sel string ) bool {
638
+ selectorExpr , ok := e .(* ast.SelectorExpr )
639
+ if ! ok {
640
+ return false
641
+ }
642
+ ident , ok := selectorExpr .X .(* ast.Ident )
643
+ if ! ok {
644
+ return false
645
+ }
646
+ return ident .Name == x && selectorExpr .Sel .Name == sel
647
+ }
648
+
649
+ // reorderParams reorders the given parameters in-place to follow common Go conventions.
650
+ func reorderParams (params []ast.Expr , paramTypes []* ast.Field ) {
651
+ // Move Context parameter (if any) to front.
652
+ for i , t := range paramTypes {
653
+ if isSelector (t .Type , "context" , "Context" ) {
654
+ p , t := params [i ], paramTypes [i ]
655
+ copy (params [1 :], params [:i ])
656
+ copy (paramTypes [1 :], paramTypes [:i ])
657
+ params [0 ], paramTypes [0 ] = p , t
658
+ break
659
+ }
660
+ }
661
+ }
662
+
634
663
// adjustRangeForCommentsAndWhiteSpace adjusts the given range to exclude unnecessary leading or
635
664
// trailing whitespace characters from selection as well as leading or trailing comments.
636
665
// In the following example, each line of the if statement is indented once. There are also two
0 commit comments