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) -> Option<()> {
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.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 let self_completion_items = ["self", "&self", "mut self", "&mut self"];
57 if ctx.impl_def.is_some() && me?.param_list()?.params().next().is_none() {
58 self_completion_items.iter().for_each(|self_item| {
59 add_new_item_to_acc(ctx, acc, self_item.to_string(), self_item.to_string())
63 params.into_iter().for_each(|(label, lookup)| add_new_item_to_acc(ctx, acc, label, lookup));
68 fn add_new_item_to_acc(
69 ctx: &CompletionContext,
70 acc: &mut Completions,
74 let mut item = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
75 item.kind(CompletionItemKind::Binding).lookup_by(lookup);
81 use expect_test::{expect, Expect};
83 use crate::{tests::filtered_completion_list, CompletionKind};
85 fn check(ra_fixture: &str, expect: Expect) {
86 let actual = filtered_completion_list(ra_fixture, CompletionKind::Magic);
87 expect.assert_eq(&actual);
91 fn test_param_completion_last_param() {
94 fn foo(file_id: FileId) {}
95 fn bar(file_id: FileId) {}
105 fn test_param_completion_first_param() {
108 fn foo(file_id: FileId) {}
109 fn bar(file_id: FileId) {}
110 fn baz(file$0 id: u32) {}
119 fn test_param_completion_nth_param() {
122 fn foo(file_id: FileId) {}
123 fn baz(file$0, x: i32) {}
132 fn test_param_completion_trait_param() {
135 pub(crate) trait SourceRoot {
136 pub fn contains(&self, file_id: FileId) -> bool;
137 pub fn module_map(&self) -> &ModuleMap;
138 pub fn lines(&self, file_id: FileId) -> &LineIndex;
139 pub fn syntax(&self, file$0)
149 fn completes_param_in_inner_function() {
152 fn outer(text: String) {
163 fn completes_non_ident_pat_param() {
166 struct Bar { bar: u32 }
168 fn foo(Bar { bar }: Bar) {}
178 fn test_param_completion_self_param() {
184 fn foo(file_id: FileId) {}