1 //! Completion of paths, i.e. `some::prefix::$0`.
3 use hir::{Adt, HasVisibility, PathResolution, ScopeDef};
4 use rustc_hash::FxHashSet;
8 use crate::{CompletionContext, Completions};
10 pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
11 let path = match &ctx.path_qual {
12 Some(path) => path.clone(),
16 if ctx.attribute_under_caret.is_some() || ctx.mod_declaration_under_caret.is_some() {
20 let context_module = ctx.scope.module();
22 let resolution = match ctx.sema.resolve_path(&path) {
27 // Add associated types on type parameters and `Self`.
28 resolution.assoc_type_shorthand_candidates(ctx.db, |alias| {
29 acc.add_type_alias(ctx, alias);
34 PathResolution::Def(hir::ModuleDef::Module(module)) => {
35 let module_scope = module.scope(ctx.db, context_module);
36 for (name, def) in module_scope {
37 if ctx.use_item_syntax.is_some() {
38 if let ScopeDef::Unknown = def {
39 if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
40 if name_ref.syntax().text() == name.to_string().as_str() {
41 // for `use self::foo$0`, don't suggest `foo` as a completion
42 mark::hit!(dont_complete_current_use);
49 acc.add_resolution(ctx, name.to_string(), &def);
52 PathResolution::Def(def @ hir::ModuleDef::Adt(_))
53 | PathResolution::Def(def @ hir::ModuleDef::TypeAlias(_)) => {
54 if let hir::ModuleDef::Adt(Adt::Enum(e)) = def {
55 for variant in e.variants(ctx.db) {
56 acc.add_enum_variant(ctx, variant, None);
60 hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
61 hir::ModuleDef::TypeAlias(a) => a.ty(ctx.db),
65 // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType.
66 // (where AssocType is defined on a trait, not an inherent impl)
68 let krate = ctx.krate;
69 if let Some(krate) = krate {
70 let traits_in_scope = ctx.scope.traits_in_scope();
71 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
72 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
76 hir::AssocItem::Function(func) => {
77 acc.add_function(ctx, func, None);
79 hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
80 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
85 // Iterate assoc types separately
86 ty.iterate_assoc_items(ctx.db, krate, |item| {
87 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
91 hir::AssocItem::Function(_) | hir::AssocItem::Const(_) => {}
92 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
98 PathResolution::Def(hir::ModuleDef::Trait(t)) => {
99 // Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
100 for item in t.items(ctx.db) {
101 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
105 hir::AssocItem::Function(func) => {
106 acc.add_function(ctx, func, None);
108 hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
109 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
113 PathResolution::TypeParam(_) | PathResolution::SelfType(_) => {
114 if let Some(krate) = ctx.krate {
115 let ty = match resolution {
116 PathResolution::TypeParam(param) => param.ty(ctx.db),
117 PathResolution::SelfType(impl_def) => impl_def.target_ty(ctx.db),
121 if let Some(Adt::Enum(e)) = ty.as_adt() {
122 for variant in e.variants(ctx.db) {
123 acc.add_enum_variant(ctx, variant, None);
127 let traits_in_scope = ctx.scope.traits_in_scope();
128 let mut seen = FxHashSet::default();
129 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
130 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
134 // We might iterate candidates of a trait multiple times here, so deduplicate
136 if seen.insert(item) {
138 hir::AssocItem::Function(func) => {
139 acc.add_function(ctx, func, None);
141 hir::AssocItem::Const(ct) => acc.add_const(ctx, ct),
142 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
155 use expect_test::{expect, Expect};
156 use test_utils::mark;
159 test_utils::{check_edit, completion_list},
163 fn check(ra_fixture: &str, expect: Expect) {
164 let actual = completion_list(ra_fixture, CompletionKind::Reference);
165 expect.assert_eq(&actual);
168 fn check_builtin(ra_fixture: &str, expect: Expect) {
169 let actual = completion_list(ra_fixture, CompletionKind::BuiltinType);
170 expect.assert_eq(&actual);
174 fn dont_complete_current_use() {
175 mark::check!(dont_complete_current_use);
176 check(r#"use self::foo$0;"#, expect![[""]]);
180 fn dont_complete_current_use_in_braces_with_glob() {
183 mod foo { pub struct S; }
184 use self::{foo::*, bar$0};
194 fn dont_complete_primitive_in_use() {
195 check_builtin(r#"use self::$0;"#, expect![[""]]);
199 fn dont_complete_primitive_in_module_scope() {
200 check_builtin(r#"fn foo() { self::$0 }"#, expect![[""]]);
204 fn completes_primitives() {
206 r#"fn main() { let _: $0 = 92; }"#,
230 fn completes_mod_with_same_name_as_function() {
235 mod my { pub struct Bar; }
245 fn filters_visibility() {
253 pub use Bar as PublicBar;
264 fn completes_use_item_starting_with_self() {
269 mod m { pub struct Bar; }
278 fn completes_use_item_starting_with_crate() {
295 fn completes_nested_use_tree() {
312 fn completes_deeply_nested_use_tree() {
323 use crate::{bar::{baz::Sp$0}};
332 fn completes_enum_variant() {
335 enum E { Foo, Bar(i32) }
336 fn foo() { let _ = E::$0 }
346 fn completes_struct_associated_items() {
359 fn foo() { let _ = S::$0 }
364 ct C const C: i32 = 42;
371 fn associated_item_visibility() {
378 pub(crate) fn public_method() { }
379 fn private_method() { }
380 pub(crate) type PublicType = u32;
381 type PrivateType = u32;
382 pub(crate) const PUBLIC_CONST: u32 = 1;
383 const PRIVATE_CONST: u32 = 1;
387 fn foo() { let _ = S::$0 }
390 fn public_method() -> ()
391 ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1;
392 ta PublicType pub(crate) type PublicType = u32;
398 fn completes_enum_associated_method() {
402 impl E { fn m() { } }
404 fn foo() { let _ = E::$0 }
413 fn completes_union_associated_method() {
417 impl U { fn m() { } }
419 fn foo() { let _ = U::$0 }
428 fn completes_use_paths_across_crates() {
431 //- /main.rs crate:main deps:foo
434 //- /foo/lib.rs crate:foo
435 pub mod bar { pub struct S; }
444 fn completes_trait_associated_method_1() {
447 trait Trait { fn m(); }
449 fn foo() { let _ = Trait::$0 }
458 fn completes_trait_associated_method_2() {
461 trait Trait { fn m(); }
466 fn foo() { let _ = S::$0 }
475 fn completes_trait_associated_method_3() {
478 trait Trait { fn m(); }
483 fn foo() { let _ = <S as Trait>::$0 }
492 fn completes_ty_param_assoc_ty() {
506 fn submethod(&self) {}
509 fn foo<T: Sub>() { T::$0 }
516 me submethod(…) -> ()
517 ct CONST const CONST: u8;
525 fn completes_self_param_assoc_ty() {
539 fn submethod(&self) {}
543 impl<T> Super for Wrap<T> {}
544 impl<T> Sub for Wrap<T> {
546 // Should be able to assume `Self: Sub + Super`
554 ct CONST const CONST: u8 = 0;
557 ct C2 const C2: () = ();
559 me submethod(…) -> ()
565 fn completes_type_alias() {
569 impl S { fn foo() {} }
571 impl T { fn bar() {} }
583 fn completes_qualified_macros() {
587 macro_rules! foo { () => {} }
589 fn main() { let _ = crate::$0 }
593 ma foo!(…) #[macro_export] macro_rules! foo
599 fn test_super_super_completion() {
606 mod c { use super::super::$0 }
618 fn completes_reexported_items_under_correct_name() {
621 fn foo() { self::m::$0 }
624 pub use super::p::wrong_fn as right_fn;
625 pub use super::p::WRONG_CONST as RIGHT_CONST;
626 pub use super::p::WrongType as RightType;
630 const WRONG_CONST: u32 = 1;
644 fn foo() { self::m::$0 }
647 pub use super::p::wrong_fn as right_fn;
648 pub use super::p::WRONG_CONST as RIGHT_CONST;
649 pub use super::p::WrongType as RightType;
653 const WRONG_CONST: u32 = 1;
658 fn foo() { self::m::RightType }
661 pub use super::p::wrong_fn as right_fn;
662 pub use super::p::WRONG_CONST as RIGHT_CONST;
663 pub use super::p::WrongType as RightType;
667 const WRONG_CONST: u32 = 1;
675 fn completes_in_simple_macro_call() {
678 macro_rules! m { ($e:expr) => { $e } }
679 fn main() { m!(self::f$0); }
690 fn function_mod_share_name() {
693 fn foo() { self::m::$0 }
708 fn completes_hashmap_new() {
712 struct HashMap<K, V, S = RandomState> {}
714 impl<K, V> HashMap<K, V, RandomState> {
715 pub fn new() -> HashMap<K, V, RandomState> { }
722 fn new() -> HashMap<K, V, RandomState>
728 fn dont_complete_attr() {
731 mod foo { pub struct Foo; }
740 fn completes_function() {
762 fn completes_self_enum() {