diff options
Diffstat (limited to 'crates/ra_ide/src/completion/complete_dot.rs')
-rw-r--r-- | crates/ra_ide/src/completion/complete_dot.rs | 125 |
1 files changed, 122 insertions, 3 deletions
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index f433faef3..358b041aa 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 |
@@ -47,6 +72,7 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Ty | |||
47 | } | 72 | } |
48 | for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() { | 73 | for (i, ty) in receiver.tuple_fields(ctx.db).into_iter().enumerate() { |
49 | // FIXME: Handle visibility | 74 | // FIXME: Handle visibility |
75 | // TODO: add the same behavior with type ? | ||
50 | acc.add_tuple_field(ctx, i, &ty); | 76 | acc.add_tuple_field(ctx, i, &ty); |
51 | } | 77 | } |
52 | } | 78 | } |
@@ -70,13 +96,20 @@ fn complete_methods(acc: &mut Completions, ctx: &CompletionContext, receiver: &T | |||
70 | 96 | ||
71 | #[cfg(test)] | 97 | #[cfg(test)] |
72 | mod tests { | 98 | mod tests { |
73 | use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; | 99 | use crate::completion::{ |
100 | test_utils::{do_completion, do_completion_without_sort}, | ||
101 | CompletionItem, CompletionKind, | ||
102 | }; | ||
74 | use insta::assert_debug_snapshot; | 103 | use insta::assert_debug_snapshot; |
75 | 104 | ||
76 | fn do_ref_completion(code: &str) -> Vec<CompletionItem> { | 105 | fn do_ref_completion(code: &str) -> Vec<CompletionItem> { |
77 | do_completion(code, CompletionKind::Reference) | 106 | do_completion(code, CompletionKind::Reference) |
78 | } | 107 | } |
79 | 108 | ||
109 | fn do_ref_completion_without_sort(code: &str) -> Vec<CompletionItem> { | ||
110 | do_completion_without_sort(code, CompletionKind::Reference) | ||
111 | } | ||
112 | |||
80 | #[test] | 113 | #[test] |
81 | fn test_struct_field_completion() { | 114 | fn test_struct_field_completion() { |
82 | assert_debug_snapshot!( | 115 | assert_debug_snapshot!( |
@@ -104,6 +137,92 @@ mod tests { | |||
104 | } | 137 | } |
105 | 138 | ||
106 | #[test] | 139 | #[test] |
140 | fn test_struct_field_completion_in_func_call() { | ||
141 | assert_debug_snapshot!( | ||
142 | do_ref_completion_without_sort( | ||
143 | r" | ||
144 | struct A { another_field: i64, the_field: u32, my_string: String } | ||
145 | fn test(my_param: u32) -> u32 { my_param } | ||
146 | fn foo(a: A) { | ||
147 | test(a.<|>) | ||
148 | } | ||
149 | ", | ||
150 | ), | ||
151 | @r###" | ||
152 | [ | ||
153 | CompletionItem { | ||
154 | label: "the_field", | ||
155 | source_range: [201; 201), | ||
156 | delete: [201; 201), | ||
157 | insert: "the_field", | ||
158 | kind: Field, | ||
159 | detail: "u32", | ||
160 | }, | ||
161 | CompletionItem { | ||
162 | label: "another_field", | ||
163 | source_range: [201; 201), | ||
164 | delete: [201; 201), | ||
165 | insert: "another_field", | ||
166 | kind: Field, | ||
167 | detail: "i64", | ||
168 | }, | ||
169 | CompletionItem { | ||
170 | label: "my_string", | ||
171 | source_range: [201; 201), | ||
172 | delete: [201; 201), | ||
173 | insert: "my_string", | ||
174 | kind: Field, | ||
175 | detail: "{unknown}", | ||
176 | }, | ||
177 | ] | ||
178 | "### | ||
179 | ); | ||
180 | } | ||
181 | |||
182 | #[test] | ||
183 | fn test_struct_field_completion_in_func_call_with_type_and_name() { | ||
184 | assert_debug_snapshot!( | ||
185 | do_ref_completion_without_sort( | ||
186 | r" | ||
187 | struct A { another_field: i64, another_good_type: u32, the_field: u32 } | ||
188 | fn test(the_field: u32) -> u32 { the_field } | ||
189 | fn foo(a: A) { | ||
190 | test(a.<|>) | ||
191 | } | ||
192 | ", | ||
193 | ), | ||
194 | @r###" | ||
195 | [ | ||
196 | CompletionItem { | ||
197 | label: "the_field", | ||
198 | source_range: [208; 208), | ||
199 | delete: [208; 208), | ||
200 | insert: "the_field", | ||
201 | kind: Field, | ||
202 | detail: "u32", | ||
203 | }, | ||
204 | CompletionItem { | ||
205 | label: "another_good_type", | ||
206 | source_range: [208; 208), | ||
207 | delete: [208; 208), | ||
208 | insert: "another_good_type", | ||
209 | kind: Field, | ||
210 | detail: "u32", | ||
211 | }, | ||
212 | CompletionItem { | ||
213 | label: "another_field", | ||
214 | source_range: [208; 208), | ||
215 | delete: [208; 208), | ||
216 | insert: "another_field", | ||
217 | kind: Field, | ||
218 | detail: "i64", | ||
219 | }, | ||
220 | ] | ||
221 | "### | ||
222 | ); | ||
223 | } | ||
224 | |||
225 | #[test] | ||
107 | fn test_struct_field_completion_self() { | 226 | fn test_struct_field_completion_self() { |
108 | assert_debug_snapshot!( | 227 | assert_debug_snapshot!( |
109 | do_ref_completion( | 228 | do_ref_completion( |