aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-07-10 15:08:36 +0100
committerGitHub <[email protected]>2020-07-10 15:08:36 +0100
commit31f2b9fbaa52d01b9251b5da74381576fe24d3b4 (patch)
tree27fd90c9c6a788cf058c4a6f65b4a43b0e8db1c3
parent9ab59e2162bf7dcb120378e0c2e9fd6dac39c107 (diff)
parentd02aabe633b6da6748e9911219b46c6ff206595d (diff)
Merge #5294
5294: Complete parameters more aggressively r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r--crates/ra_ide/src/completion/complete_fn_param.rs58
1 files changed, 19 insertions, 39 deletions
diff --git a/crates/ra_ide/src/completion/complete_fn_param.rs b/crates/ra_ide/src/completion/complete_fn_param.rs
index 9fb5c050e..ee91bbdea 100644
--- a/crates/ra_ide/src/completion/complete_fn_param.rs
+++ b/crates/ra_ide/src/completion/complete_fn_param.rs
@@ -1,4 +1,4 @@
1//! FIXME: write short doc here 1//! See `complete_fn_param`.
2 2
3use ra_syntax::{ 3use ra_syntax::{
4 ast::{self, ModuleItemOwner}, 4 ast::{self, ModuleItemOwner},
@@ -18,35 +18,40 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
18 } 18 }
19 19
20 let mut params = FxHashMap::default(); 20 let mut params = FxHashMap::default();
21 let mut me = None;
21 for node in ctx.token.parent().ancestors() { 22 for node in ctx.token.parent().ancestors() {
22 let items = match_ast! { 23 let items = match_ast! {
23 match node { 24 match node {
24 ast::SourceFile(it) => it.items(), 25 ast::SourceFile(it) => it.items(),
25 ast::ItemList(it) => it.items(), 26 ast::ItemList(it) => it.items(),
27 ast::FnDef(it) => {
28 me = Some(it);
29 continue;
30 },
26 _ => continue, 31 _ => continue,
27 } 32 }
28 }; 33 };
29 for item in items { 34 for item in items {
30 if let ast::ModuleItem::FnDef(func) = item { 35 if let ast::ModuleItem::FnDef(func) = item {
36 if Some(&func) == me.as_ref() {
37 continue;
38 }
31 func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| { 39 func.param_list().into_iter().flat_map(|it| it.params()).for_each(|param| {
32 let text = param.syntax().text().to_string(); 40 let text = param.syntax().text().to_string();
33 params.entry(text).or_insert((0, param)).0 += 1; 41 params.entry(text).or_insert(param);
34 }) 42 })
35 } 43 }
36 } 44 }
37 } 45 }
38 params 46 params
39 .into_iter() 47 .into_iter()
40 .filter_map(|(label, (count, param))| { 48 .filter_map(|(label, param)| {
41 let lookup = param.pat()?.syntax().text().to_string(); 49 let lookup = param.pat()?.syntax().text().to_string();
42 if count < 2 { 50 Some((label, lookup))
43 None
44 } else {
45 Some((label, lookup))
46 }
47 }) 51 })
48 .for_each(|(label, lookup)| { 52 .for_each(|(label, lookup)| {
49 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label) 53 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
54 .kind(crate::CompletionItemKind::Binding)
50 .lookup_by(lookup) 55 .lookup_by(lookup)
51 .add_to(acc) 56 .add_to(acc)
52 }); 57 });
@@ -56,11 +61,11 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
56mod tests { 61mod tests {
57 use expect::{expect, Expect}; 62 use expect::{expect, Expect};
58 63
59 use crate::completion::{test_utils::do_completion, CompletionKind}; 64 use crate::completion::{test_utils::completion_list, CompletionKind};
60 65
61 fn check(ra_fixture: &str, expect: Expect) { 66 fn check(ra_fixture: &str, expect: Expect) {
62 let actual = do_completion(ra_fixture, CompletionKind::Magic); 67 let actual = completion_list(ra_fixture, CompletionKind::Magic);
63 expect.assert_debug_eq(&actual); 68 expect.assert_eq(&actual);
64 } 69 }
65 70
66 #[test] 71 #[test]
@@ -72,15 +77,7 @@ fn bar(file_id: FileId) {}
72fn baz(file<|>) {} 77fn baz(file<|>) {}
73"#, 78"#,
74 expect![[r#" 79 expect![[r#"
75 [ 80 bn file_id: FileId
76 CompletionItem {
77 label: "file_id: FileId",
78 source_range: 61..65,
79 delete: 61..65,
80 insert: "file_id: FileId",
81 lookup: "file_id",
82 },
83 ]
84 "#]], 81 "#]],
85 ); 82 );
86 } 83 }
@@ -90,19 +87,10 @@ fn baz(file<|>) {}
90 check( 87 check(
91 r#" 88 r#"
92fn foo(file_id: FileId) {} 89fn foo(file_id: FileId) {}
93fn bar(file_id: FileId) {}
94fn baz(file<|>, x: i32) {} 90fn baz(file<|>, x: i32) {}
95"#, 91"#,
96 expect![[r#" 92 expect![[r#"
97 [ 93 bn file_id: FileId
98 CompletionItem {
99 label: "file_id: FileId",
100 source_range: 61..65,
101 delete: 61..65,
102 insert: "file_id: FileId",
103 lookup: "file_id",
104 },
105 ]
106 "#]], 94 "#]],
107 ); 95 );
108 } 96 }
@@ -119,15 +107,7 @@ pub(crate) trait SourceRoot {
119} 107}
120"#, 108"#,
121 expect![[r#" 109 expect![[r#"
122 [ 110 bn file_id: FileId
123 CompletionItem {
124 label: "file_id: FileId",
125 source_range: 208..212,
126 delete: 208..212,
127 insert: "file_id: FileId",
128 lookup: "file_id",
129 },
130 ]
131 "#]], 111 "#]],
132 ); 112 );
133 } 113 }