aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide/src/completion/complete_dot.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide/src/completion/complete_dot.rs')
-rw-r--r--crates/ra_ide/src/completion/complete_dot.rs141
1 files changed, 140 insertions, 1 deletions
diff --git a/crates/ra_ide/src/completion/complete_dot.rs b/crates/ra_ide/src/completion/complete_dot.rs
index cb899d8ff..2e228b638 100644
--- a/crates/ra_ide/src/completion/complete_dot.rs
+++ b/crates/ra_ide/src/completion/complete_dot.rs
@@ -50,7 +50,9 @@ fn complete_fields(acc: &mut Completions, ctx: &CompletionContext, receiver: &Ty
50 let fields = receiver.fields(ctx.db); 50 let fields = receiver.fields(ctx.db);
51 51
52 // If we use this implementation we can delete call_info in the CompletionContext 52 // If we use this implementation we can delete call_info in the CompletionContext
53 if let Some(call_info) = call_info(ctx.db, ctx.file_position) { 53 if let Some(record_field) = &ctx.record_field_syntax {
54 acc.with_sort_option(SortOption::RecordField(record_field.clone()));
55 } else if let Some(call_info) = call_info(ctx.db, ctx.file_position) {
54 acc.with_sort_option(SortOption::CallFn(call_info)); 56 acc.with_sort_option(SortOption::CallFn(call_info));
55 } 57 }
56 58
@@ -241,6 +243,143 @@ mod tests {
241 } 243 }
242 244
243 #[test] 245 #[test]
246 fn test_struct_field_completion_in_record_lit() {
247 assert_debug_snapshot!(
248 do_ref_completion_without_sort(
249 r"
250 struct A { another_field: i64, another_good_type: u32, the_field: u32 }
251 struct B { my_string: String, my_vec: Vec<u32>, the_field: u32 }
252 fn foo(a: A) {
253 let b = B {
254 the_field: a.<|>
255 };
256 }
257 ",
258 ),
259 @r###"
260 [
261 CompletionItem {
262 label: "the_field",
263 source_range: [270; 270),
264 delete: [270; 270),
265 insert: "the_field",
266 kind: Field,
267 detail: "u32",
268 },
269 CompletionItem {
270 label: "another_good_type",
271 source_range: [270; 270),
272 delete: [270; 270),
273 insert: "another_good_type",
274 kind: Field,
275 detail: "u32",
276 },
277 CompletionItem {
278 label: "another_field",
279 source_range: [270; 270),
280 delete: [270; 270),
281 insert: "another_field",
282 kind: Field,
283 detail: "i64",
284 },
285 ]
286 "###
287 );
288 }
289
290 #[test]
291 fn test_struct_field_completion_in_record_lit_and_fn_call() {
292 assert_debug_snapshot!(
293 do_ref_completion_without_sort(
294 r"
295 struct A { another_field: i64, another_good_type: u32, the_field: u32 }
296 struct B { my_string: String, my_vec: Vec<u32>, the_field: u32 }
297 fn test(the_field: i64) -> i64 { the_field }
298 fn foo(a: A) {
299 let b = B {
300 the_field: test(a.<|>)
301 };
302 }
303 ",
304 ),
305 @r###"
306 [
307 CompletionItem {
308 label: "another_field",
309 source_range: [336; 336),
310 delete: [336; 336),
311 insert: "another_field",
312 kind: Field,
313 detail: "i64",
314 },
315 CompletionItem {
316 label: "another_good_type",
317 source_range: [336; 336),
318 delete: [336; 336),
319 insert: "another_good_type",
320 kind: Field,
321 detail: "u32",
322 },
323 CompletionItem {
324 label: "the_field",
325 source_range: [336; 336),
326 delete: [336; 336),
327 insert: "the_field",
328 kind: Field,
329 detail: "u32",
330 },
331 ]
332 "###
333 );
334 }
335
336 #[test]
337 fn test_struct_field_completion_in_fn_call_and_record_lit() {
338 assert_debug_snapshot!(
339 do_ref_completion_without_sort(
340 r"
341 struct A { another_field: i64, another_good_type: u32, the_field: u32 }
342 struct B { my_string: String, my_vec: Vec<u32>, the_field: u32 }
343 fn test(the_field: i64) -> i64 { the_field }
344 fn foo(a: A) {
345 test(B {
346 the_field: a.<|>
347 });
348 }
349 ",
350 ),
351 @r###"
352 [
353 CompletionItem {
354 label: "the_field",
355 source_range: [328; 328),
356 delete: [328; 328),
357 insert: "the_field",
358 kind: Field,
359 detail: "u32",
360 },
361 CompletionItem {
362 label: "another_good_type",
363 source_range: [328; 328),
364 delete: [328; 328),
365 insert: "another_good_type",
366 kind: Field,
367 detail: "u32",
368 },
369 CompletionItem {
370 label: "another_field",
371 source_range: [328; 328),
372 delete: [328; 328),
373 insert: "another_field",
374 kind: Field,
375 detail: "i64",
376 },
377 ]
378 "###
379 );
380 }
381
382 #[test]
244 fn test_struct_field_completion_self() { 383 fn test_struct_field_completion_self() {
245 assert_debug_snapshot!( 384 assert_debug_snapshot!(
246 do_ref_completion( 385 do_ref_completion(