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:
bors 2022-04-27 10:43:43 +00:00
commit 0b49c93b91
3 changed files with 29 additions and 26 deletions

View file

@ -15,8 +15,7 @@ use crate::{
/// Complete repeated parameters, both name and type. For example, if all /// Complete repeated parameters, both name and type. For example, if all
/// functions in a file have a `spam: &mut Spam` parameter, a completion with /// 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 /// `spam: &mut Spam` insert text/label will be suggested.
/// suggested.
/// ///
/// Also complete parameters for closure or local functions from the surrounding defined locals. /// 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<()> { 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 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| { let mk_item = |label: &str, range: TextRange| {
CompletionItem::new(CompletionItemKind::Binding, range, label) CompletionItem::new(CompletionItemKind::Binding, range, label)
}; };
let item = match &comma_wrapper { let item = match &comma_wrapper {
Some((fmt, range, lookup)) => mk_item(&fmt(label), *range).lookup_by(lookup).to_owned(), Some((fmt, range)) => mk_item(&fmt(label), *range),
None => mk_item(label, ctx.source_range()).lookup_by(lookup).to_owned(), 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) item.add_to(acc)
}; };
@ -44,7 +45,7 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
ParamKind::Closure(closure) => { ParamKind::Closure(closure) => {
let stmt_list = closure.syntax().ancestors().find_map(ast::StmtList::cast)?; let stmt_list = closure.syntax().ancestors().find_map(ast::StmtList::cast)?;
params_from_stmt_list_scope(ctx, stmt_list, |name, ty| { 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, ctx: &CompletionContext,
function: &ast::Fn, function: &ast::Fn,
param_list: &ast::ParamList, 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(); 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()); file_params.entry(format!("{name}: {ty}")).or_insert(name.to_string());
}); });
} }
remove_duplicated(&mut file_params, param_list.params()); remove_duplicated(&mut file_params, param_list.params());
let self_completion_items = ["self", "&self", "mut self", "&mut self"]; let self_completion_items = ["self", "&self", "mut self", "&mut self"];
if should_add_self_completions(ctx, param_list) { if should_add_self_completions(ctx, param_list) {
self_completion_items self_completion_items.into_iter().for_each(|self_item| add_new_item_to_acc(self_item));
.into_iter()
.for_each(|self_item| add_new_item_to_acc(self_item, self_item.to_string()));
} }
file_params file_params.keys().for_each(|whole_param| add_new_item_to_acc(whole_param));
.into_iter()
.for_each(|(whole_param, binding)| add_new_item_to_acc(&whole_param, binding));
} }
fn params_from_stmt_list_scope( 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 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 param = ctx.token.ancestors().find(|node| node.kind() == SyntaxKind::PARAM)?;
let next_token_kind = { 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); matches!(prev_token_kind, SyntaxKind::COMMA | SyntaxKind::L_PAREN | SyntaxKind::PIPE);
let leading = if has_leading_comma { "" } else { ", " }; let leading = if has_leading_comma { "" } else { ", " };
Some(( Some((move |label: &_| (format!("{}{}{}", leading, label, trailing)), param.text_range()))
move |label: &_| (format!("{}{}{}", leading, label, trailing)),
param.text_range(),
format!("{}{}", leading, param.text()),
))
} }

View file

@ -570,7 +570,7 @@ fn main() {
fn complete_fn_param() { fn complete_fn_param() {
// has mut kw // has mut kw
check_edit( check_edit(
"mut ba", "mut bar: u32",
r#" r#"
fn f(foo: (), mut bar: u32) {} fn f(foo: (), mut bar: u32) {}
fn g(foo: (), mut ba$0) fn g(foo: (), mut ba$0)
@ -583,7 +583,7 @@ fn g(foo: (), mut bar: u32)
// has type param // has type param
check_edit( check_edit(
"mut ba: u32", "mut bar: u32",
r#" r#"
fn g(foo: (), mut ba$0: u32) fn g(foo: (), mut ba$0: u32)
fn f(foo: (), mut bar: u32) {} fn f(foo: (), mut bar: u32) {}
@ -599,7 +599,7 @@ fn f(foo: (), mut bar: u32) {}
fn complete_fn_mut_param_add_comma() { fn complete_fn_mut_param_add_comma() {
// add leading and trailing comma // add leading and trailing comma
check_edit( check_edit(
", mut ba", ", mut bar: u32,",
r#" r#"
fn f(foo: (), mut bar: u32) {} fn f(foo: (), mut bar: u32) {}
fn g(foo: ()mut ba$0 baz: ()) fn g(foo: ()mut ba$0 baz: ())
@ -614,7 +614,7 @@ fn g(foo: (), mut bar: u32, baz: ())
#[test] #[test]
fn complete_fn_mut_param_has_attribute() { fn complete_fn_mut_param_has_attribute() {
check_edit( check_edit(
"mut ba", r#"#[baz = "qux"] mut bar: u32"#,
r#" r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {} fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), mut ba$0) fn g(foo: (), mut ba$0)
@ -626,7 +626,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
); );
check_edit( check_edit(
r#"#[baz = "qux"] mut ba"#, r#"#[baz = "qux"] mut bar: u32"#,
r#" r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {} fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: (), #[baz = "qux"] mut ba$0) fn g(foo: (), #[baz = "qux"] mut ba$0)
@ -638,7 +638,7 @@ fn g(foo: (), #[baz = "qux"] mut bar: u32)
); );
check_edit( check_edit(
r#", #[baz = "qux"] mut ba"#, r#", #[baz = "qux"] mut bar: u32"#,
r#" r#"
fn f(foo: (), #[baz = "qux"] mut bar: u32) {} fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
fn g(foo: ()#[baz = "qux"] mut ba$0) fn g(foo: ()#[baz = "qux"] mut ba$0)

View file

@ -67,6 +67,17 @@ fn bar(file_id: u32, $0) {}
kw mut kw mut
"#]], "#]],
); );
check(
r#"
fn f(#[foo = "bar"] baz: u32,) {}
fn g(baz: (), ba$0)
"#,
expect![[r##"
kw ref
kw mut
"##]],
)
} }
#[test] #[test]