mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-27 21:43:37 +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::{
|
use ra_syntax::{
|
||||||
ast::{self, ModuleItemOwner},
|
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 params = FxHashMap::default();
|
||||||
|
let mut me = None;
|
||||||
for node in ctx.token.parent().ancestors() {
|
for node in ctx.token.parent().ancestors() {
|
||||||
let items = match_ast! {
|
let items = match_ast! {
|
||||||
match node {
|
match node {
|
||||||
ast::SourceFile(it) => it.items(),
|
ast::SourceFile(it) => it.items(),
|
||||||
ast::ItemList(it) => it.items(),
|
ast::ItemList(it) => it.items(),
|
||||||
|
ast::FnDef(it) => {
|
||||||
|
me = Some(it);
|
||||||
|
continue;
|
||||||
|
},
|
||||||
_ => continue,
|
_ => continue,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
for item in items {
|
for item in items {
|
||||||
if let ast::ModuleItem::FnDef(func) = item {
|
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| {
|
func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
|
||||||
let text = param.syntax().text().to_string();
|
let text = param.syntax().text().to_string();
|
||||||
params.entry(text).or_insert((0, param)).0 += 1;
|
params.entry(text).or_insert(param);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
params
|
params
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|(label, (count, param))| {
|
.filter_map(|(label, param)| {
|
||||||
let lookup = param.pat()?.syntax().text().to_string();
|
let lookup = param.pat()?.syntax().text().to_string();
|
||||||
if count < 2 {
|
|
||||||
None
|
|
||||||
} else {
|
|
||||||
Some((label, lookup))
|
Some((label, lookup))
|
||||||
}
|
|
||||||
})
|
})
|
||||||
.for_each(|(label, lookup)| {
|
.for_each(|(label, lookup)| {
|
||||||
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
|
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
|
||||||
|
.kind(crate::CompletionItemKind::Binding)
|
||||||
.lookup_by(lookup)
|
.lookup_by(lookup)
|
||||||
.add_to(acc)
|
.add_to(acc)
|
||||||
});
|
});
|
||||||
|
@ -56,11 +61,11 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
|
||||||
mod tests {
|
mod tests {
|
||||||
use expect::{expect, Expect};
|
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) {
|
fn check(ra_fixture: &str, expect: Expect) {
|
||||||
let actual = do_completion(ra_fixture, CompletionKind::Magic);
|
let actual = completion_list(ra_fixture, CompletionKind::Magic);
|
||||||
expect.assert_debug_eq(&actual);
|
expect.assert_eq(&actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -72,15 +77,7 @@ fn bar(file_id: FileId) {}
|
||||||
fn baz(file<|>) {}
|
fn baz(file<|>) {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
[
|
bn file_id: FileId
|
||||||
CompletionItem {
|
|
||||||
label: "file_id: FileId",
|
|
||||||
source_range: 61..65,
|
|
||||||
delete: 61..65,
|
|
||||||
insert: "file_id: FileId",
|
|
||||||
lookup: "file_id",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -90,19 +87,10 @@ fn baz(file<|>) {}
|
||||||
check(
|
check(
|
||||||
r#"
|
r#"
|
||||||
fn foo(file_id: FileId) {}
|
fn foo(file_id: FileId) {}
|
||||||
fn bar(file_id: FileId) {}
|
|
||||||
fn baz(file<|>, x: i32) {}
|
fn baz(file<|>, x: i32) {}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
[
|
bn file_id: FileId
|
||||||
CompletionItem {
|
|
||||||
label: "file_id: FileId",
|
|
||||||
source_range: 61..65,
|
|
||||||
delete: 61..65,
|
|
||||||
insert: "file_id: FileId",
|
|
||||||
lookup: "file_id",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -119,15 +107,7 @@ pub(crate) trait SourceRoot {
|
||||||
}
|
}
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
[
|
bn file_id: FileId
|
||||||
CompletionItem {
|
|
||||||
label: "file_id: FileId",
|
|
||||||
source_range: 208..212,
|
|
||||||
delete: 208..212,
|
|
||||||
insert: "file_id: FileId",
|
|
||||||
lookup: "file_id",
|
|
||||||
},
|
|
||||||
]
|
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue