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 let text = param.syntax().text().to_string();
29 params.entry(text).or_insert(param);
33 for node in ctx.token.parent().ancestors() {
36 ast::SourceFile(it) => it.items().filter_map(|item| match item {
37 ast::Item::Fn(it) => Some(it),
39 }).for_each(&mut process_fn),
40 ast::ItemList(it) => it.items().filter_map(|item| match item {
41 ast::Item::Fn(it) => Some(it),
43 }).for_each(&mut process_fn),
44 ast::AssocItemList(it) => it.assoc_items().filter_map(|item| match item {
45 ast::AssocItem::Fn(it) => Some(it),
47 }).for_each(&mut process_fn),
55 .filter_map(|(label, param)| {
56 let lookup = param.pat()?.syntax().text().to_string();
59 .for_each(|(label, lookup)| {
60 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
61 .kind(CompletionItemKind::Binding)
69 use expect_test::{expect, Expect};
71 use crate::{test_utils::completion_list, CompletionKind};
73 fn check(ra_fixture: &str, expect: Expect) {
74 let actual = completion_list(ra_fixture, CompletionKind::Magic);
75 expect.assert_eq(&actual);
79 fn test_param_completion_last_param() {
82 fn foo(file_id: FileId) {}
83 fn bar(file_id: FileId) {}
93 fn test_param_completion_nth_param() {
96 fn foo(file_id: FileId) {}
97 fn baz(file$0, x: i32) {}
106 fn test_param_completion_trait_param() {
109 pub(crate) trait SourceRoot {
110 pub fn contains(&self, file_id: FileId) -> bool;
111 pub fn module_map(&self) -> &ModuleMap;
112 pub fn lines(&self, file_id: FileId) -> &LineIndex;
113 pub fn syntax(&self, file$0)
123 fn completes_param_in_inner_function() {
126 fn outer(text: String) {