mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 05:23:24 +00:00
Merge #5294
5294: Complete parameters more aggressively r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
31f2b9fbaa
1 changed files with 19 additions and 39 deletions
|
@ -1,4 +1,4 @@
|
|||
//! FIXME: write short doc here
|
||||
//! See `complete_fn_param`.
|
||||
|
||||
use ra_syntax::{
|
||||
ast::{self, ModuleItemOwner},
|
||||
|
@ -18,35 +18,40 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
|
|||
}
|
||||
|
||||
let mut params = FxHashMap::default();
|
||||
let mut me = None;
|
||||
for node in ctx.token.parent().ancestors() {
|
||||
let items = match_ast! {
|
||||
match node {
|
||||
ast::SourceFile(it) => it.items(),
|
||||
ast::ItemList(it) => it.items(),
|
||||
ast::FnDef(it) => {
|
||||
me = Some(it);
|
||||
continue;
|
||||
},
|
||||
_ => continue,
|
||||
}
|
||||
};
|
||||
for item in items {
|
||||
if let ast::ModuleItem::FnDef(func) = item {
|
||||
if Some(&func) == me.as_ref() {
|
||||
continue;
|
||||
}
|
||||
func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
|
||||
let text = param.syntax().text().to_string();
|
||||
params.entry(text).or_insert((0, param)).0 += 1;
|
||||
params.entry(text).or_insert(param);
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
params
|
||||
.into_iter()
|
||||
.filter_map(|(label, (count, param))| {
|
||||
.filter_map(|(label, param)| {
|
||||
let lookup = param.pat()?.syntax().text().to_string();
|
||||
if count < 2 {
|
||||
None
|
||||
} else {
|
||||
Some((label, lookup))
|
||||
}
|
||||
})
|
||||
.for_each(|(label, lookup)| {
|
||||
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
|
||||
.kind(crate::CompletionItemKind::Binding)
|
||||
.lookup_by(lookup)
|
||||
.add_to(acc)
|
||||
});
|
||||
|
@ -56,11 +61,11 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
|
|||
mod tests {
|
||||
use expect::{expect, Expect};
|
||||
|
||||
use crate::completion::{test_utils::do_completion, CompletionKind};
|
||||
use crate::completion::{test_utils::completion_list, CompletionKind};
|
||||
|
||||
fn check(ra_fixture: &str, expect: Expect) {
|
||||
let actual = do_completion(ra_fixture, CompletionKind::Magic);
|
||||
expect.assert_debug_eq(&actual);
|
||||
let actual = completion_list(ra_fixture, CompletionKind::Magic);
|
||||
expect.assert_eq(&actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -72,15 +77,7 @@ fn bar(file_id: FileId) {}
|
|||
fn baz(file<|>) {}
|
||||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
CompletionItem {
|
||||
label: "file_id: FileId",
|
||||
source_range: 61..65,
|
||||
delete: 61..65,
|
||||
insert: "file_id: FileId",
|
||||
lookup: "file_id",
|
||||
},
|
||||
]
|
||||
bn file_id: FileId
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
@ -90,19 +87,10 @@ fn baz(file<|>) {}
|
|||
check(
|
||||
r#"
|
||||
fn foo(file_id: FileId) {}
|
||||
fn bar(file_id: FileId) {}
|
||||
fn baz(file<|>, x: i32) {}
|
||||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
CompletionItem {
|
||||
label: "file_id: FileId",
|
||||
source_range: 61..65,
|
||||
delete: 61..65,
|
||||
insert: "file_id: FileId",
|
||||
lookup: "file_id",
|
||||
},
|
||||
]
|
||||
bn file_id: FileId
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
@ -119,15 +107,7 @@ pub(crate) trait SourceRoot {
|
|||
}
|
||||
"#,
|
||||
expect![[r#"
|
||||
[
|
||||
CompletionItem {
|
||||
label: "file_id: FileId",
|
||||
source_range: 208..212,
|
||||
delete: 208..212,
|
||||
insert: "file_id: FileId",
|
||||
lookup: "file_id",
|
||||
},
|
||||
]
|
||||
bn file_id: FileId
|
||||
"#]],
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue