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

126 lines
3.2 KiB
Rust
Raw Normal View History

2020-07-10 16:07:12 +02:00
//! See `complete_fn_param`.
2020-04-09 23:02:10 +02:00
use ra_syntax::{
ast::{self, ModuleItemOwner},
match_ast, AstNode,
};
2019-01-08 22:33:36 +03:00
use rustc_hash::FxHashMap;
use crate::completion::{CompletionContext, CompletionItem, CompletionKind, Completions};
2019-01-08 22:33:36 +03:00
2019-02-11 17:18:27 +01:00
/// Complete repeated parameters, both name and type. For example, if all
2019-01-08 22:33:36 +03: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();
2020-07-10 16:29:14 +02:00
let me = ctx.token.ancestors().find_map(ast::FnDef::cast);
2019-03-30 13:25:53 +03:00
for node in ctx.token.parent().ancestors() {
2020-04-09 23:02:10 +02:00
let items = match_ast! {
2019-10-05 17:03:03 +03:00
match node {
2020-04-09 23:02:10 +02:00
ast::SourceFile(it) => it.items(),
ast::ItemList(it) => it.items(),
_ => continue,
}
};
for item in items {
2020-07-30 00:23:03 +02:00
if let ast::Item::FnDef(func) = item {
2020-07-10 16:07:12 +02:00
if Some(&func) == me.as_ref() {
continue;
}
2020-04-09 23:02:10 +02:00
func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
let text = param.syntax().text().to_string();
2020-07-10 16:07:12 +02:00
params.entry(text).or_insert(param);
2020-04-09 23:02:10 +02:00
})
2019-10-05 17:03:03 +03:00
}
}
2019-01-08 22:33:36 +03:00
}
2020-07-10 16:29:14 +02:00
2019-01-08 22:33:36 +03:00
params
.into_iter()
2020-07-10 16:07:12 +02:00
.filter_map(|(label, param)| {
2019-01-08 22:33:36 +03:00
let lookup = param.pat()?.syntax().text().to_string();
2020-07-10 16:07:12 +02:00
Some((label, lookup))
2019-01-08 22:33:36 +03:00
})
.for_each(|(label, lookup)| {
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
2020-07-10 16:05:01 +02:00
.kind(crate::CompletionItemKind::Binding)
2019-01-08 22:33:36 +03:00
.lookup_by(lookup)
.add_to(acc)
});
}
#[cfg(test)]
mod tests {
2020-07-04 14:53:43 +02:00
use expect::{expect, Expect};
2019-01-08 22:33:36 +03:00
2020-07-10 16:05:01 +02:00
use crate::completion::{test_utils::completion_list, CompletionKind};
2020-07-04 14:53:43 +02:00
fn check(ra_fixture: &str, expect: Expect) {
2020-07-10 16:05:01 +02:00
let actual = completion_list(ra_fixture, CompletionKind::Magic);
expect.assert_eq(&actual);
2019-01-08 22:33:36 +03:00
}
#[test]
fn test_param_completion_last_param() {
2020-07-04 14:53:43 +02:00
check(
r#"
fn foo(file_id: FileId) {}
fn bar(file_id: FileId) {}
fn baz(file<|>) {}
"#,
expect![[r#"
2020-07-10 16:05:01 +02:00
bn file_id: FileId
2020-07-04 14:53:43 +02:00
"#]],
2019-01-08 22:33:36 +03:00
);
}
#[test]
fn test_param_completion_nth_param() {
2020-07-04 14:53:43 +02:00
check(
r#"
fn foo(file_id: FileId) {}
fn baz(file<|>, x: i32) {}
"#,
expect![[r#"
2020-07-10 16:05:01 +02:00
bn file_id: FileId
2020-07-04 14:53:43 +02:00
"#]],
2019-01-08 22:33:36 +03:00
);
}
#[test]
fn test_param_completion_trait_param() {
2020-07-04 14:53:43 +02:00
check(
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<|>)
}
"#,
expect![[r#"
2020-07-10 16:05:01 +02:00
bn file_id: FileId
2020-07-04 14:53:43 +02:00
"#]],
2019-01-08 22:33:36 +03:00
);
}
2020-07-10 16:29:14 +02:00
#[test]
fn completes_param_in_inner_function() {
check(
r#"
fn outer(text: String) {
fn inner(<|>)
}
"#,
expect![[r#"
bn text: String
"#]],
)
}
2019-01-08 22:33:36 +03:00
}