Skip to content

Commit 0b49c93

Browse files
committed
Auto merge of rust-lang#12085 - rainy-me:fix_fn_param, r=Veykril
fix: fn_param completion lookup close rust-lang#12073 caused by rust-lang#12040
2 parents 198c075 + c1685e5 commit 0b49c93

File tree

3 files changed

+29
-26
lines changed

3 files changed

+29
-26
lines changed

crates/ide_completion/src/completions/fn_param.rs

+12-20
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,14 +25,16 @@ 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
};
36+
// Completion lookup is omitted intentionally here.
37+
// See the full discussion: https://github.com/rust-lang/rust-analyzer/issues/12073
3738
item.add_to(acc)
3839
};
3940

@@ -44,7 +45,7 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
4445
ParamKind::Closure(closure) => {
4546
let stmt_list = closure.syntax().ancestors().find_map(ast::StmtList::cast)?;
4647
params_from_stmt_list_scope(ctx, stmt_list, |name, ty| {
47-
add_new_item_to_acc(&format!("{name}: {ty}"), name.to_string());
48+
add_new_item_to_acc(&format!("{name}: {ty}"));
4849
});
4950
}
5051
}
@@ -56,7 +57,7 @@ fn fill_fn_params(
5657
ctx: &CompletionContext,
5758
function: &ast::Fn,
5859
param_list: &ast::ParamList,
59-
mut add_new_item_to_acc: impl FnMut(&str, String),
60+
mut add_new_item_to_acc: impl FnMut(&str),
6061
) {
6162
let mut file_params = FxHashMap::default();
6263

@@ -96,18 +97,13 @@ fn fill_fn_params(
9697
file_params.entry(format!("{name}: {ty}")).or_insert(name.to_string());
9798
});
9899
}
99-
100100
remove_duplicated(&mut file_params, param_list.params());
101101
let self_completion_items = ["self", "&self", "mut self", "&mut self"];
102102
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()));
103+
self_completion_items.into_iter().for_each(|self_item| add_new_item_to_acc(self_item));
106104
}
107105

108-
file_params
109-
.into_iter()
110-
.for_each(|(whole_param, binding)| add_new_item_to_acc(&whole_param, binding));
106+
file_params.keys().for_each(|whole_param| add_new_item_to_acc(whole_param));
111107
}
112108

113109
fn params_from_stmt_list_scope(
@@ -161,7 +157,7 @@ fn should_add_self_completions(ctx: &CompletionContext, param_list: &ast::ParamL
161157
inside_impl && no_params
162158
}
163159

164-
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange, String)> {
160+
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange)> {
165161
let param = ctx.token.ancestors().find(|node| node.kind() == SyntaxKind::PARAM)?;
166162

167163
let next_token_kind = {
@@ -183,9 +179,5 @@ fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, Te
183179
matches!(prev_token_kind, SyntaxKind::COMMA | SyntaxKind::L_PAREN | SyntaxKind::PIPE);
184180
let leading = if has_leading_comma { "" } else { ", " };
185181

186-
Some((
187-
move |label: &_| (format!("{}{}{}", leading, label, trailing)),
188-
param.text_range(),
189-
format!("{}{}", leading, param.text()),
190-
))
182+
Some((move |label: &_| (format!("{}{}{}", leading, label, trailing)), param.text_range()))
191183
}

crates/ide_completion/src/render/function.rs

+6-6
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,7 +583,7 @@ 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) {}
@@ -599,7 +599,7 @@ fn f(foo: (), mut bar: u32) {}
599599
fn complete_fn_mut_param_add_comma() {
600600
// add leading and trailing comma
601601
check_edit(
602-
", mut ba",
602+
", mut bar: u32,",
603603
r#"
604604
fn f(foo: (), mut bar: u32) {}
605605
fn g(foo: ()mut ba$0 baz: ())
@@ -614,7 +614,7 @@ fn g(foo: (), mut bar: u32, baz: ())
614614
#[test]
615615
fn complete_fn_mut_param_has_attribute() {
616616
check_edit(
617-
"mut ba",
617+
r#"#[baz = "qux"] mut bar: u32"#,
618618
r#"
619619
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
620620
fn g(foo: (), mut ba$0)
@@ -626,7 +626,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
626626
);
627627

628628
check_edit(
629-
r#"#[baz = "qux"] mut ba"#,
629+
r#"#[baz = "qux"] mut bar: u32"#,
630630
r#"
631631
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
632632
fn g(foo: (), #[baz = "qux"] mut ba$0)
@@ -638,7 +638,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
638638
);
639639

640640
check_edit(
641-
r#", #[baz = "qux"] mut ba"#,
641+
r#", #[baz = "qux"] mut bar: u32"#,
642642
r#"
643643
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
644644
fn g(foo: ()#[baz = "qux"] mut ba$0)

crates/ide_completion/src/tests/fn_param.rs

+11
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)