Auto merge of #12085 - rainy-me:fix_fn_param, r=Veykril
fix: fn_param completion lookup close #12073 caused by #12040
This commit is contained in:
commit
0b49c93b91
@ -15,8 +15,7 @@
|
||||
|
||||
/// Complete repeated parameters, both name and type. For example, if all
|
||||
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
|
||||
/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
|
||||
/// suggested.
|
||||
/// `spam: &mut Spam` insert text/label will be suggested.
|
||||
///
|
||||
/// Also complete parameters for closure or local functions from the surrounding defined locals.
|
||||
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)
|
||||
};
|
||||
|
||||
let comma_wrapper = comma_wrapper(ctx);
|
||||
let mut add_new_item_to_acc = |label: &str, lookup: String| {
|
||||
let mut add_new_item_to_acc = |label: &str| {
|
||||
let mk_item = |label: &str, range: TextRange| {
|
||||
CompletionItem::new(CompletionItemKind::Binding, range, label)
|
||||
};
|
||||
let item = match &comma_wrapper {
|
||||
Some((fmt, range, lookup)) => mk_item(&fmt(label), *range).lookup_by(lookup).to_owned(),
|
||||
None => mk_item(label, ctx.source_range()).lookup_by(lookup).to_owned(),
|
||||
Some((fmt, range)) => mk_item(&fmt(label), *range),
|
||||
None => mk_item(label, ctx.source_range()),
|
||||
};
|
||||
// Completion lookup is omitted intentionally here.
|
||||
// See the full discussion: https://github.com/rust-lang/rust-analyzer/issues/12073
|
||||
item.add_to(acc)
|
||||
};
|
||||
|
||||
@ -44,7 +45,7 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
|
||||
ParamKind::Closure(closure) => {
|
||||
let stmt_list = closure.syntax().ancestors().find_map(ast::StmtList::cast)?;
|
||||
params_from_stmt_list_scope(ctx, stmt_list, |name, ty| {
|
||||
add_new_item_to_acc(&format!("{name}: {ty}"), name.to_string());
|
||||
add_new_item_to_acc(&format!("{name}: {ty}"));
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -56,7 +57,7 @@ fn fill_fn_params(
|
||||
ctx: &CompletionContext,
|
||||
function: &ast::Fn,
|
||||
param_list: &ast::ParamList,
|
||||
mut add_new_item_to_acc: impl FnMut(&str, String),
|
||||
mut add_new_item_to_acc: impl FnMut(&str),
|
||||
) {
|
||||
let mut file_params = FxHashMap::default();
|
||||
|
||||
@ -96,18 +97,13 @@ fn fill_fn_params(
|
||||
file_params.entry(format!("{name}: {ty}")).or_insert(name.to_string());
|
||||
});
|
||||
}
|
||||
|
||||
remove_duplicated(&mut file_params, param_list.params());
|
||||
let self_completion_items = ["self", "&self", "mut self", "&mut self"];
|
||||
if should_add_self_completions(ctx, param_list) {
|
||||
self_completion_items
|
||||
.into_iter()
|
||||
.for_each(|self_item| add_new_item_to_acc(self_item, self_item.to_string()));
|
||||
self_completion_items.into_iter().for_each(|self_item| add_new_item_to_acc(self_item));
|
||||
}
|
||||
|
||||
file_params
|
||||
.into_iter()
|
||||
.for_each(|(whole_param, binding)| add_new_item_to_acc(&whole_param, binding));
|
||||
file_params.keys().for_each(|whole_param| add_new_item_to_acc(whole_param));
|
||||
}
|
||||
|
||||
fn params_from_stmt_list_scope(
|
||||
@ -161,7 +157,7 @@ fn should_add_self_completions(ctx: &CompletionContext, param_list: &ast::ParamL
|
||||
inside_impl && no_params
|
||||
}
|
||||
|
||||
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange, String)> {
|
||||
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange)> {
|
||||
let param = ctx.token.ancestors().find(|node| node.kind() == SyntaxKind::PARAM)?;
|
||||
|
||||
let next_token_kind = {
|
||||
@ -183,9 +179,5 @@ fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, Te
|
||||
matches!(prev_token_kind, SyntaxKind::COMMA | SyntaxKind::L_PAREN | SyntaxKind::PIPE);
|
||||
let leading = if has_leading_comma { "" } else { ", " };
|
||||
|
||||
Some((
|
||||
move |label: &_| (format!("{}{}{}", leading, label, trailing)),
|
||||
param.text_range(),
|
||||
format!("{}{}", leading, param.text()),
|
||||
))
|
||||
Some((move |label: &_| (format!("{}{}{}", leading, label, trailing)), param.text_range()))
|
||||
}
|
||||
|
@ -570,7 +570,7 @@ fn main() {
|
||||
fn complete_fn_param() {
|
||||
// has mut kw
|
||||
check_edit(
|
||||
"mut ba",
|
||||
"mut bar: u32",
|
||||
r#"
|
||||
fn f(foo: (), mut bar: u32) {}
|
||||
fn g(foo: (), mut ba$0)
|
||||
@ -583,7 +583,7 @@ fn g(foo: (), mut bar: u32)
|
||||
|
||||
// has type param
|
||||
check_edit(
|
||||
"mut ba: u32",
|
||||
"mut bar: u32",
|
||||
r#"
|
||||
fn g(foo: (), mut ba$0: u32)
|
||||
fn f(foo: (), mut bar: u32) {}
|
||||
@ -599,7 +599,7 @@ fn f(foo: (), mut bar: u32) {}
|
||||
fn complete_fn_mut_param_add_comma() {
|
||||
// add leading and trailing comma
|
||||
check_edit(
|
||||
", mut ba",
|
||||
", mut bar: u32,",
|
||||
r#"
|
||||
fn f(foo: (), mut bar: u32) {}
|
||||
fn g(foo: ()mut ba$0 baz: ())
|
||||
@ -614,7 +614,7 @@ fn g(foo: (), mut bar: u32, baz: ())
|
||||
#[test]
|
||||
fn complete_fn_mut_param_has_attribute() {
|
||||
check_edit(
|
||||
"mut ba",
|
||||
r#"#[baz = "qux"] mut bar: u32"#,
|
||||
r#"
|
||||
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
|
||||
fn g(foo: (), mut ba$0)
|
||||
@ -626,7 +626,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
|
||||
);
|
||||
|
||||
check_edit(
|
||||
r#"#[baz = "qux"] mut ba"#,
|
||||
r#"#[baz = "qux"] mut bar: u32"#,
|
||||
r#"
|
||||
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
|
||||
fn g(foo: (), #[baz = "qux"] mut ba$0)
|
||||
@ -638,7 +638,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
|
||||
);
|
||||
|
||||
check_edit(
|
||||
r#", #[baz = "qux"] mut ba"#,
|
||||
r#", #[baz = "qux"] mut bar: u32"#,
|
||||
r#"
|
||||
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
|
||||
fn g(foo: ()#[baz = "qux"] mut ba$0)
|
||||
|
@ -67,6 +67,17 @@ fn bar(file_id: u32, $0) {}
|
||||
kw mut
|
||||
"#]],
|
||||
);
|
||||
|
||||
check(
|
||||
r#"
|
||||
fn f(#[foo = "bar"] baz: u32,) {}
|
||||
fn g(baz: (), ba$0)
|
||||
"#,
|
||||
expect![[r##"
|
||||
kw ref
|
||||
kw mut
|
||||
"##]],
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Loading…
Reference in New Issue
Block a user