diff options
Diffstat (limited to 'crates/ra_ide/src/completion/complete_dot.rs')
-rw-r--r-- | crates/ra_ide/src/completion/complete_dot.rs | 239 |
1 files changed, 237 insertions, 2 deletions
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs index f433faef3..c16357a7e 100644 --- a/crates/ra_ide/src/completion/complete_dot.rs +++ b/crates/ra_ide/src/completion/complete_dot.rs | |||
@@ -2,12 +2,16 @@ | |||
2 | 2 | ||
3 | use hir::{HasVisibility, Type}; | 3 | use hir::{HasVisibility, Type}; |
4 | 4 | ||
5 | use crate::completion::completion_item::CompletionKind; | ||
6 | use crate::{ | 5 | use crate::{ |
7 | completion::{completion_context::CompletionContext, completion_item::Completions}, | 6 | completion::{ |
7 | completion_context::CompletionContext, | ||
8 | completion_item::{CompletionKind, Completions}, | ||
9 | }, | ||
10 | // CallInfo, | ||
8 | CompletionItem, | 11 | CompletionItem, |
9 | }; | 12 | }; |
10 | use rustc_hash::FxHashSet; | 13 | use rustc_hash::FxHashSet; |
14 | // use std::cmp::Ordering; | ||
11 | 15 | ||
12 | /// Complete dot accesses, i.e. fields or methods (and .await syntax). | 16 | /// Complete dot accesses, i.e. fields or methods (and .await syntax). |
13 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { | 17 | pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { |
@@ -104,6 +108,237 @@ mod tests { | |||
104 | } | 108 | } |
105 | 109 | ||
106 | #[test] | 110 | #[test] |
111 | fn test_struct_field_completion_in_func_call() { | ||
112 | assert_debug_snapshot!( | ||
113 | do_ref_completion( | ||
114 | r" | ||
115 | struct A { another_field: i64, the_field: u32, my_string: String } | ||
116 | fn test(my_param: u32) -> u32 { my_param } | ||
117 | fn foo(a: A) { | ||
118 | test(a.<|>) | ||
119 | } | ||
120 | ", | ||
121 | ), | ||
122 | @r###" | ||
123 | [ | ||
124 | CompletionItem { | ||
125 | label: "another_field", | ||
126 | source_range: [201; 201), | ||
127 | delete: [201; 201), | ||
128 | insert: "another_field", | ||
129 | kind: Field, | ||
130 | detail: "i64", | ||
131 | }, | ||
132 | CompletionItem { | ||
133 | label: "my_string", | ||
134 | source_range: [201; 201), | ||
135 | delete: [201; 201), | ||
136 | insert: "my_string", | ||
137 | kind: Field, | ||
138 | detail: "{unknown}", | ||
139 | }, | ||
140 | CompletionItem { | ||
141 | label: "the_field", | ||
142 | source_range: [201; 201), | ||
143 | delete: [201; 201), | ||
144 | insert: "the_field", | ||
145 | kind: Field, | ||
146 | detail: "u32", | ||
147 | score: TypeMatch, | ||
148 | }, | ||
149 | ] | ||
150 | "### | ||
151 | ); | ||
152 | } | ||
153 | |||
154 | #[test] | ||
155 | fn test_struct_field_completion_in_func_call_with_type_and_name() { | ||
156 | assert_debug_snapshot!( | ||
157 | do_ref_completion( | ||
158 | r" | ||
159 | struct A { another_field: i64, another_good_type: u32, the_field: u32 } | ||
160 | fn test(the_field: u32) -> u32 { the_field } | ||
161 | fn foo(a: A) { | ||
162 | test(a.<|>) | ||
163 | } | ||
164 | ", | ||
165 | ), | ||
166 | @r###" | ||
167 | [ | ||
168 | CompletionItem { | ||
169 | label: "another_field", | ||
170 | source_range: [208; 208), | ||
171 | delete: [208; 208), | ||
172 | insert: "another_field", | ||
173 | kind: Field, | ||
174 | detail: "i64", | ||
175 | }, | ||
176 | CompletionItem { | ||
177 | label: "another_good_type", | ||
178 | source_range: [208; 208), | ||
179 | delete: [208; 208), | ||
180 | insert: "another_good_type", | ||
181 | kind: Field, | ||
182 | detail: "u32", | ||
183 | score: TypeMatch, | ||
184 | }, | ||
185 | CompletionItem { | ||
186 | label: "the_field", | ||
187 | source_range: [208; 208), | ||
188 | delete: [208; 208), | ||
189 | insert: "the_field", | ||
190 | kind: Field, | ||
191 | detail: "u32", | ||
192 | score: TypeAndNameMatch, | ||
193 | }, | ||
194 | ] | ||
195 | "### | ||
196 | ); | ||
197 | } | ||
198 | |||
199 | #[test] | ||
200 | fn test_struct_field_completion_in_record_lit() { | ||
201 | assert_debug_snapshot!( | ||
202 | do_ref_completion( | ||
203 | r" | ||
204 | struct A { another_field: i64, another_good_type: u32, the_field: u32 } | ||
205 | struct B { my_string: String, my_vec: Vec<u32>, the_field: u32 } | ||
206 | fn foo(a: A) { | ||
207 | let b = B { | ||
208 | the_field: a.<|> | ||
209 | }; | ||
210 | } | ||
211 | ", | ||
212 | ), | ||
213 | @r###" | ||
214 | [ | ||
215 | CompletionItem { | ||
216 | label: "another_field", | ||
217 | source_range: [270; 270), | ||
218 | delete: [270; 270), | ||
219 | insert: "another_field", | ||
220 | kind: Field, | ||
221 | detail: "i64", | ||
222 | }, | ||
223 | CompletionItem { | ||
224 | label: "another_good_type", | ||
225 | source_range: [270; 270), | ||
226 | delete: [270; 270), | ||
227 | insert: "another_good_type", | ||
228 | kind: Field, | ||
229 | detail: "u32", | ||
230 | score: TypeMatch, | ||
231 | }, | ||
232 | CompletionItem { | ||
233 | label: "the_field", | ||
234 | source_range: [270; 270), | ||
235 | delete: [270; 270), | ||
236 | insert: "the_field", | ||
237 | kind: Field, | ||
238 | detail: "u32", | ||
239 | score: TypeAndNameMatch, | ||
240 | }, | ||
241 | ] | ||
242 | "### | ||
243 | ); | ||
244 | } | ||
245 | |||
246 | #[test] | ||
247 | fn test_struct_field_completion_in_record_lit_and_fn_call() { | ||
248 | assert_debug_snapshot!( | ||
249 | do_ref_completion( | ||
250 | r" | ||
251 | struct A { another_field: i64, another_good_type: u32, the_field: u32 } | ||
252 | struct B { my_string: String, my_vec: Vec<u32>, the_field: u32 } | ||
253 | fn test(the_field: i64) -> i64 { the_field } | ||
254 | fn foo(a: A) { | ||
255 | let b = B { | ||
256 | the_field: test(a.<|>) | ||
257 | }; | ||
258 | } | ||
259 | ", | ||
260 | ), | ||
261 | @r###" | ||
262 | [ | ||
263 | CompletionItem { | ||
264 | label: "another_field", | ||
265 | source_range: [336; 336), | ||
266 | delete: [336; 336), | ||
267 | insert: "another_field", | ||
268 | kind: Field, | ||
269 | detail: "i64", | ||
270 | score: TypeMatch, | ||
271 | }, | ||
272 | CompletionItem { | ||
273 | label: "another_good_type", | ||
274 | source_range: [336; 336), | ||
275 | delete: [336; 336), | ||
276 | insert: "another_good_type", | ||
277 | kind: Field, | ||
278 | detail: "u32", | ||
279 | }, | ||
280 | CompletionItem { | ||
281 | label: "the_field", | ||
282 | source_range: [336; 336), | ||
283 | delete: [336; 336), | ||
284 | insert: "the_field", | ||
285 | kind: Field, | ||
286 | detail: "u32", | ||
287 | }, | ||
288 | ] | ||
289 | "### | ||
290 | ); | ||
291 | } | ||
292 | |||
293 | #[test] | ||
294 | fn test_struct_field_completion_in_fn_call_and_record_lit() { | ||
295 | assert_debug_snapshot!( | ||
296 | do_ref_completion( | ||
297 | r" | ||
298 | struct A { another_field: i64, another_good_type: u32, the_field: u32 } | ||
299 | struct B { my_string: String, my_vec: Vec<u32>, the_field: u32 } | ||
300 | fn test(the_field: i64) -> i64 { the_field } | ||
301 | fn foo(a: A) { | ||
302 | test(B { | ||
303 | the_field: a.<|> | ||
304 | }); | ||
305 | } | ||
306 | ", | ||
307 | ), | ||
308 | @r###" | ||
309 | [ | ||
310 | CompletionItem { | ||
311 | label: "another_field", | ||
312 | source_range: [328; 328), | ||
313 | delete: [328; 328), | ||
314 | insert: "another_field", | ||
315 | kind: Field, | ||
316 | detail: "i64", | ||
317 | }, | ||
318 | CompletionItem { | ||
319 | label: "another_good_type", | ||
320 | source_range: [328; 328), | ||
321 | delete: [328; 328), | ||
322 | insert: "another_good_type", | ||
323 | kind: Field, | ||
324 | detail: "u32", | ||
325 | score: TypeMatch, | ||
326 | }, | ||
327 | CompletionItem { | ||
328 | label: "the_field", | ||
329 | source_range: [328; 328), | ||
330 | delete: [328; 328), | ||
331 | insert: "the_field", | ||
332 | kind: Field, | ||
333 | detail: "u32", | ||
334 | score: TypeAndNameMatch, | ||
335 | }, | ||
336 | ] | ||
337 | "### | ||
338 | ); | ||
339 | } | ||
340 | |||
341 | #[test] | ||
107 | fn test_struct_field_completion_self() { | 342 | fn test_struct_field_completion_self() { |
108 | assert_debug_snapshot!( | 343 | assert_debug_snapshot!( |
109 | do_ref_completion( | 344 | do_ref_completion( |