diff options
Diffstat (limited to 'crates/ra_ide/src/completion')
-rw-r--r-- | crates/ra_ide/src/completion/complete_dot.rs | 124 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/completion_context.rs | 8 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/presentation.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/test_utils.rs | 11 |
4 files changed, 138 insertions, 7 deletions
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index f433faef3..b5448af5c 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | //! FIXME: write short doc here | 1 | //! FIXME: write short doc here |
2 | 2 | ||
3 | use hir::{HasVisibility, Type}; | 3 | use hir::{HasVisibility, HirDisplay, Type}; |
4 | 4 | ||
5 | use crate::completion::completion_item::CompletionKind; | 5 | use crate::completion::completion_item::CompletionKind; |
6 | use crate::{ | 6 | use crate::{ |
@@ -8,6 +8,7 @@ use crate::{ | |||
8 | CompletionItem, | 8 | CompletionItem, |
9 | }; | 9 | }; |
10 | use rustc_hash::FxHashSet; | 10 | use rustc_hash::FxHashSet; |
11 | use std::cmp::Ordering; | ||
11 | 12 | ||
12 | /// Complete dot accesses, i.e. fields or methods (and .await syntax). | 13 | /// Complete dot accesses, i.e. fields or methods (and .await syntax). |
13 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { | 14 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { |
@@ -37,7 +38,31 @@ pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { | |||
37 | 38 | ||
38 | fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { | 39 | fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Type) { |
39 | for receiver in receiver.autoderef(ctx.db) { | 40 | for receiver in receiver.autoderef(ctx.db) { |
40 | for (field, ty) in receiver.fields(ctx.db) { | 41 | let mut fields = receiver.fields(ctx.db); |
42 | if let Some(call_info) = &ctx.call_info { | ||
43 | if let Some(active_parameter_type) = call_info.active_parameter_type() { | ||
44 | let active_parameter_name = call_info.active_parameter_name().unwrap(); | ||
45 | fields.sort_by(|a, b| { | ||
46 | // For the same type | ||
47 | if active_parameter_type == a.1.display(ctx.db).to_string() { | ||
48 | // If same type + same name then go top position | ||
49 | if active_parameter_name == a.0.name(ctx.db).to_string() { | ||
50 | Ordering::Less | ||
51 | } else { | ||
52 | if active_parameter_type == b.1.display(ctx.db).to_string() { | ||
53 | Ordering::Equal | ||
54 | } else { | ||
55 | Ordering::Less | ||
56 | } | ||
57 | } | ||
58 | } else { | ||
59 | Ordering::Greater | ||
60 | } | ||
61 | }); | ||
62 | } | ||
63 | } | ||
64 | |||
65 | for (field, ty) in fields { | ||
41 | if ctx.scope().module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) { | 66 | if ctx.scope().module().map_or(false, |m| !field.is_visible_from(ctx.db, m)) { |
42 | // Skip private field. FIXME: If the definition location of the | 67 | // Skip private field. FIXME: If the definition location of the |
43 | // field is editable, we should show the completion | 68 | // field is editable, we should show the completion |
@@ -70,13 +95,20 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &T | |||
70 | 95 | ||
71 | #[cfg(test)] | 96 | #[cfg(test)] |
72 | mod tests { | 97 | mod tests { |
73 | use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; | 98 | use crate::completion::{ |
99 | test_utils::{do_completion, do_completion_without_sort}, | ||
100 | CompletionItem, CompletionKind, | ||
101 | }; | ||
74 | use insta::assert_debug_snapshot; | 102 | use insta::assert_debug_snapshot; |
75 | 103 | ||
76 | fn do_ref_completion(code: &str) -> Vec<CompletionItem> { | 104 | fn do_ref_completion(code: &str) -> Vec<CompletionItem> { |
77 | do_completion(code, CompletionKind::Reference) | 105 | do_completion(code, CompletionKind::Reference) |
78 | } | 106 | } |
79 | 107 | ||
108 | fn do_ref_completion_without_sort(code: &str) -> Vec<CompletionItem> { | ||
109 | do_completion_without_sort(code, CompletionKind::Reference) | ||
110 | } | ||
111 | |||
80 | #[test] | 112 | #[test] |
81 | fn test_struct_field_completion() { | 113 | fn test_struct_field_completion() { |
82 | assert_debug_snapshot!( | 114 | assert_debug_snapshot!( |
@@ -104,6 +136,92 @@ mod tests { | |||
104 | } | 136 | } |
105 | 137 | ||
106 | #[test] | 138 | #[test] |
139 | fn test_struct_field_completion_in_func_call() { | ||
140 | assert_debug_snapshot!( | ||
141 | do_ref_completion_without_sort( | ||
142 | r" | ||
143 | struct A { another_field: i64, the_field: u32, my_string: String } | ||
144 | fn test(my_param: u32) -> u32 { my_param } | ||
145 | fn foo(a: A) { | ||
146 | test(a.<|>) | ||
147 | } | ||
148 | ", | ||
149 | ), | ||
150 | @r###" | ||
151 | [ | ||
152 | CompletionItem { | ||
153 | label: "the_field", | ||
154 | source_range: [201; 201), | ||
155 | delete: [201; 201), | ||
156 | insert: "the_field", | ||
157 | kind: Field, | ||
158 | detail: "u32", | ||
159 | }, | ||
160 | CompletionItem { | ||
161 | label: "another_field", | ||
162 | source_range: [201; 201), | ||
163 | delete: [201; 201), | ||
164 | insert: "another_field", | ||
165 | kind: Field, | ||
166 | detail: "i64", | ||
167 | }, | ||
168 | CompletionItem { | ||
169 | label: "my_string", | ||
170 | source_range: [201; 201), | ||
171 | delete: [201; 201), | ||
172 | insert: "my_string", | ||
173 | kind: Field, | ||
174 | detail: "{unknown}", | ||
175 | }, | ||
176 | ] | ||
177 | "### | ||
178 | ); | ||
179 | } | ||
180 | |||
181 | #[test] | ||
182 | fn test_struct_field_completion_in_func_call_with_type_and_name() { | ||
183 | assert_debug_snapshot!( | ||
184 | do_ref_completion_without_sort( | ||
185 | r" | ||
186 | struct A { another_field: i64, another_good_type: u32, the_field: u32 } | ||
187 | fn test(the_field: u32) -> u32 { the_field } | ||
188 | fn foo(a: A) { | ||
189 | test(a.<|>) | ||
190 | } | ||
191 | ", | ||
192 | ), | ||
193 | @r###" | ||
194 | [ | ||
195 | CompletionItem { | ||
196 | label: "the_field", | ||
197 | source_range: [208; 208), | ||
198 | delete: [208; 208), | ||
199 | insert: "the_field", | ||
200 | kind: Field, | ||
201 | detail: "u32", | ||
202 | }, | ||
203 | CompletionItem { | ||
204 | label: "another_good_type", | ||
205 | source_range: [208; 208), | ||
206 | delete: [208; 208), | ||
207 | insert: "another_good_type", | ||
208 | kind: Field, | ||
209 | detail: "u32", | ||
210 | }, | ||
211 | CompletionItem { | ||
212 | label: "another_field", | ||
213 | source_range: [208; 208), | ||
214 | delete: [208; 208), | ||
215 | insert: "another_field", | ||
216 | kind: Field, | ||
217 | detail: "i64", | ||
218 | }, | ||
219 | ] | ||
220 | "### | ||
221 | ); | ||
222 | } | ||
223 | |||
224 | #[test] | ||
107 | fn test_struct_field_completion_self() { | 225 | fn test_struct_field_completion_self() { |
108 | assert_debug_snapshot!( | 226 | assert_debug_snapshot!( |
109 | do_ref_completion( | 227 | do_ref_completion( |
diff --git a/crates/ra_ide/src/completion/completion_context.rs b/crates/ra_ide/src/completion/completion_context.rs index 14a4a14d7..eb8016dd1 100644 --- a/crates/ra_ide/src/completion/completion_context.rs +++ b/crates/ra_ide/src/completion/completion_context.rs | |||
@@ -11,7 +11,7 @@ use ra_syntax::{ | |||
11 | }; | 11 | }; |
12 | use ra_text_edit::AtomTextEdit; | 12 | use ra_text_edit::AtomTextEdit; |
13 | 13 | ||
14 | use crate::{completion::CompletionConfig, FilePosition}; | 14 | use crate::{call_info::call_info, completion::CompletionConfig, CallInfo, FilePosition}; |
15 | 15 | ||
16 | /// `CompletionContext` is created early during completion to figure out, where | 16 | /// `CompletionContext` is created early during completion to figure out, where |
17 | /// exactly is the cursor, syntax-wise. | 17 | /// exactly is the cursor, syntax-wise. |
@@ -21,6 +21,7 @@ pub(crate) struct CompletionContext<'a> { | |||
21 | pub(super) db: &'a RootDatabase, | 21 | pub(super) db: &'a RootDatabase, |
22 | pub(super) config: &'a CompletionConfig, | 22 | pub(super) config: &'a CompletionConfig, |
23 | pub(super) offset: TextUnit, | 23 | pub(super) offset: TextUnit, |
24 | pub(super) file_position: FilePosition, | ||
24 | /// The token before the cursor, in the original file. | 25 | /// The token before the cursor, in the original file. |
25 | pub(super) original_token: SyntaxToken, | 26 | pub(super) original_token: SyntaxToken, |
26 | /// The token before the cursor, in the macro-expanded file. | 27 | /// The token before the cursor, in the macro-expanded file. |
@@ -32,6 +33,7 @@ pub(crate) struct CompletionContext<'a> { | |||
32 | pub(super) record_lit_syntax: Option<ast::RecordLit>, | 33 | pub(super) record_lit_syntax: Option<ast::RecordLit>, |
33 | pub(super) record_lit_pat: Option<ast::RecordPat>, | 34 | pub(super) record_lit_pat: Option<ast::RecordPat>, |
34 | pub(super) impl_def: Option<ast::ImplDef>, | 35 | pub(super) impl_def: Option<ast::ImplDef>, |
36 | pub(super) call_info: Option<CallInfo>, | ||
35 | pub(super) is_param: bool, | 37 | pub(super) is_param: bool, |
36 | /// If a name-binding or reference to a const in a pattern. | 38 | /// If a name-binding or reference to a const in a pattern. |
37 | /// Irrefutable patterns (like let) are excluded. | 39 | /// Irrefutable patterns (like let) are excluded. |
@@ -88,9 +90,11 @@ impl<'a> CompletionContext<'a> { | |||
88 | original_token, | 90 | original_token, |
89 | token, | 91 | token, |
90 | offset: position.offset, | 92 | offset: position.offset, |
93 | file_position: position, | ||
91 | krate, | 94 | krate, |
92 | name_ref_syntax: None, | 95 | name_ref_syntax: None, |
93 | function_syntax: None, | 96 | function_syntax: None, |
97 | call_info: None, | ||
94 | use_item_syntax: None, | 98 | use_item_syntax: None, |
95 | record_lit_syntax: None, | 99 | record_lit_syntax: None, |
96 | record_lit_pat: None, | 100 | record_lit_pat: None, |
@@ -256,6 +260,8 @@ impl<'a> CompletionContext<'a> { | |||
256 | self.use_item_syntax = | 260 | self.use_item_syntax = |
257 | self.sema.ancestors_with_macros(self.token.parent()).find_map(ast::UseItem::cast); | 261 | self.sema.ancestors_with_macros(self.token.parent()).find_map(ast::UseItem::cast); |
258 | 262 | ||
263 | self.call_info = call_info(self.db, self.file_position); | ||
264 | |||
259 | self.function_syntax = self | 265 | self.function_syntax = self |
260 | .sema | 266 | .sema |
261 | .ancestors_with_macros(self.token.parent()) | 267 | .ancestors_with_macros(self.token.parent()) |
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 55f75b15a..8be2d02d0 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs | |||
@@ -367,7 +367,7 @@ mod tests { | |||
367 | ra_fixture: &str, | 367 | ra_fixture: &str, |
368 | options: CompletionConfig, | 368 | options: CompletionConfig, |
369 | ) -> Vec<CompletionItem> { | 369 | ) -> Vec<CompletionItem> { |
370 | do_completion_with_options(ra_fixture, CompletionKind::Reference, &options) | 370 | do_completion_with_options(ra_fixture, CompletionKind::Reference, &options, true) |
371 | } | 371 | } |
372 | 372 | ||
373 | #[test] | 373 | #[test] |
diff --git a/crates/ra_ide/src/completion/test_utils.rs b/crates/ra_ide/src/completion/test_utils.rs index eb90b5279..f54d15a90 100644 --- a/crates/ra_ide/src/completion/test_utils.rs +++ b/crates/ra_ide/src/completion/test_utils.rs | |||
@@ -7,13 +7,18 @@ use crate::{ | |||
7 | }; | 7 | }; |
8 | 8 | ||
9 | pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> { | 9 | pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec<CompletionItem> { |
10 | do_completion_with_options(code, kind, &CompletionConfig::default()) | 10 | do_completion_with_options(code, kind, &CompletionConfig::default(), true) |
11 | } | ||
12 | |||
13 | pub(crate) fn do_completion_without_sort(code: &str, kind: CompletionKind) -> Vec<CompletionItem> { | ||
14 | do_completion_with_options(code, kind, &CompletionConfig::default(), false) | ||
11 | } | 15 | } |
12 | 16 | ||
13 | pub(crate) fn do_completion_with_options( | 17 | pub(crate) fn do_completion_with_options( |
14 | code: &str, | 18 | code: &str, |
15 | kind: CompletionKind, | 19 | kind: CompletionKind, |
16 | options: &CompletionConfig, | 20 | options: &CompletionConfig, |
21 | sort_by_key: bool, | ||
17 | ) -> Vec<CompletionItem> { | 22 | ) -> Vec<CompletionItem> { |
18 | let (analysis, position) = if code.contains("//-") { | 23 | let (analysis, position) = if code.contains("//-") { |
19 | analysis_and_position(code) | 24 | analysis_and_position(code) |
@@ -24,6 +29,8 @@ pub(crate) fn do_completion_with_options( | |||
24 | let completion_items: Vec<CompletionItem> = completions.into(); | 29 | let completion_items: Vec<CompletionItem> = completions.into(); |
25 | let mut kind_completions: Vec<CompletionItem> = | 30 | let mut kind_completions: Vec<CompletionItem> = |
26 | completion_items.into_iter().filter(|c| c.completion_kind == kind).collect(); | 31 | completion_items.into_iter().filter(|c| c.completion_kind == kind).collect(); |
27 | kind_completions.sort_by_key(|c| c.label().to_owned()); | 32 | if sort_by_key { |
33 | kind_completions.sort_by_key(|c| c.label().to_owned()); | ||
34 | } | ||
28 | kind_completions | 35 | kind_completions |
29 | } | 36 | } |