1 //! Completion of paths, i.e. `some::prefix::$0`.
5 use hir::HasVisibility;
6 use rustc_hash::FxHashSet;
7 use syntax::{ast, AstNode};
9 use crate::{context::PathCompletionContext, CompletionContext, Completions};
11 pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
12 if ctx.is_path_disallowed() {
15 let (path, use_tree_parent) = match &ctx.path_context {
16 Some(PathCompletionContext { qualifier: Some(qualifier), use_tree_parent, .. }) => {
17 (qualifier, *use_tree_parent)
22 let resolution = match ctx.sema.resolve_path(path) {
27 let context_module = ctx.scope.module();
29 if ctx.expects_item() || ctx.expects_assoc_item() {
30 if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
31 let module_scope = module.scope(ctx.db, context_module);
32 for (name, def) in module_scope {
33 if let hir::ScopeDef::MacroDef(macro_def) = def {
34 if macro_def.is_fn_like() {
35 acc.add_macro(ctx, Some(name.clone()), macro_def);
38 if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
39 acc.add_resolution(ctx, name, &def);
46 if ctx.in_use_tree() {
47 if iter::successors(Some(path.clone()), |p| p.qualifier())
48 .all(|p| p.segment().and_then(|s| s.super_token()).is_some())
50 acc.add_keyword(ctx, "super::");
52 // only show `self` in a new use-tree when the qualifier doesn't end in self
55 path.segment().and_then(|it| it.kind()),
56 Some(ast::PathSegmentKind::SelfKw)
59 acc.add_keyword(ctx, "self");
63 // Add associated types on type parameters and `Self`.
64 resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| {
65 acc.add_type_alias(ctx, alias);
70 hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
71 let module_scope = module.scope(ctx.db, context_module);
72 for (name, def) in module_scope {
73 if ctx.in_use_tree() {
74 if let hir::ScopeDef::Unknown = def {
75 if let Some(name_ref) = ctx.name_ref_syntax.as_ref() {
76 if name_ref.syntax().text() == name.to_string().as_str() {
77 // for `use self::foo$0`, don't suggest `foo` as a completion
78 cov_mark::hit!(dont_complete_current_use);
85 let add_resolution = match def {
86 // Don't suggest attribute macros and derives.
87 hir::ScopeDef::MacroDef(mac) => mac.is_fn_like(),
88 // no values in type places
89 hir::ScopeDef::ModuleDef(
90 hir::ModuleDef::Function(_)
91 | hir::ModuleDef::Variant(_)
92 | hir::ModuleDef::Static(_),
94 | hir::ScopeDef::Local(_) => !ctx.expects_type(),
95 // unless its a constant in a generic arg list position
96 hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(_)) => {
97 !ctx.expects_type() || ctx.expects_generic_arg()
103 acc.add_resolution(ctx, name, &def);
107 hir::PathResolution::Def(
110 (hir::ModuleDef::Adt(_)
111 | hir::ModuleDef::TypeAlias(_)
112 | hir::ModuleDef::BuiltinType(_)),
114 if let hir::ModuleDef::Adt(hir::Adt::Enum(e)) = def {
115 add_enum_variants(acc, ctx, e);
118 hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
119 hir::ModuleDef::TypeAlias(a) => {
120 let ty = a.ty(ctx.db);
121 if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
122 cov_mark::hit!(completes_variant_through_alias);
123 add_enum_variants(acc, ctx, e);
127 hir::ModuleDef::BuiltinType(builtin) => {
128 let module = match ctx.scope.module() {
132 cov_mark::hit!(completes_primitive_assoc_const);
133 builtin.ty(ctx.db, module)
138 // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType.
139 // (where AssocType is defined on a trait, not an inherent impl)
141 let krate = ctx.krate;
142 if let Some(krate) = krate {
143 let traits_in_scope = ctx.scope.traits_in_scope();
144 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
145 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
148 add_assoc_item(acc, ctx, item);
152 // Iterate assoc types separately
153 ty.iterate_assoc_items(ctx.db, krate, |item| {
154 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
157 if let hir::AssocItem::TypeAlias(ty) = item {
158 acc.add_type_alias(ctx, ty)
164 hir::PathResolution::Def(hir::ModuleDef::Trait(t)) => {
165 // Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
166 for item in t.items(ctx.db) {
167 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
170 add_assoc_item(acc, ctx, item);
173 hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_) => {
174 if let Some(krate) = ctx.krate {
175 let ty = match resolution {
176 hir::PathResolution::TypeParam(param) => param.ty(ctx.db),
177 hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
181 if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
182 add_enum_variants(acc, ctx, e);
185 let traits_in_scope = ctx.scope.traits_in_scope();
186 let mut seen = FxHashSet::default();
187 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
188 if context_module.map_or(false, |m| !item.is_visible_from(ctx.db, m)) {
192 // We might iterate candidates of a trait multiple times here, so deduplicate
194 if seen.insert(item) {
195 add_assoc_item(acc, ctx, item);
205 fn add_assoc_item(acc: &mut Completions, ctx: &CompletionContext, item: hir::AssocItem) {
207 hir::AssocItem::Function(func) if !ctx.expects_type() => acc.add_function(ctx, func, None),
208 hir::AssocItem::Const(ct) if !ctx.expects_type() || ctx.expects_generic_arg() => {
209 acc.add_const(ctx, ct)
211 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
216 fn add_enum_variants(acc: &mut Completions, ctx: &CompletionContext, e: hir::Enum) {
217 if ctx.expects_type() {
220 e.variants(ctx.db).into_iter().for_each(|variant| acc.add_enum_variant(ctx, variant, None));
225 use expect_test::{expect, Expect};
228 tests::{check_edit, filtered_completion_list},
232 fn check(ra_fixture: &str, expect: Expect) {
233 let actual = filtered_completion_list(ra_fixture, CompletionKind::Reference);
234 expect.assert_eq(&actual);
237 fn check_builtin(ra_fixture: &str, expect: Expect) {
238 let actual = filtered_completion_list(ra_fixture, CompletionKind::BuiltinType);
239 expect.assert_eq(&actual);
243 fn dont_complete_primitive_in_use() {
244 check_builtin(r#"use self::$0;"#, expect![[""]]);
248 fn dont_complete_primitive_in_module_scope() {
249 check_builtin(r#"fn foo() { self::$0 }"#, expect![[""]]);
253 fn completes_enum_variant() {
256 enum E { Foo, Bar(i32) }
257 fn foo() { let _ = E::$0 }
267 fn completes_struct_associated_items() {
280 fn foo() { let _ = S::$0 }
285 ct C const C: i32 = 42;
292 fn associated_item_visibility() {
299 pub(crate) fn public_method() { }
300 fn private_method() { }
301 pub(crate) type PublicType = u32;
302 type PrivateType = u32;
303 pub(crate) const PUBLIC_CONST: u32 = 1;
304 const PRIVATE_CONST: u32 = 1;
308 fn foo() { let _ = S::$0 }
311 fn public_method() fn()
312 ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1;
313 ta PublicType pub(crate) type PublicType = u32;
319 fn completes_enum_associated_method() {
323 impl E { fn m() { } }
325 fn foo() { let _ = E::$0 }
334 fn completes_union_associated_method() {
338 impl U { fn m() { } }
340 fn foo() { let _ = U::$0 }
349 fn completes_trait_associated_method_1() {
352 trait Trait { fn m(); }
354 fn foo() { let _ = Trait::$0 }
357 fn m() (as Trait) fn()
363 fn completes_trait_associated_method_2() {
366 trait Trait { fn m(); }
371 fn foo() { let _ = S::$0 }
374 fn m() (as Trait) fn()
380 fn completes_trait_associated_method_3() {
383 trait Trait { fn m(); }
388 fn foo() { let _ = <S as Trait>::$0 }
391 fn m() (as Trait) fn()
397 fn completes_ty_param_assoc_ty() {
411 fn submethod(&self) {}
414 fn foo<T: Sub>() { T::$0 }
417 ta SubTy (as Sub) type SubTy;
418 ta Ty (as Super) type Ty;
419 ct C2 (as Sub) const C2: ();
420 fn subfunc() (as Sub) fn()
421 me submethod(…) (as Sub) fn(&self)
422 ct CONST (as Super) const CONST: u8;
423 fn func() (as Super) fn()
424 me method(…) (as Super) fn(&self)
430 fn completes_self_param_assoc_ty() {
444 fn submethod(&self) {}
448 impl<T> Super for Wrap<T> {}
449 impl<T> Sub for Wrap<T> {
451 // Should be able to assume `Self: Sub + Super`
457 ta SubTy (as Sub) type SubTy;
458 ta Ty (as Super) type Ty;
459 ct CONST (as Super) const CONST: u8 = 0;
460 fn func() (as Super) fn()
461 me method(…) (as Super) fn(&self)
462 ct C2 (as Sub) const C2: () = ();
463 fn subfunc() (as Sub) fn()
464 me submethod(…) (as Sub) fn(&self)
470 fn completes_type_alias() {
474 impl S { fn foo() {} }
476 impl T { fn bar() {} }
488 fn completes_qualified_macros() {
492 macro_rules! foo { () => {} }
494 fn main() { let _ = crate::$0 }
498 ma foo!(…) #[macro_export] macro_rules! foo
504 fn does_not_complete_non_fn_macros() {
508 #[rustc_builtin_macro]
519 #[rustc_builtin_macro]
530 fn completes_reexported_items_under_correct_name() {
533 fn foo() { self::m::$0 }
536 pub use super::p::wrong_fn as right_fn;
537 pub use super::p::WRONG_CONST as RIGHT_CONST;
538 pub use super::p::WrongType as RightType;
542 const WRONG_CONST: u32 = 1;
556 fn foo() { self::m::$0 }
559 pub use super::p::wrong_fn as right_fn;
560 pub use super::p::WRONG_CONST as RIGHT_CONST;
561 pub use super::p::WrongType as RightType;
565 const WRONG_CONST: u32 = 1;
570 fn foo() { self::m::RightType }
573 pub use super::p::wrong_fn as right_fn;
574 pub use super::p::WRONG_CONST as RIGHT_CONST;
575 pub use super::p::WrongType as RightType;
579 const WRONG_CONST: u32 = 1;
587 fn completes_in_simple_macro_call() {
590 macro_rules! m { ($e:expr) => { $e } }
591 fn main() { m!(self::f$0); }
602 fn function_mod_share_name() {
605 fn foo() { self::m::$0 }
620 fn completes_hashmap_new() {
624 struct HashMap<K, V, S = RandomState> {}
626 impl<K, V> HashMap<K, V, RandomState> {
627 pub fn new() -> HashMap<K, V, RandomState> { }
634 fn new() fn() -> HashMap<K, V, RandomState>
640 fn dont_complete_attr() {
643 mod foo { pub struct Foo; }
652 fn completes_variant_through_self() {
675 fn completes_primitive_assoc_const() {
676 cov_mark::check!(completes_primitive_assoc_const);
679 //- /lib.rs crate:lib deps:core
684 //- /core.rs crate:core
687 pub const MAX: Self = 255;
693 ct MAX pub const MAX: Self = 255;
700 fn completes_variant_through_alias() {
701 cov_mark::check!(completes_variant_through_alias);