Skip to content

Commit b777e49

Browse files
committed
refactor: use single fn_args
1 parent 9ca7352 commit b777e49

File tree

1 file changed

+28
-38
lines changed

1 file changed

+28
-38
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 28 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syntax::{
66
ast::{
77
self,
88
edit::{AstNodeEdit, IndentLevel},
9-
make, ArgListOwner, AstNode, ModuleItemOwner,
9+
make, ArgList, ArgListOwner, AstNode, ModuleItemOwner,
1010
},
1111
SyntaxKind, SyntaxNode, TextSize,
1212
};
@@ -17,6 +17,20 @@ use crate::{
1717
AssistContext, AssistId, AssistKind, Assists,
1818
};
1919

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+
2034
// Assist: generate_function
2135
//
2236
// Adds a stub function with a signature matching the function under the cursor.
@@ -164,7 +178,7 @@ impl FunctionBuilder {
164178
let needs_pub = target_module.is_some();
165179
let target_module = target_module.or_else(|| ctx.sema.scope(target.syntax()).module())?;
166180
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))?;
168182

169183
let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
170184
let is_async = await_expr.is_some();
@@ -229,7 +243,7 @@ impl FunctionBuilder {
229243
let needs_pub = false;
230244
let target_module = target_module.or_else(|| ctx.sema.scope(target.syntax()).module())?;
231245
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))?;
233247

234248
let await_expr = call.syntax().parent().and_then(ast::AwaitExpr::cast);
235249
let is_async = await_expr.is_some();
@@ -342,7 +356,7 @@ fn fn_name(call: &ast::Path) -> Option<ast::Name> {
342356
fn fn_args(
343357
ctx: &AssistContext,
344358
target_module: hir::Module,
345-
call: &ast::CallExpr,
359+
call: FuncExpr,
346360
) -> Option<(Option<ast::GenericParamList>, ast::ParamList)> {
347361
let mut arg_names = Vec::new();
348362
let mut arg_types = Vec::new();
@@ -370,41 +384,17 @@ fn fn_args(
370384
let params = arg_names.into_iter().zip(arg_types).map(|(name, ty)| {
371385
make::param(make::ext::simple_ident_pat(make::name(&name)).into(), make::ty(&ty))
372386
});
373-
Some((None, make::param_list(None, params)))
374-
}
375387

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+
))
408398
}
409399

410400
/// Makes duplicate argument names unique by appending incrementing numbers.

0 commit comments

Comments
 (0)