1 //! Completion of paths, i.e. `some::prefix::$0`.
6 use rustc_hash::FxHashSet;
7 use syntax::{ast, AstNode};
10 context::{PathCompletionContext, PathKind},
11 patterns::ImmediateLocation,
12 CompletionContext, Completions,
15 pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
16 if ctx.is_path_disallowed() || ctx.has_impl_or_trait_prev_sibling() {
19 let (path, use_tree_parent, kind) = match ctx.path_context {
20 // let ... else, syntax would come in really handy here right now
21 Some(PathCompletionContext {
22 qualifier: Some(ref qualifier),
26 }) => (qualifier, use_tree_parent, kind),
30 let resolution = match ctx.sema.resolve_path(path) {
35 let context_module = ctx.scope.module();
37 match ctx.completion_location {
38 Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) => {
39 if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
40 for (name, def) in module.scope(ctx.db, context_module) {
41 if let ScopeDef::MacroDef(macro_def) = def {
42 if macro_def.is_fn_like() {
43 acc.add_macro(ctx, Some(name.clone()), macro_def);
46 if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = def {
47 acc.add_resolution(ctx, name, &def);
57 Some(PathKind::Vis { .. }) => {
58 if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
59 if let Some(current_module) = ctx.scope.module() {
60 if let Some(next) = current_module
63 .take_while(|&it| it != module)
66 if let Some(name) = next.name(ctx.db) {
67 acc.add_resolution(ctx, name, &ScopeDef::ModuleDef(next.into()));
74 Some(PathKind::Attr) => {
75 if let hir::PathResolution::Def(hir::ModuleDef::Module(module)) = resolution {
76 for (name, def) in module.scope(ctx.db, context_module) {
77 let add_resolution = match def {
78 ScopeDef::MacroDef(mac) => mac.is_attr(),
79 ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => true,
83 acc.add_resolution(ctx, name, &def);
89 Some(PathKind::Use) => {
90 if iter::successors(Some(path.clone()), |p| p.qualifier())
91 .all(|p| p.segment().and_then(|s| s.super_token()).is_some())
93 acc.add_keyword(ctx, "super::");
95 // only show `self` in a new use-tree when the qualifier doesn't end in self
98 path.segment().and_then(|it| it.kind()),
99 Some(ast::PathSegmentKind::SelfKw)
102 acc.add_keyword(ctx, "self");
108 if !matches!(kind, Some(PathKind::Pat)) {
109 // Add associated types on type parameters and `Self`.
110 resolution.assoc_type_shorthand_candidates(ctx.db, |_, alias| {
111 acc.add_type_alias(ctx, alias);
117 hir::PathResolution::Def(hir::ModuleDef::Module(module)) => {
118 let module_scope = module.scope(ctx.db, context_module);
119 for (name, def) in module_scope {
120 if let Some(PathKind::Use) = kind {
121 if let ScopeDef::Unknown = def {
122 if let Some(ast::NameLike::NameRef(name_ref)) = ctx.name_syntax.as_ref() {
123 if name_ref.syntax().text() == name.to_smol_str().as_str() {
124 // for `use self::foo$0`, don't suggest `foo` as a completion
125 cov_mark::hit!(dont_complete_current_use);
132 let add_resolution = match def {
133 // Don't suggest attribute macros and derives.
134 ScopeDef::MacroDef(mac) => mac.is_fn_like(),
135 // no values in type places
137 hir::ModuleDef::Function(_)
138 | hir::ModuleDef::Variant(_)
139 | hir::ModuleDef::Static(_),
141 | ScopeDef::Local(_) => !ctx.expects_type(),
142 // unless its a constant in a generic arg list position
143 ScopeDef::ModuleDef(hir::ModuleDef::Const(_)) => {
144 !ctx.expects_type() || ctx.expects_generic_arg()
150 acc.add_resolution(ctx, name, &def);
154 hir::PathResolution::Def(
155 def @ (hir::ModuleDef::Adt(_)
156 | hir::ModuleDef::TypeAlias(_)
157 | hir::ModuleDef::BuiltinType(_)),
159 if let hir::ModuleDef::Adt(hir::Adt::Enum(e)) = def {
160 add_enum_variants(acc, ctx, e);
163 hir::ModuleDef::Adt(adt) => adt.ty(ctx.db),
164 hir::ModuleDef::TypeAlias(a) => {
165 let ty = a.ty(ctx.db);
166 if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
167 cov_mark::hit!(completes_variant_through_alias);
168 add_enum_variants(acc, ctx, e);
172 hir::ModuleDef::BuiltinType(builtin) => {
173 let module = match ctx.scope.module() {
177 cov_mark::hit!(completes_primitive_assoc_const);
178 builtin.ty(ctx.db, module)
183 // XXX: For parity with Rust bug #22519, this does not complete Ty::AssocType.
184 // (where AssocType is defined on a trait, not an inherent impl)
186 let krate = ctx.krate;
187 if let Some(krate) = krate {
188 let traits_in_scope = ctx.scope.visible_traits();
189 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
190 add_assoc_item(acc, ctx, item);
194 // Iterate assoc types separately
195 ty.iterate_assoc_items(ctx.db, krate, |item| {
196 if let hir::AssocItem::TypeAlias(ty) = item {
197 acc.add_type_alias(ctx, ty)
203 hir::PathResolution::Def(hir::ModuleDef::Trait(t)) => {
204 // Handles `Trait::assoc` as well as `<Ty as Trait>::assoc`.
205 for item in t.items(ctx.db) {
206 add_assoc_item(acc, ctx, item);
209 hir::PathResolution::TypeParam(_) | hir::PathResolution::SelfType(_) => {
210 if let Some(krate) = ctx.krate {
211 let ty = match resolution {
212 hir::PathResolution::TypeParam(param) => param.ty(ctx.db),
213 hir::PathResolution::SelfType(impl_def) => impl_def.self_ty(ctx.db),
217 if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
218 add_enum_variants(acc, ctx, e);
221 let traits_in_scope = ctx.scope.visible_traits();
222 let mut seen = FxHashSet::default();
223 ty.iterate_path_candidates(ctx.db, krate, &traits_in_scope, None, |_ty, item| {
224 // We might iterate candidates of a trait multiple times here, so deduplicate
226 if seen.insert(item) {
227 add_assoc_item(acc, ctx, item);
233 hir::PathResolution::Macro(mac) => acc.add_macro(ctx, None, mac),
238 fn add_assoc_item(acc: &mut Completions, ctx: &CompletionContext, item: hir::AssocItem) {
240 hir::AssocItem::Function(func) if !ctx.expects_type() => acc.add_function(ctx, func, None),
241 hir::AssocItem::Const(ct) if !ctx.expects_type() || ctx.expects_generic_arg() => {
242 acc.add_const(ctx, ct)
244 hir::AssocItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
249 fn add_enum_variants(acc: &mut Completions, ctx: &CompletionContext, e: hir::Enum) {
250 if ctx.expects_type() {
253 e.variants(ctx.db).into_iter().for_each(|variant| acc.add_enum_variant(ctx, variant, None));
258 use expect_test::{expect, Expect};
260 use crate::tests::{check_edit, completion_list_no_kw};
262 fn check(ra_fixture: &str, expect: Expect) {
263 let actual = completion_list_no_kw(ra_fixture);
264 expect.assert_eq(&actual);
268 fn associated_item_visibility() {
271 //- /lib.rs crate:lib new_source_root:library
275 pub fn public_method() { }
276 fn private_method() { }
277 pub type PublicType = u32;
278 type PrivateType = u32;
279 pub const PUBLIC_CONST: u32 = 1;
280 const PRIVATE_CONST: u32 = 1;
283 //- /main.rs crate:main deps:lib new_source_root:local
284 fn foo() { let _ = lib::S::$0 }
287 fn public_method() fn()
288 ct PUBLIC_CONST pub const PUBLIC_CONST: u32;
289 ta PublicType pub type PublicType;
295 fn completes_union_associated_method() {
299 impl U { fn m() { } }
301 fn foo() { let _ = U::$0 }
310 fn completes_trait_associated_method_1() {
313 trait Trait { fn m(); }
315 fn foo() { let _ = Trait::$0 }
318 fn m() (as Trait) fn()
324 fn completes_trait_associated_method_2() {
327 trait Trait { fn m(); }
332 fn foo() { let _ = S::$0 }
335 fn m() (as Trait) fn()
341 fn completes_trait_associated_method_3() {
344 trait Trait { fn m(); }
349 fn foo() { let _ = <S as Trait>::$0 }
352 fn m() (as Trait) fn()
358 fn completes_ty_param_assoc_ty() {
372 fn submethod(&self) {}
375 fn foo<T: Sub>() { T::$0 }
378 ta SubTy (as Sub) type SubTy;
379 ta Ty (as Super) type Ty;
380 ct C2 (as Sub) const C2: ();
381 fn subfunc() (as Sub) fn()
382 me submethod(…) (as Sub) fn(&self)
383 ct CONST (as Super) const CONST: u8;
384 fn func() (as Super) fn()
385 me method(…) (as Super) fn(&self)
391 fn completes_self_param_assoc_ty() {
405 fn submethod(&self) {}
409 impl<T> Super for Wrap<T> {}
410 impl<T> Sub for Wrap<T> {
412 // Should be able to assume `Self: Sub + Super`
418 ta SubTy (as Sub) type SubTy;
419 ta Ty (as Super) type Ty;
420 ct CONST (as Super) const CONST: u8;
421 fn func() (as Super) fn()
422 me method(…) (as Super) fn(&self)
423 ct C2 (as Sub) const C2: ();
424 fn subfunc() (as Sub) fn()
425 me submethod(…) (as Sub) fn(&self)
431 fn completes_type_alias() {
435 impl S { fn foo() {} }
437 impl T { fn bar() {} }
449 fn completes_qualified_macros() {
453 macro_rules! foo { () => {} }
455 fn main() { let _ = crate::$0 }
459 ma foo!(…) #[macro_export] macro_rules! foo
465 fn does_not_complete_non_fn_macros() {
469 #[rustc_builtin_macro]
480 #[rustc_builtin_macro]
491 fn completes_reexported_items_under_correct_name() {
494 fn foo() { self::m::$0 }
497 pub use super::p::wrong_fn as right_fn;
498 pub use super::p::WRONG_CONST as RIGHT_CONST;
499 pub use super::p::WrongType as RightType;
503 const WRONG_CONST: u32 = 1;
517 fn foo() { self::m::$0 }
520 pub use super::p::wrong_fn as right_fn;
521 pub use super::p::WRONG_CONST as RIGHT_CONST;
522 pub use super::p::WrongType as RightType;
526 const WRONG_CONST: u32 = 1;
531 fn foo() { self::m::RightType }
534 pub use super::p::wrong_fn as right_fn;
535 pub use super::p::WRONG_CONST as RIGHT_CONST;
536 pub use super::p::WrongType as RightType;
540 const WRONG_CONST: u32 = 1;
548 fn completes_in_simple_macro_call() {
551 macro_rules! m { ($e:expr) => { $e } }
552 fn main() { m!(self::f$0); }
563 fn function_mod_share_name() {
566 fn foo() { self::m::$0 }
581 fn completes_hashmap_new() {
585 struct HashMap<K, V, S = RandomState> {}
587 impl<K, V> HashMap<K, V, RandomState> {
588 pub fn new() -> HashMap<K, V, RandomState> { }
595 fn new() fn() -> HashMap<K, V, RandomState>
601 fn dont_complete_attr() {
604 mod foo { pub struct Foo; }
613 fn completes_variant_through_self() {
636 fn completes_primitive_assoc_const() {
637 cov_mark::check!(completes_primitive_assoc_const);
640 //- /lib.rs crate:lib deps:core
645 //- /core.rs crate:core
648 pub const MAX: Self = 255;
654 ct MAX pub const MAX: Self;
661 fn completes_variant_through_alias() {
662 cov_mark::check!(completes_variant_through_alias);
680 fn respects_doc_hidden() {
681 cov_mark::check!(qualified_path_doc_hidden);
684 //- /lib.rs crate:lib deps:dep
689 //- /dep.rs crate:dep