Skip to content

Commit 2cd925c

Browse files
committed
fix: remove lookup and handle mut kw case
1 parent 198c075 commit 2cd925c

File tree

3 files changed

+43
-26
lines changed

3 files changed

+43
-26
lines changed

crates/ide_completion/src/completions/fn_param.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use crate::{
1515

1616
/// Complete repeated parameters, both name and type. For example, if all
1717
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
18-
/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
19-
/// suggested.
18+
/// `spam: &mut Spam` insert text/label will be suggested.
2019
///
2120
/// Also complete parameters for closure or local functions from the surrounding defined locals.
2221
pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
@@ -26,13 +25,13 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
2625
};
2726

2827
let comma_wrapper = comma_wrapper(ctx);
29-
let mut add_new_item_to_acc = |label: &str, lookup: String| {
28+
let mut add_new_item_to_acc = |label: &str| {
3029
let mk_item = |label: &str, range: TextRange| {
3130
CompletionItem::new(CompletionItemKind::Binding, range, label)
3231
};
3332
let item = match &comma_wrapper {
34-
Some((fmt, range, lookup)) => mk_item(&fmt(label), *range).lookup_by(lookup).to_owned(),
35-
None => mk_item(label, ctx.source_range()).lookup_by(lookup).to_owned(),
33+
Some((fmt, range)) => mk_item(&fmt(label), *range),
34+
None => mk_item(label, ctx.source_range()),
3635
};
3736
item.add_to(acc)
3837
};
@@ -44,7 +43,7 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
4443
ParamKind::Closure(closure) => {
4544
let stmt_list = closure.syntax().ancestors().find_map(ast::StmtList::cast)?;
4645
params_from_stmt_list_scope(ctx, stmt_list, |name, ty| {
47-
add_new_item_to_acc(&format!("{name}: {ty}"), name.to_string());
46+
add_new_item_to_acc(&format!("{name}: {ty}"));
4847
});
4948
}
5049
}
@@ -56,7 +55,7 @@ fn fill_fn_params(
5655
ctx: &CompletionContext,
5756
function: &ast::Fn,
5857
param_list: &ast::ParamList,
59-
mut add_new_item_to_acc: impl FnMut(&str, String),
58+
mut add_new_item_to_acc: impl FnMut(&str),
6059
) {
6160
let mut file_params = FxHashMap::default();
6261

@@ -96,18 +95,13 @@ fn fill_fn_params(
9695
file_params.entry(format!("{name}: {ty}")).or_insert(name.to_string());
9796
});
9897
}
99-
10098
remove_duplicated(&mut file_params, param_list.params());
10199
let self_completion_items = ["self", "&self", "mut self", "&mut self"];
102100
if should_add_self_completions(ctx, param_list) {
103-
self_completion_items
104-
.into_iter()
105-
.for_each(|self_item| add_new_item_to_acc(self_item, self_item.to_string()));
101+
self_completion_items.into_iter().for_each(|self_item| add_new_item_to_acc(self_item));
106102
}
107103

108-
file_params
109-
.into_iter()
110-
.for_each(|(whole_param, binding)| add_new_item_to_acc(&whole_param, binding));
104+
file_params.keys().for_each(|whole_param| add_new_item_to_acc(whole_param));
111105
}
112106

113107
fn params_from_stmt_list_scope(
@@ -161,16 +155,16 @@ fn should_add_self_completions(ctx: &CompletionContext, param_list: &ast::ParamL
161155
inside_impl && no_params
162156
}
163157

164-
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange, String)> {
158+
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange)> {
165159
let param = ctx.token.ancestors().find(|node| node.kind() == SyntaxKind::PARAM)?;
166-
160+
let is_mut_token = (ctx.token.kind() == SyntaxKind::MUT_KW).then(|| &ctx.token);
167161
let next_token_kind = {
168-
let t = param.last_token()?.next_token()?;
162+
let t = is_mut_token.or(param.last_token().as_ref())?.next_token()?;
169163
let t = algo::skip_whitespace_token(t, Direction::Next)?;
170164
t.kind()
171165
};
172166
let prev_token_kind = {
173-
let t = param.first_token()?.prev_token()?;
167+
let t = param.first_token().as_ref()?.prev_token()?;
174168
let t = algo::skip_whitespace_token(t, Direction::Prev)?;
175169
t.kind()
176170
};
@@ -185,7 +179,6 @@ fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, Te
185179

186180
Some((
187181
move |label: &_| (format!("{}{}{}", leading, label, trailing)),
188-
param.text_range(),
189-
format!("{}{}", leading, param.text()),
182+
is_mut_token.map(|token| token.text_range()).unwrap_or_else(|| param.text_range()),
190183
))
191184
}

crates/ide_completion/src/render/function.rs

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ fn main() {
570570
fn complete_fn_param() {
571571
// has mut kw
572572
check_edit(
573-
"mut ba",
573+
"mut bar: u32",
574574
r#"
575575
fn f(foo: (), mut bar: u32) {}
576576
fn g(foo: (), mut ba$0)
@@ -583,14 +583,27 @@ fn g(foo: (), mut bar: u32)
583583

584584
// has type param
585585
check_edit(
586-
"mut ba: u32",
586+
"mut bar: u32",
587587
r#"
588588
fn g(foo: (), mut ba$0: u32)
589589
fn f(foo: (), mut bar: u32) {}
590590
"#,
591591
r#"
592592
fn g(foo: (), mut bar: u32)
593593
fn f(foo: (), mut bar: u32) {}
594+
"#,
595+
);
596+
597+
// is mut kw
598+
check_edit(
599+
"mut foo: u32",
600+
r#"
601+
fn f(mut foo: u32) {}
602+
fn g(mut$0) {}
603+
"#,
604+
r#"
605+
fn f(mut foo: u32) {}
606+
fn g(mut foo: u32) {}
594607
"#,
595608
);
596609
}
@@ -599,7 +612,7 @@ fn f(foo: (), mut bar: u32) {}
599612
fn complete_fn_mut_param_add_comma() {
600613
// add leading and trailing comma
601614
check_edit(
602-
", mut ba",
615+
", mut bar: u32,",
603616
r#"
604617
fn f(foo: (), mut bar: u32) {}
605618
fn g(foo: ()mut ba$0 baz: ())
@@ -614,7 +627,7 @@ fn g(foo: (), mut bar: u32, baz: ())
614627
#[test]
615628
fn complete_fn_mut_param_has_attribute() {
616629
check_edit(
617-
"mut ba",
630+
r#"#[baz = "qux"] mut bar: u32"#,
618631
r#"
619632
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
620633
fn g(foo: (), mut ba$0)
@@ -626,7 +639,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
626639
);
627640

628641
check_edit(
629-
r#"#[baz = "qux"] mut ba"#,
642+
r#"#[baz = "qux"] mut bar: u32"#,
630643
r#"
631644
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
632645
fn g(foo: (), #[baz = "qux"] mut ba$0)
@@ -638,7 +651,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
638651
);
639652

640653
check_edit(
641-
r#", #[baz = "qux"] mut ba"#,
654+
r#", #[baz = "qux"] mut bar: u32"#,
642655
r#"
643656
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
644657
fn g(foo: ()#[baz = "qux"] mut ba$0)

crates/ide_completion/src/tests/fn_param.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ fn bar(file_id: u32, $0) {}
6767
kw mut
6868
"#]],
6969
);
70+
71+
check(
72+
r#"
73+
fn f(#[foo = "bar"] baz: u32,) {}
74+
fn g(baz: (), ba$0)
75+
"#,
76+
expect![[r##"
77+
kw ref
78+
kw mut
79+
"##]],
80+
)
7081
}
7182

7283
#[test]

0 commit comments

Comments
 (0)