Skip to content

Commit a973e5a

Browse files
committed
refactor: apply review suggestions
1 parent 30b7e92 commit a973e5a

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

crates/ide_completion/src/completions/fn_param.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
5959
let function = ctx.token.ancestors().find_map(ast::Fn::cast)?;
6060
let param_list = function.param_list()?;
6161

62-
remove_duplicated(&mut file_params, param_list.params())?;
62+
remove_duplicated(&mut file_params, param_list.params());
6363

6464
let self_completion_items = ["self", "&self", "mut self", "&mut self"];
6565
if should_add_self_completions(ctx, param_list) {
@@ -69,24 +69,24 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
6969
}
7070

7171
file_params.into_iter().try_for_each(|(whole_param, binding)| {
72-
Some(add_new_item_to_acc(ctx, acc, surround_with_commas(ctx, whole_param)?, binding))
72+
Some(add_new_item_to_acc(ctx, acc, surround_with_commas(ctx, whole_param), binding))
7373
})?;
7474

7575
Some(())
7676
}
7777

7878
fn remove_duplicated(
7979
file_params: &mut FxHashMap<String, String>,
80-
mut fn_params: ast::AstChildren<ast::Param>,
81-
) -> Option<()> {
82-
fn_params.try_for_each(|param| {
80+
fn_params: ast::AstChildren<ast::Param>,
81+
) {
82+
fn_params.for_each(|param| {
8383
let whole_param = param.syntax().text().to_string();
8484
file_params.remove(&whole_param);
8585

86-
let binding = param.pat()?.syntax().text().to_string();
87-
88-
file_params.retain(|_, v| v != &binding);
89-
Some(())
86+
if let Some(pattern) = param.pat() {
87+
let binding = pattern.syntax().text().to_string();
88+
file_params.retain(|_, v| v != &binding);
89+
}
9090
})
9191
}
9292

@@ -97,13 +97,20 @@ fn should_add_self_completions(ctx: &CompletionContext, param_list: ast::ParamLi
9797
inside_impl && no_params
9898
}
9999

100-
fn surround_with_commas(ctx: &CompletionContext, param: String) -> Option<String> {
100+
fn surround_with_commas(ctx: &CompletionContext, param: String) -> String {
101+
match fallible_surround_with_commas(ctx, &param) {
102+
Some(surrounded) => surrounded,
103+
// fallback to the original parameter
104+
None => param,
105+
}
106+
}
107+
108+
fn fallible_surround_with_commas(ctx: &CompletionContext, param: &str) -> Option<String> {
101109
let next_token = {
102110
let t = ctx.token.next_token()?;
103-
if !matches!(t.kind(), SyntaxKind::WHITESPACE) {
104-
t
105-
} else {
106-
t.next_token()?
111+
match t.kind() {
112+
SyntaxKind::WHITESPACE => t.next_token()?,
113+
_ => t,
107114
}
108115
};
109116

0 commit comments

Comments
 (0)