Skip to content

Commit

Permalink
Auto merge of rust-lang#12085 - rainy-me:fix_fn_param, r=Veykril
Browse files Browse the repository at this point in the history
fix: fn_param completion lookup

close rust-lang#12073 caused by rust-lang#12040
  • Loading branch information
bors committed Apr 27, 2022
2 parents 198c075 + c1685e5 commit 0b49c93
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 26 deletions.
32 changes: 12 additions & 20 deletions crates/ide_completion/src/completions/fn_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use crate::{

/// 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<()> {
Expand All @@ -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)
};

Expand All @@ -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}"));
});
}
}
Expand All @@ -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();

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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 = {
Expand All @@ -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()))
}
12 changes: 6 additions & 6 deletions crates/ide_completion/src/render/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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) {}
Expand All @@ -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: ())
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions crates/ide_completion/src/tests/fn_param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 0b49c93

Please sign in to comment.