1 //! Completion of paths, i.e. `some::prefix::$0`.
5 use rustc_hash::FxHashSet;
6 use syntax::{ast, AstNode};
9 context::PathCompletionContext, patterns::ImmediateLocation, CompletionContext, Completions,
12 pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
13 if ctx.is_path_disallowed() || ctx.has_impl_or_trait_prev_sibling() {
16 let (path, use_tree_parent) = match &ctx.path_context {
17 Some(PathCompletionContext { qualifier: Some(qualifier), use_tree_parent, .. }) => {
18 (qualifier, *use_tree_parent)
23 let resolution = match ctx.sema.resolve_path(path) {
28 let context_module = ctx.scope.module();
30 match ctx.completion_location {
31 Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) => {
32 if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
33 for (name, def) in module.scope(ctx.db, context_module) {
34 if let hir::ScopeDef::MacroDef(macro_def) = def {
35 if macro_def.is_fn_like() {
36 acc.add_macro(ctx, Some(name.clone()), macro_def);
39 if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
40 acc.add_resolution(ctx, name, &def);
46 Some(ImmediateLocation::Visibility(_)) => {
47 if let hir::PathResolution::Def(hir::ModuleDef::Module(resolved)) = resolution {
48 if let Some(current_module) = ctx.scope.module() {
49 if let Some(next) = current_module
52 .take_while(|&it| it != resolved)
55 if let Some(name) = next.name(ctx.db) {
56 acc.add_resolution(ctx, name, &hir::ScopeDef::ModuleDef(next.into()));
66 if ctx.in_use_tree() {
67 if iter::successors(Some(path.clone()), |p| p.qualifier())
68 .all(|p| p.segment().and_then(|s| s.super_token()).is_some())
70 acc.add_keyword(ctx, "super::");
72 // only show `self` in a new use-tree when the qualifier doesn't end in self
75 path.segment().and_then(|it| it.kind()),
76 Some(ast::PathSegmentKind::SelfKw)
79 acc.add_keyword(ctx, "self");
83 // Add associated types on type parameters and `Self`.
84 resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| {
85 acc.add_type_alias(ctx, alias);
90 hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
91 let module_scope = module.scope(ctx.db, context_module);
92 for (name, def) in module_scope {
93 if ctx.in_use_tree() {
94 if let hir::ScopeDef::Unknown = def {
95 if let Some(ast::NameLike::NameRef(name_ref)) = ctx.name_syntax.as_ref() {
96 if name_ref.syntax().text() == name.to_string().as_str() {
97 // for `use self::foo$0`, don't suggest `foo` as a completion
98 cov_mark::hit!(dont_complete_current_use);
105 if ctx.is_scope_def_hidden(&def) {
106 cov_mark::hit!(qualified_path_doc_hidden);
110 let add_resolution = match def {
111 // Don't suggest attribute macros and derives.
112 hir::ScopeDef::MacroDef(mac) => mac.is_fn_like(),
113 // no values in type places
114 hir::ScopeDef::ModuleDef(
115 hir::ModuleDef::Function(_)
116 | hir::ModuleDef::Variant(_)
117 | hir::ModuleDef::Static(_),
119 | hir::ScopeDef::Local(_) => !ctx.expects_type(),
120 // unless its a constant in a generic arg list position
121 hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(_)) => {
122 !ctx.expects_type() || ctx.expects_generic_arg()
128 acc.add_resolution(ctx, name, &def);
132 hir::PathResolution::Def(
135 (hir::ModuleDef::Adt(_)
136 | hir::ModuleDef::TypeAlias(_)
137 | hir::ModuleDef::BuiltinType(_)),
139 if let hir::ModuleDef::Adt(hir::Adt::Enum(e)) = def {
140 add_enum_variants(acc, ctx, e);
143 hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
144 hir::ModuleDef::TypeAlias(a) => {
145 let ty = a.ty(ctx.db);
146 if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
147 cov_mark::hit!(completes_variant_through_alias);
148 add_enum_variants(acc, ctx, e);
152 hir::ModuleDef::BuiltinType(builtin) => {
153 let module = match ctx.scope.module() {
157 cov_mark::hit!(completes_primitive_assoc_const);
158 builtin.ty(ctx.db, module)
163 // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType.
164 // (where AssocType is defined on a trait, not an inherent impl)
166 let krate = ctx.krate;
167 if let Some(krate) = krate {
168 let traits_in_scope = ctx.scope.traits_in_scope();
169 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
170 if !ctx.is_visible(&item) {
173 add_assoc_item(acc, ctx, item);
177 // Iterate assoc types separately
178 ty.iterate_assoc_items(ctx.db, krate, |item| {
179 if !ctx.is_visible(&item) {
182 if let hir::AssocItem::TypeAlias(ty) = item {
183 acc.add_type_alias(ctx, ty)
189 hir::PathResolution::Def(hir::ModuleDef::Trait(t)) => {
190 // Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
191 for item in t.items(ctx.db) {
192 if !ctx.is_visible(&item) {
195 add_assoc_item(acc, ctx, item);
198 hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_) => {
199 if let Some(krate) = ctx.krate {
200 let ty = match resolution {
201 hir::PathResolution::TypeParam(param) => param.ty(ctx.db),
202 hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
206 if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
207 add_enum_variants(acc, ctx, e);
210 let traits_in_scope = ctx.scope.traits_in_scope();
211 let mut seen = FxHashSet::default();
212 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
213 if !ctx.is_visible(&item) {
217 // We might iterate candidates of a trait multiple times here, so deduplicate
219 if seen.insert(item) {
220 add_assoc_item(acc, ctx, item);
226 hir::PathResolution::Macro(mac) => acc.add_macro(ctx, None, mac),
231 fn add_assoc_item(acc: &mut Completions, ctx: &CompletionContext, item: hir::AssocItem) {
233 hir::AssocItem::Function(func) if !ctx.expects_type() => acc.add_function(ctx, func, None),
234 hir::AssocItem::Const(ct) if !ctx.expects_type() || ctx.expects_generic_arg() => {
235 acc.add_const(ctx, ct)
237 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
242 fn add_enum_variants(acc: &mut Completions, ctx: &CompletionContext, e: hir::Enum) {
243 if ctx.expects_type() {
246 e.variants(ctx.db).into_iter().for_each(|variant| acc.add_enum_variant(ctx, variant, None));
251 use expect_test::{expect, Expect};
254 tests::{check_edit, filtered_completion_list},
258 fn check(ra_fixture: &str, expect: Expect) {
259 let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
260 expect.assert_eq(&actual);
264 fn associated_item_visibility() {
267 //- /lib.rs crate:lib new_source_root:library
271 pub fn public_method() { }
272 fn private_method() { }
273 pub type PublicType = u32;
274 type PrivateType = u32;
275 pub const PUBLIC_CONST: u32 = 1;
276 const PRIVATE_CONST: u32 = 1;
279 //- /main.rs crate:main deps:lib new_source_root:local
280 fn foo() { let _ = lib::S::$0 }
283 fn public_method() fn()
284 ct PUBLIC_CONST pub const PUBLIC_CONST: u32 = 1;
285 ta PublicType pub type PublicType = u32;
291 fn completes_union_associated_method() {
295 impl U { fn m() { } }
297 fn foo() { let _ = U::$0 }
306 fn completes_trait_associated_method_1() {
309 trait Trait { fn m(); }
311 fn foo() { let _ = Trait::$0 }
314 fn m() (as Trait) fn()
320 fn completes_trait_associated_method_2() {
323 trait Trait { fn m(); }
328 fn foo() { let _ = S::$0 }
331 fn m() (as Trait) fn()
337 fn completes_trait_associated_method_3() {
340 trait Trait { fn m(); }
345 fn foo() { let _ = <S as Trait>::$0 }
348 fn m() (as Trait) fn()
354 fn completes_ty_param_assoc_ty() {
368 fn submethod(&self) {}
371 fn foo<T: Sub>() { T::$0 }
374 ta SubTy (as Sub) type SubTy;
375 ta Ty (as Super) type Ty;
376 ct C2 (as Sub) const C2: ();
377 fn subfunc() (as Sub) fn()
378 me submethod(…) (as Sub) fn(&self)
379 ct CONST (as Super) const CONST: u8;
380 fn func() (as Super) fn()
381 me method(…) (as Super) fn(&self)
387 fn completes_self_param_assoc_ty() {
401 fn submethod(&self) {}
405 impl<T> Super for Wrap<T> {}
406 impl<T> Sub for Wrap<T> {
408 // Should be able to assume `Self: Sub + Super`
414 ta SubTy (as Sub) type SubTy;
415 ta Ty (as Super) type Ty;
416 ct CONST (as Super) const CONST: u8 = 0;
417 fn func() (as Super) fn()
418 me method(…) (as Super) fn(&self)
419 ct C2 (as Sub) const C2: () = ();
420 fn subfunc() (as Sub) fn()
421 me submethod(…) (as Sub) fn(&self)
427 fn completes_type_alias() {
431 impl S { fn foo() {} }
433 impl T { fn bar() {} }
445 fn completes_qualified_macros() {
449 macro_rules! foo { () => {} }
451 fn main() { let _ = crate::$0 }
455 ma foo!(…) #[macro_export] macro_rules! foo
461 fn does_not_complete_non_fn_macros() {
465 #[rustc_builtin_macro]
476 #[rustc_builtin_macro]
487 fn completes_reexported_items_under_correct_name() {
490 fn foo() { self::m::$0 }
493 pub use super::p::wrong_fn as right_fn;
494 pub use super::p::WRONG_CONST as RIGHT_CONST;
495 pub use super::p::WrongType as RightType;
499 const WRONG_CONST: u32 = 1;
513 fn foo() { self::m::$0 }
516 pub use super::p::wrong_fn as right_fn;
517 pub use super::p::WRONG_CONST as RIGHT_CONST;
518 pub use super::p::WrongType as RightType;
522 const WRONG_CONST: u32 = 1;
527 fn foo() { self::m::RightType }
530 pub use super::p::wrong_fn as right_fn;
531 pub use super::p::WRONG_CONST as RIGHT_CONST;
532 pub use super::p::WrongType as RightType;
536 const WRONG_CONST: u32 = 1;
544 fn completes_in_simple_macro_call() {
547 macro_rules! m { ($e:expr) => { $e } }
548 fn main() { m!(self::f$0); }
559 fn function_mod_share_name() {
562 fn foo() { self::m::$0 }
577 fn completes_hashmap_new() {
581 struct HashMap<K, V, S = RandomState> {}
583 impl<K, V> HashMap<K, V, RandomState> {
584 pub fn new() -> HashMap<K, V, RandomState> { }
591 fn new() fn() -> HashMap<K, V, RandomState>
597 fn dont_complete_attr() {
600 mod foo { pub struct Foo; }
609 fn completes_variant_through_self() {
632 fn completes_primitive_assoc_const() {
633 cov_mark::check!(completes_primitive_assoc_const);
636 //- /lib.rs crate:lib deps:core
641 //- /core.rs crate:core
644 pub const MAX: Self = 255;
650 ct MAX pub const MAX: Self = 255;
657 fn completes_variant_through_alias() {
658 cov_mark::check!(completes_variant_through_alias);
676 fn respects_doc_hidden() {
677 cov_mark::check!(qualified_path_doc_hidden);
680 //- /lib.rs crate:lib deps:dep
685 //- /dep.rs crate:dep