aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion/complete_fn_param.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/completion/complete_fn_param.rs')
-rw-r--r--crates/ra_analysis/src/completion/complete_fn_param.rs107
1 files changed, 107 insertions, 0 deletions
diff --git a/crates/ra_analysis/src/completion/complete_fn_param.rs b/crates/ra_analysis/src/completion/complete_fn_param.rs
new file mode 100644
index 000000000..d05a5e3cf
--- /dev/null
+++ b/crates/ra_analysis/src/completion/complete_fn_param.rs
@@ -0,0 +1,107 @@
1use ra_syntax::{
2 algo::{
3 visit::{visitor_ctx, VisitorCtx}
4 },
5 ast,
6 AstNode,
7};
8use rustc_hash::{FxHashMap};
9
10use crate::{
11 completion::{SyntaxContext, Completions, CompletionKind, CompletionItem},
12};
13
14/// Complete repeated parametes, both name and type. For example, if all
15/// functions in a file have a `spam: &mut Spam` parameter, a completion with
16/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
17/// suggested.
18pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &SyntaxContext) {
19 if !ctx.is_param {
20 return;
21 }
22
23 let mut params = FxHashMap::default();
24 for node in ctx.leaf.ancestors() {
25 let _ = visitor_ctx(&mut params)
26 .visit::<ast::SourceFile, _>(process)
27 .visit::<ast::ItemList, _>(process)
28 .accept(node);
29 }
30 params
31 .into_iter()
32 .filter_map(|(label, (count, param))| {
33 let lookup = param.pat()?.syntax().text().to_string();
34 if count < 2 {
35 None
36 } else {
37 Some((label, lookup))
38 }
39 })
40 .for_each(|(label, lookup)| {
41 CompletionItem::new(label)
42 .lookup_by(lookup)
43 .kind(CompletionKind::Magic)
44 .add_to(acc)
45 });
46
47 fn process<'a, N: ast::FnDefOwner<'a>>(
48 node: N,
49 params: &mut FxHashMap<String, (u32, ast::Param<'a>)>,
50 ) {
51 node.functions()
52 .filter_map(|it| it.param_list())
53 .flat_map(|it| it.params())
54 .for_each(|param| {
55 let text = param.syntax().text().to_string();
56 params.entry(text).or_insert((0, param)).0 += 1;
57 })
58 }
59}
60
61#[cfg(test)]
62mod tests {
63 use crate::completion::*;
64
65 fn check_magic_completion(code: &str, expected_completions: &str) {
66 check_completion(code, expected_completions, CompletionKind::Magic);
67 }
68
69 #[test]
70 fn test_param_completion_last_param() {
71 check_magic_completion(
72 r"
73 fn foo(file_id: FileId) {}
74 fn bar(file_id: FileId) {}
75 fn baz(file<|>) {}
76 ",
77 r#"file_id "file_id: FileId""#,
78 );
79 }
80
81 #[test]
82 fn test_param_completion_nth_param() {
83 check_magic_completion(
84 r"
85 fn foo(file_id: FileId) {}
86 fn bar(file_id: FileId) {}
87 fn baz(file<|>, x: i32) {}
88 ",
89 r#"file_id "file_id: FileId""#,
90 );
91 }
92
93 #[test]
94 fn test_param_completion_trait_param() {
95 check_magic_completion(
96 r"
97 pub(crate) trait SourceRoot {
98 pub fn contains(&self, file_id: FileId) -> bool;
99 pub fn module_map(&self) -> &ModuleMap;
100 pub fn lines(&self, file_id: FileId) -> &LineIndex;
101 pub fn syntax(&self, file<|>)
102 }
103 ",
104 r#"file_id "file_id: FileId""#,
105 );
106 }
107}