rust-analyzer/crates/ra_ide/src/completion/complete_fn_param.rs

139 lines
3.9 KiB
Rust
Raw Normal View History

//! FIXME: write short doc here
2020-04-09 21:02:10 +00:00
use ra_syntax::{
ast::{self, ModuleItemOwner},
match_ast, AstNode,
};
2019-01-08 19:33:36 +00:00
use rustc_hash::FxHashMap;
use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};
2019-01-08 19:33:36 +00:00
2019-02-11 16:18:27 +00:00
/// Complete repeated parameters, both name and type. For example, if all
2019-01-08 19:33:36 +00:00
/// 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.
pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_param {
return;
}
let mut params = FxHashMap::default();
2019-03-30 10:25:53 +00:00
for node in ctx.token.parent().ancestors() {
2020-04-09 21:02:10 +00:00
let items = match_ast! {
2019-10-05 14:03:03 +00:00
match node {
2020-04-09 21:02:10 +00:00
ast::SourceFile(it) => it.items(),
ast::ItemList(it) => it.items(),
_ => continue,
}
};
for item in items {
if let ast::ModuleItem::FnDef(func) = item {
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;
})
2019-10-05 14:03:03 +00:00
}
}
2019-01-08 19:33:36 +00:00
}
params
.into_iter()
.filter_map(|(label, (count, 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)
2019-01-08 19:33:36 +00:00
.lookup_by(lookup)
.add_to(acc)
});
}
#[cfg(test)]
mod tests {
2020-03-11 09:46:43 +00:00
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
2019-08-29 13:49:10 +00:00
use insta::assert_debug_snapshot;
2019-01-08 19:33:36 +00:00
fn do_magic_completion(code: &str) -> Vec<CompletionItem> {
do_completion(code, CompletionKind::Magic)
2019-01-08 19:33:36 +00:00
}
#[test]
fn test_param_completion_last_param() {
2019-08-29 13:49:10 +00:00
assert_debug_snapshot!(
do_magic_completion(
r"
fn foo(file_id: FileId) {}
fn bar(file_id: FileId) {}
fn baz(file<|>) {}
",
),
2019-05-23 22:46:23 +00:00
@r###"
2019-11-15 09:56:24 +00:00
[
CompletionItem {
label: "file_id: FileId",
2020-06-24 09:29:43 +00:00
source_range: 61..65,
delete: 61..65,
2019-11-15 09:56:24 +00:00
insert: "file_id: FileId",
lookup: "file_id",
},
]
2019-05-23 22:46:23 +00:00
"###
2019-01-08 19:33:36 +00:00
);
}
#[test]
fn test_param_completion_nth_param() {
2019-08-29 13:49:10 +00:00
assert_debug_snapshot!(
do_magic_completion(
r"
fn foo(file_id: FileId) {}
fn bar(file_id: FileId) {}
fn baz(file<|>, x: i32) {}
",
),
2019-05-23 22:46:23 +00:00
@r###"
2019-11-15 09:56:24 +00:00
[
CompletionItem {
label: "file_id: FileId",
2020-06-24 09:29:43 +00:00
source_range: 61..65,
delete: 61..65,
2019-11-15 09:56:24 +00:00
insert: "file_id: FileId",
lookup: "file_id",
},
]
2019-05-23 22:46:23 +00:00
"###
2019-01-08 19:33:36 +00:00
);
}
#[test]
fn test_param_completion_trait_param() {
2019-08-29 13:49:10 +00:00
assert_debug_snapshot!(
do_magic_completion(
r"
pub(crate) trait SourceRoot {
pub fn contains(&self, file_id: FileId) -> bool;
pub fn module_map(&self) -> &ModuleMap;
pub fn lines(&self, file_id: FileId) -> &LineIndex;
pub fn syntax(&self, file<|>)
}
",
),
2019-05-23 22:46:23 +00:00
@r###"
2019-11-15 09:56:24 +00:00
[
CompletionItem {
label: "file_id: FileId",
2020-06-24 09:29:43 +00:00
source_range: 208..212,
delete: 208..212,
2019-11-15 09:56:24 +00:00
insert: "file_id: FileId",
lookup: "file_id",
},
]
2019-05-23 22:46:23 +00:00
"###
2019-01-08 19:33:36 +00:00
);
}
}