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