@@ -6,7 +6,7 @@ use syntax::{
6
6
ast:: {
7
7
self ,
8
8
edit:: { AstNodeEdit , IndentLevel } ,
9
- make, ArgListOwner , AstNode , ModuleItemOwner ,
9
+ make, ArgList , ArgListOwner , AstNode , ModuleItemOwner ,
10
10
} ,
11
11
SyntaxKind , SyntaxNode , TextSize ,
12
12
} ;
@@ -17,6 +17,20 @@ use crate::{
17
17
AssistContext , AssistId , AssistKind , Assists ,
18
18
} ;
19
19
20
+ enum FuncExpr < ' a > {
21
+ Func ( & ' a ast:: CallExpr ) ,
22
+ Method ( & ' a ast:: MethodCallExpr ) ,
23
+ }
24
+
25
+ impl < ' a > FuncExpr < ' a > {
26
+ fn arg_list ( & self ) -> Option < ArgList > {
27
+ match * self {
28
+ FuncExpr :: Func ( fn_call) => fn_call. arg_list ( ) ,
29
+ FuncExpr :: Method ( m_call) => m_call. arg_list ( ) ,
30
+ }
31
+ }
32
+ }
33
+
20
34
// Assist: generate_function
21
35
//
22
36
// Adds a stub function with a signature matching the function under the cursor.
@@ -164,7 +178,7 @@ impl FunctionBuilder {
164
178
let needs_pub = target_module. is_some ( ) ;
165
179
let target_module = target_module. or_else ( || ctx. sema . scope ( target. syntax ( ) ) . module ( ) ) ?;
166
180
let fn_name = fn_name ( path) ?;
167
- let ( type_params, params) = fn_args ( ctx, target_module, call) ?;
181
+ let ( type_params, params) = fn_args ( ctx, target_module, FuncExpr :: Func ( call) ) ?;
168
182
169
183
let await_expr = call. syntax ( ) . parent ( ) . and_then ( ast:: AwaitExpr :: cast) ;
170
184
let is_async = await_expr. is_some ( ) ;
@@ -229,7 +243,7 @@ impl FunctionBuilder {
229
243
let needs_pub = false ;
230
244
let target_module = target_module. or_else ( || ctx. sema . scope ( target. syntax ( ) ) . module ( ) ) ?;
231
245
let fn_name = make:: name ( & name. text ( ) ) ;
232
- let ( type_params, params) = method_args ( ctx, target_module, call) ?;
246
+ let ( type_params, params) = fn_args ( ctx, target_module, FuncExpr :: Method ( call) ) ?;
233
247
234
248
let await_expr = call. syntax ( ) . parent ( ) . and_then ( ast:: AwaitExpr :: cast) ;
235
249
let is_async = await_expr. is_some ( ) ;
@@ -342,7 +356,7 @@ fn fn_name(call: &ast::Path) -> Option<ast::Name> {
342
356
fn fn_args (
343
357
ctx : & AssistContext ,
344
358
target_module : hir:: Module ,
345
- call : & ast :: CallExpr ,
359
+ call : FuncExpr ,
346
360
) -> Option < ( Option < ast:: GenericParamList > , ast:: ParamList ) > {
347
361
let mut arg_names = Vec :: new ( ) ;
348
362
let mut arg_types = Vec :: new ( ) ;
@@ -370,41 +384,17 @@ fn fn_args(
370
384
let params = arg_names. into_iter ( ) . zip ( arg_types) . map ( |( name, ty) | {
371
385
make:: param ( make:: ext:: simple_ident_pat ( make:: name ( & name) ) . into ( ) , make:: ty ( & ty) )
372
386
} ) ;
373
- Some ( ( None , make:: param_list ( None , params) ) )
374
- }
375
387
376
- fn method_args (
377
- ctx : & AssistContext ,
378
- target_module : hir:: Module ,
379
- call : & ast:: MethodCallExpr ,
380
- ) -> Option < ( Option < ast:: GenericParamList > , ast:: ParamList ) > {
381
- let mut arg_names = Vec :: new ( ) ;
382
- let mut arg_types = Vec :: new ( ) ;
383
- for arg in call. arg_list ( ) ?. args ( ) {
384
- arg_names. push ( match fn_arg_name ( & arg) {
385
- Some ( name) => name,
386
- None => String :: from ( "arg" ) ,
387
- } ) ;
388
- arg_types. push ( match fn_arg_type ( ctx, target_module, & arg) {
389
- Some ( ty) => {
390
- if ty. len ( ) > 0 && ty. starts_with ( '&' ) {
391
- if let Some ( ( new_ty, _) ) = useless_type_special_case ( "" , & ty[ 1 ..] . to_owned ( ) ) {
392
- new_ty
393
- } else {
394
- ty
395
- }
396
- } else {
397
- ty
398
- }
399
- }
400
- None => String :: from ( "()" ) ,
401
- } ) ;
402
- }
403
- deduplicate_arg_names ( & mut arg_names) ;
404
- let params = arg_names. into_iter ( ) . zip ( arg_types) . map ( |( name, ty) | {
405
- make:: param ( make:: ext:: simple_ident_pat ( make:: name ( & name) ) . into ( ) , make:: ty ( & ty) )
406
- } ) ;
407
- Some ( ( None , make:: param_list ( Some ( make:: self_param ( ) ) , params) ) )
388
+ Some ( (
389
+ None ,
390
+ make:: param_list (
391
+ match call {
392
+ FuncExpr :: Func ( _) => None ,
393
+ FuncExpr :: Method ( _) => Some ( make:: self_param ( ) ) ,
394
+ } ,
395
+ params,
396
+ ) ,
397
+ ) )
408
398
}
409
399
410
400
/// Makes duplicate argument names unique by appending incrementing numbers.
0 commit comments