1 //! Completion of names from the current scope, e.g. locals and imported items.
4 use syntax::{ast, AstNode};
6 use crate::{patterns::ImmediateLocation, CompletionContext, Completions};
8 pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
9 if ctx.is_path_disallowed() || !ctx.is_trivial_path() || ctx.has_impl_or_trait_prev_sibling() {
13 if ctx.in_use_tree() {
14 // only show modules in a fresh UseTree
15 cov_mark::hit!(unqualified_path_only_modules_in_import);
16 ctx.process_all_names(&mut |name, res| {
17 if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
18 acc.add_resolution(ctx, name, &res);
22 ["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
25 ["self", "super", "crate"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
27 match &ctx.completion_location {
28 Some(ImmediateLocation::Visibility(_)) => return,
29 Some(ImmediateLocation::ItemList | ImmediateLocation::Trait | ImmediateLocation::Impl) => {
30 // only show macros in {Assoc}ItemList
31 ctx.process_all_names(&mut |name, res| {
32 if let hir::ScopeDef::MacroDef(mac) = res {
34 acc.add_macro(ctx, Some(name.clone()), mac);
37 if let hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
38 acc.add_resolution(ctx, name, &res);
43 Some(ImmediateLocation::TypeBound) => {
44 ctx.process_all_names(&mut |name, res| {
45 let add_resolution = match res {
46 ScopeDef::MacroDef(mac) => mac.is_fn_like(),
47 ScopeDef::ModuleDef(hir::ModuleDef::Trait(_) | hir::ModuleDef::Module(_)) => {
53 acc.add_resolution(ctx, name, &res);
61 if !ctx.expects_type() {
62 if let Some(hir::Adt::Enum(e)) =
63 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
65 super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
66 acc.add_qualified_enum_variant(ctx, variant, path)
71 if let Some(ImmediateLocation::GenericArgList(arg_list)) = &ctx.completion_location {
72 if let Some(path_seg) = arg_list.syntax().parent().and_then(ast::PathSegment::cast) {
73 if let Some(hir::PathResolution::Def(hir::ModuleDef::Trait(trait_))) =
74 ctx.sema.resolve_path(&path_seg.parent_path())
76 trait_.items(ctx.sema.db).into_iter().for_each(|it| {
77 if let hir::AssocItem::TypeAlias(alias) = it {
78 acc.add_type_alias_with_eq(ctx, alias)
85 ctx.process_all_names(&mut |name, res| {
86 let add_resolution = match res {
87 ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) | ScopeDef::Label(_) => {
88 cov_mark::hit!(unqualified_skip_lifetime_completion);
91 ScopeDef::ImplSelfType(_) => {
92 !ctx.previous_token_is(syntax::T![impl]) && !ctx.previous_token_is(syntax::T![for])
94 // Don't suggest attribute macros and derives.
95 ScopeDef::MacroDef(mac) => mac.is_fn_like(),
96 // no values in type places
98 hir::ModuleDef::Function(_)
99 | hir::ModuleDef::Variant(_)
100 | hir::ModuleDef::Static(_),
102 | ScopeDef::Local(_) => !ctx.expects_type(),
103 // unless its a constant in a generic arg list position
104 ScopeDef::ModuleDef(hir::ModuleDef::Const(_))
105 | ScopeDef::GenericParam(hir::GenericParam::ConstParam(_)) => {
106 !ctx.expects_type() || ctx.expects_generic_arg()
111 acc.add_resolution(ctx, name, &res);
118 use expect_test::{expect, Expect};
120 use crate::tests::{check_edit, completion_list_no_kw};
122 fn check(ra_fixture: &str, expect: Expect) {
123 let actual = completion_list_no_kw(ra_fixture);
124 expect.assert_eq(&actual)
128 fn completes_if_prefix_is_keyword() {
146 /// Regression test for issue #6091.
148 fn correctly_completes_module_items_prefixed_with_underscore() {
167 fn completes_prelude() {
170 //- /main.rs crate:main deps:std
171 fn foo() { let x: $0 }
173 //- /std/lib.rs crate:std
189 fn completes_prelude_macros() {
192 //- /main.rs crate:main deps:std
195 //- /std/lib.rs crate:std
198 pub use crate::concat;
203 #[rustc_builtin_macro]
205 macro_rules! concat { }
210 ma concat!(…) #[macro_export] macro_rules! concat
218 fn completes_std_prelude_if_core_is_defined() {
221 //- /main.rs crate:main deps:core,std
222 fn foo() { let x: $0 }
224 //- /core/lib.rs crate:core
231 //- /std/lib.rs crate:std deps:core
248 fn respects_doc_hidden() {
251 //- /lib.rs crate:lib deps:std
256 //- /std.rs crate:std
259 macro_rules! format_args_nl {
276 fn respects_doc_hidden_in_assoc_item_list() {
279 //- /lib.rs crate:lib deps:std
285 //- /std.rs crate:std
288 macro_rules! format_args_nl {