]> git.lizzy.rs Git - rust.git/blob - crates/ide_completion/src/completions/unqualified_path.rs
Merge #8048
[rust.git] / crates / ide_completion / src / completions / unqualified_path.rs
1 //! Completion of names from the current scope, e.g. locals and imported items.
2
3 use hir::ScopeDef;
4 use syntax::AstNode;
5
6 use crate::{CompletionContext, Completions};
7
8 pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
9     if !ctx.is_trivial_path {
10         return;
11     }
12     if ctx.record_lit_syntax.is_some()
13         || ctx.record_pat_syntax.is_some()
14         || ctx.attribute_under_caret.is_some()
15         || ctx.mod_declaration_under_caret.is_some()
16     {
17         return;
18     }
19
20     if let Some(ty) = &ctx.expected_type {
21         super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
22             acc.add_qualified_enum_variant(ctx, variant, path)
23         });
24     }
25
26     ctx.scope.process_all_names(&mut |name, res| {
27         if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
28             cov_mark::hit!(skip_lifetime_completion);
29             return;
30         }
31         if ctx.use_item_syntax.is_some() {
32             if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
33                 if name_ref.syntax().text() == name.to_string().as_str() {
34                     cov_mark::hit!(self_fulfilling_completion);
35                     return;
36                 }
37             }
38         }
39         acc.add_resolution(ctx, name.to_string(), &res);
40     });
41 }
42
43 #[cfg(test)]
44 mod tests {
45     use expect_test::{expect, Expect};
46
47     use crate::{
48         test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
49         CompletionConfig, CompletionKind,
50     };
51
52     fn check(ra_fixture: &str, expect: Expect) {
53         check_with_config(TEST_CONFIG, ra_fixture, expect);
54     }
55
56     fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
57         let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
58         expect.assert_eq(&actual)
59     }
60
61     #[test]
62     fn self_fulfilling_completion() {
63         cov_mark::check!(self_fulfilling_completion);
64         check(
65             r#"
66 use foo$0
67 use std::collections;
68 "#,
69             expect![[r#"
70                 ?? collections
71             "#]],
72         );
73     }
74
75     #[test]
76     fn bind_pat_and_path_ignore_at() {
77         check(
78             r#"
79 enum Enum { A, B }
80 fn quux(x: Option<Enum>) {
81     match x {
82         None => (),
83         Some(en$0 @ Enum::A) => (),
84     }
85 }
86 "#,
87             expect![[""]],
88         );
89     }
90
91     #[test]
92     fn bind_pat_and_path_ignore_ref() {
93         check(
94             r#"
95 enum Enum { A, B }
96 fn quux(x: Option<Enum>) {
97     match x {
98         None => (),
99         Some(ref en$0) => (),
100     }
101 }
102 "#,
103             expect![[""]],
104         );
105     }
106
107     #[test]
108     fn bind_pat_and_path() {
109         check(
110             r#"
111 enum Enum { A, B }
112 fn quux(x: Option<Enum>) {
113     match x {
114         None => (),
115         Some(En$0) => (),
116     }
117 }
118 "#,
119             expect![[r#"
120                 en Enum
121             "#]],
122         );
123     }
124
125     #[test]
126     fn completes_bindings_from_let() {
127         check(
128             r#"
129 fn quux(x: i32) {
130     let y = 92;
131     1 + $0;
132     let z = ();
133 }
134 "#,
135             expect![[r#"
136                 lc y       i32
137                 lc x       i32
138                 fn quux(…) fn(i32)
139             "#]],
140         );
141     }
142
143     #[test]
144     fn completes_bindings_from_if_let() {
145         check(
146             r#"
147 fn quux() {
148     if let Some(x) = foo() {
149         let y = 92;
150     };
151     if let Some(a) = bar() {
152         let b = 62;
153         1 + $0
154     }
155 }
156 "#,
157             expect![[r#"
158                 lc b      i32
159                 lc a
160                 fn quux() fn()
161             "#]],
162         );
163     }
164
165     #[test]
166     fn completes_bindings_from_for() {
167         check(
168             r#"
169 fn quux() {
170     for x in &[1, 2, 3] { $0 }
171 }
172 "#,
173             expect![[r#"
174                 lc x
175                 fn quux() fn()
176             "#]],
177         );
178     }
179
180     #[test]
181     fn completes_if_prefix_is_keyword() {
182         cov_mark::check!(completes_if_prefix_is_keyword);
183         check_edit(
184             "wherewolf",
185             r#"
186 fn main() {
187     let wherewolf = 92;
188     drop(where$0)
189 }
190 "#,
191             r#"
192 fn main() {
193     let wherewolf = 92;
194     drop(wherewolf)
195 }
196 "#,
197         )
198     }
199
200     #[test]
201     fn completes_generic_params() {
202         check(
203             r#"fn quux<T>() { $0 }"#,
204             expect![[r#"
205                 tp T
206                 fn quux() fn()
207             "#]],
208         );
209         check(
210             r#"fn quux<const C: usize>() { $0 }"#,
211             expect![[r#"
212                 cp C
213                 fn quux() fn()
214             "#]],
215         );
216     }
217
218     #[test]
219     fn does_not_complete_lifetimes() {
220         cov_mark::check!(skip_lifetime_completion);
221         check(
222             r#"fn quux<'a>() { $0 }"#,
223             expect![[r#"
224                 fn quux() fn()
225             "#]],
226         );
227     }
228
229     #[test]
230     fn completes_generic_params_in_struct() {
231         check(
232             r#"struct S<T> { x: $0}"#,
233             expect![[r#"
234                 sp Self
235                 tp T
236                 st S<…>
237             "#]],
238         );
239     }
240
241     #[test]
242     fn completes_self_in_enum() {
243         check(
244             r#"enum X { Y($0) }"#,
245             expect![[r#"
246                 sp Self
247                 en X
248             "#]],
249         );
250     }
251
252     #[test]
253     fn completes_module_items() {
254         check(
255             r#"
256 struct S;
257 enum E {}
258 fn quux() { $0 }
259 "#,
260             expect![[r#"
261                 st S
262                 fn quux() fn()
263                 en E
264             "#]],
265         );
266     }
267
268     /// Regression test for issue #6091.
269     #[test]
270     fn correctly_completes_module_items_prefixed_with_underscore() {
271         check_edit(
272             "_alpha",
273             r#"
274 fn main() {
275     _$0
276 }
277 fn _alpha() {}
278 "#,
279             r#"
280 fn main() {
281     _alpha()$0
282 }
283 fn _alpha() {}
284 "#,
285         )
286     }
287
288     #[test]
289     fn completes_extern_prelude() {
290         check(
291             r#"
292 //- /lib.rs crate:main deps:other_crate
293 use $0;
294
295 //- /other_crate/lib.rs crate:other_crate
296 // nothing here
297 "#,
298             expect![[r#"
299                 md other_crate
300             "#]],
301         );
302     }
303
304     #[test]
305     fn completes_module_items_in_nested_modules() {
306         check(
307             r#"
308 struct Foo;
309 mod m {
310     struct Bar;
311     fn quux() { $0 }
312 }
313 "#,
314             expect![[r#"
315                 fn quux() fn()
316                 st Bar
317             "#]],
318         );
319     }
320
321     #[test]
322     fn completes_return_type() {
323         check(
324             r#"
325 struct Foo;
326 fn x() -> $0
327 "#,
328             expect![[r#"
329                 st Foo
330                 fn x() fn()
331             "#]],
332         );
333     }
334
335     #[test]
336     fn dont_show_both_completions_for_shadowing() {
337         check(
338             r#"
339 fn foo() {
340     let bar = 92;
341     {
342         let bar = 62;
343         drop($0)
344     }
345 }
346 "#,
347             // FIXME: should be only one bar here
348             expect![[r#"
349                 lc bar   i32
350                 lc bar   i32
351                 fn foo() fn()
352             "#]],
353         );
354     }
355
356     #[test]
357     fn completes_self_in_methods() {
358         check(
359             r#"impl S { fn foo(&self) { $0 } }"#,
360             expect![[r#"
361                 lc self &{unknown}
362                 sp Self
363             "#]],
364         );
365     }
366
367     #[test]
368     fn completes_prelude() {
369         check(
370             r#"
371 //- /main.rs crate:main deps:std
372 fn foo() { let x: $0 }
373
374 //- /std/lib.rs crate:std
375 #[prelude_import]
376 use prelude::*;
377
378 mod prelude { struct Option; }
379 "#,
380             expect![[r#"
381                 fn foo()  fn()
382                 md std
383                 st Option
384             "#]],
385         );
386     }
387
388     #[test]
389     fn completes_prelude_macros() {
390         check(
391             r#"
392 //- /main.rs crate:main deps:std
393 fn f() {$0}
394
395 //- /std/lib.rs crate:std
396 #[prelude_import]
397 pub use prelude::*;
398
399 #[macro_use]
400 mod prelude {
401     pub use crate::concat;
402 }
403
404 mod macros {
405     #[rustc_builtin_macro]
406     #[macro_export]
407     macro_rules! concat { }
408 }
409 "#,
410             expect![[r##"
411                 fn f()        fn()
412                 ma concat!(…) #[macro_export] macro_rules! concat
413                 md std
414             "##]],
415         );
416     }
417
418     #[test]
419     fn completes_std_prelude_if_core_is_defined() {
420         check(
421             r#"
422 //- /main.rs crate:main deps:core,std
423 fn foo() { let x: $0 }
424
425 //- /core/lib.rs crate:core
426 #[prelude_import]
427 use prelude::*;
428
429 mod prelude { struct Option; }
430
431 //- /std/lib.rs crate:std deps:core
432 #[prelude_import]
433 use prelude::*;
434
435 mod prelude { struct String; }
436 "#,
437             expect![[r#"
438                 fn foo()  fn()
439                 md std
440                 md core
441                 st String
442             "#]],
443         );
444     }
445
446     #[test]
447     fn completes_macros_as_value() {
448         check(
449             r#"
450 macro_rules! foo { () => {} }
451
452 #[macro_use]
453 mod m1 {
454     macro_rules! bar { () => {} }
455 }
456
457 mod m2 {
458     macro_rules! nope { () => {} }
459
460     #[macro_export]
461     macro_rules! baz { () => {} }
462 }
463
464 fn main() { let v = $0 }
465 "#,
466             expect![[r##"
467                 md m1
468                 ma baz!(…) #[macro_export] macro_rules! baz
469                 fn main()  fn()
470                 md m2
471                 ma bar!(…) macro_rules! bar
472                 ma foo!(…) macro_rules! foo
473             "##]],
474         );
475     }
476
477     #[test]
478     fn completes_both_macro_and_value() {
479         check(
480             r#"
481 macro_rules! foo { () => {} }
482 fn foo() { $0 }
483 "#,
484             expect![[r#"
485                 fn foo()   fn()
486                 ma foo!(…) macro_rules! foo
487             "#]],
488         );
489     }
490
491     #[test]
492     fn completes_macros_as_type() {
493         check(
494             r#"
495 macro_rules! foo { () => {} }
496 fn main() { let x: $0 }
497 "#,
498             expect![[r#"
499                 fn main()  fn()
500                 ma foo!(…) macro_rules! foo
501             "#]],
502         );
503     }
504
505     #[test]
506     fn completes_macros_as_stmt() {
507         check(
508             r#"
509 macro_rules! foo { () => {} }
510 fn main() { $0 }
511 "#,
512             expect![[r#"
513                 fn main()  fn()
514                 ma foo!(…) macro_rules! foo
515             "#]],
516         );
517     }
518
519     #[test]
520     fn completes_local_item() {
521         check(
522             r#"
523 fn main() {
524     return f$0;
525     fn frobnicate() {}
526 }
527 "#,
528             expect![[r#"
529                 fn frobnicate() fn()
530                 fn main()       fn()
531             "#]],
532         );
533     }
534
535     #[test]
536     fn completes_in_simple_macro_1() {
537         check(
538             r#"
539 macro_rules! m { ($e:expr) => { $e } }
540 fn quux(x: i32) {
541     let y = 92;
542     m!($0);
543 }
544 "#,
545             expect![[r#"
546                 lc y       i32
547                 lc x       i32
548                 fn quux(…) fn(i32)
549                 ma m!(…)   macro_rules! m
550             "#]],
551         );
552     }
553
554     #[test]
555     fn completes_in_simple_macro_2() {
556         check(
557             r"
558 macro_rules! m { ($e:expr) => { $e } }
559 fn quux(x: i32) {
560     let y = 92;
561     m!(x$0);
562 }
563 ",
564             expect![[r#"
565                 lc y       i32
566                 lc x       i32
567                 fn quux(…) fn(i32)
568                 ma m!(…)   macro_rules! m
569             "#]],
570         );
571     }
572
573     #[test]
574     fn completes_in_simple_macro_without_closing_parens() {
575         check(
576             r#"
577 macro_rules! m { ($e:expr) => { $e } }
578 fn quux(x: i32) {
579     let y = 92;
580     m!(x$0
581 }
582 "#,
583             expect![[r#"
584                 lc y       i32
585                 lc x       i32
586                 fn quux(…) fn(i32)
587                 ma m!(…)   macro_rules! m
588             "#]],
589         );
590     }
591
592     #[test]
593     fn completes_unresolved_uses() {
594         check(
595             r#"
596 use spam::Quux;
597
598 fn main() { $0 }
599 "#,
600             expect![[r#"
601                 fn main() fn()
602                 ?? Quux
603             "#]],
604         );
605     }
606
607     #[test]
608     fn completes_enum_variant_basic_expr() {
609         check(
610             r#"
611 enum Foo { Bar, Baz, Quux }
612 fn main() { let foo: Foo = Q$0 }
613 "#,
614             expect![[r#"
615                 ev Foo::Bar  ()
616                 ev Foo::Baz  ()
617                 ev Foo::Quux ()
618                 en Foo
619                 fn main()    fn()
620             "#]],
621         )
622     }
623
624     #[test]
625     fn completes_enum_variant_from_module() {
626         check(
627             r#"
628 mod m { pub enum E { V } }
629 fn f() -> m::E { V$0 }
630 "#,
631             expect![[r#"
632                 ev m::E::V ()
633                 md m
634                 fn f()     fn() -> E
635             "#]],
636         )
637     }
638
639     #[test]
640     fn dont_complete_attr() {
641         check(
642             r#"
643 struct Foo;
644 #[$0]
645 fn f() {}
646 "#,
647             expect![[""]],
648         )
649     }
650
651     #[test]
652     fn completes_type_or_trait_in_impl_block() {
653         check(
654             r#"
655 trait MyTrait {}
656 struct MyStruct {}
657
658 impl My$0
659 "#,
660             expect![[r#"
661                 sp Self
662                 tt MyTrait
663                 st MyStruct
664             "#]],
665         )
666     }
667 }