1 //! See `complete_fn_param`.
3 use rustc_hash::FxHashMap;
5 ast::{self, ModuleItemOwner},
9 use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
11 /// Complete repeated parameters, both name and type. For example, if all
12 /// functions in a file have a `spam: &mut Spam` parameter, a completion with
13 /// `spam: &mut Spam` insert text/label and `spam` lookup string will be
15 pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) {
20 let mut params = FxHashMap::default();
22 let me = ctx.token.ancestors().find_map(ast::Fn::cast);
23 let mut process_fn = |func: ast::Fn| {
24 if Some(&func) == me.as_ref() {
27 func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
28 if let Some(pat) = param.pat() {
29 let text = param.syntax().text().to_string();
30 let lookup = pat.syntax().text().to_string();
31 params.entry(text).or_insert(lookup);
36 for node in ctx.token.parent().ancestors() {
39 ast::SourceFile(it) => it.items().filter_map(|item| match item {
40 ast::Item::Fn(it) => Some(it),
42 }).for_each(&mut process_fn),
43 ast::ItemList(it) => it.items().filter_map(|item| match item {
44 ast::Item::Fn(it) => Some(it),
46 }).for_each(&mut process_fn),
47 ast::AssocItemList(it) => it.assoc_items().filter_map(|item| match item {
48 ast::AssocItem::Fn(it) => Some(it),
50 }).for_each(&mut process_fn),
56 params.into_iter().for_each(|(label, lookup)| {
57 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
58 .kind(CompletionItemKind::Binding)
66 use expect_test::{expect, Expect};
68 use crate::{test_utils::completion_list, CompletionKind};
70 fn check(ra_fixture: &str, expect: Expect) {
71 let actual = completion_list(ra_fixture, CompletionKind::Magic);
72 expect.assert_eq(&actual);
76 fn test_param_completion_last_param() {
79 fn foo(file_id: FileId) {}
80 fn bar(file_id: FileId) {}
90 fn test_param_completion_nth_param() {
93 fn foo(file_id: FileId) {}
94 fn baz(file$0, x: i32) {}
103 fn test_param_completion_trait_param() {
106 pub(crate) trait SourceRoot {
107 pub fn contains(&self, file_id: FileId) -> bool;
108 pub fn module_map(&self) -> &ModuleMap;
109 pub fn lines(&self, file_id: FileId) -> &LineIndex;
110 pub fn syntax(&self, file$0)
120 fn completes_param_in_inner_function() {
123 fn outer(text: String) {