147 lines
3.8 KiB
Rust
Raw Normal View History

2020-07-10 16:07:12 +02:00
//! See `complete_fn_param`.
2020-08-12 18:26:51 +02:00
use rustc_hash::FxHashMap;
use syntax::{
2020-04-09 23:02:10 +02:00
ast::{self, ModuleItemOwner},
match_ast, AstNode,
};
2019-01-08 22:33:36 +03:00
use crate::{CompletionContext, CompletionItem, CompletionItemKind, 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.
2020-10-25 10:59:15 +03:00
pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) {
2019-01-08 22:33:36 +03:00
if !ctx.is_param {
return;
}
let mut params = FxHashMap::default();
2020-07-30 11:42:51 +02:00
2020-07-30 14:51:08 +02:00
let me = ctx.token.ancestors().find_map(ast::Fn::cast);
let mut process_fn = |func: ast::Fn| {
2020-07-30 11:42:51 +02:00
if Some(&func) == me.as_ref() {
return;
}
func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
if let Some(pat) = param.pat() {
let text = param.syntax().text().to_string();
let lookup = pat.syntax().text().to_string();
params.entry(text).or_insert(lookup);
}
});
2020-07-30 11:42:51 +02:00
};
for node in ctx.token.ancestors() {
2020-07-30 11:42:51 +02:00
match_ast! {
2019-10-05 17:03:03 +03:00
match node {
2020-07-30 11:42:51 +02:00
ast::SourceFile(it) => it.items().filter_map(|item| match item {
2020-07-30 14:51:08 +02:00
ast::Item::Fn(it) => Some(it),
2020-07-30 11:42:51 +02:00
_ => None,
}).for_each(&mut process_fn),
ast::ItemList(it) => it.items().filter_map(|item| match item {
2020-07-30 14:51:08 +02:00
ast::Item::Fn(it) => Some(it),
2020-07-30 11:42:51 +02:00
_ => None,
}).for_each(&mut process_fn),
ast::AssocItemList(it) => it.assoc_items().filter_map(|item| match item {
2020-07-30 14:51:08 +02:00
ast::AssocItem::Fn(it) => Some(it),
2020-07-30 11:42:51 +02:00
_ => None,
}).for_each(&mut process_fn),
2020-04-09 23:02:10 +02:00
_ => continue,
}
};
2019-01-08 22:33:36 +03:00
}
2020-07-10 16:29:14 +02:00
params.into_iter().for_each(|(label, lookup)| {
2021-03-12 12:12:32 +03:00
let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
item.kind(CompletionItemKind::Binding).lookup_by(lookup);
item.add_to(acc)
});
2019-01-08 22:33:36 +03:00
}
#[cfg(test)]
mod tests {
2020-08-21 13:19:31 +02:00
use expect_test::{expect, Expect};
2019-01-08 22:33:36 +03:00
use crate::{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) {}
2021-01-06 20:15:48 +00:00
fn baz(file$0) {}
2020-07-04 14:53:43 +02:00
"#,
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) {}
2021-01-06 20:15:48 +00:00
fn baz(file$0, x: i32) {}
2020-07-04 14:53:43 +02:00
"#,
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;
2021-01-06 20:15:48 +00:00
pub fn syntax(&self, file$0)
2020-07-04 14:53:43 +02:00
}
"#,
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) {
2021-01-06 20:15:48 +00:00
fn inner($0)
2020-07-10 16:29:14 +02:00
}
"#,
expect![[r#"
bn text: String
"#]],
)
}
#[test]
fn completes_non_ident_pat_param() {
check(
r#"
struct Bar { bar: u32 }
fn foo(Bar { bar }: Bar) {}
fn foo2($0) {}
"#,
expect![[r#"
bn Bar { bar }: Bar
"#]],
)
}
2019-01-08 22:33:36 +03:00
}