1 //! Completion of names from the current scope, e.g. locals and imported items.
6 use crate::{CompletionContext, Completions};
8 pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
9 if !ctx.is_trivial_path {
12 if ctx.record_lit_syntax.is_some()
13 || ctx.record_pat_syntax.is_some()
14 || ctx.attribute_under_caret.is_some()
15 || ctx.mod_declaration_under_caret.is_some()
20 if let Some(ty) = &ctx.expected_type {
21 super::complete_enum_variants(acc, ctx, ty, |acc, ctx, variant, path| {
22 acc.add_qualified_enum_variant(ctx, variant, path)
26 ctx.scope.process_all_names(&mut |name, res| {
27 if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
28 cov_mark::hit!(skip_lifetime_completion);
31 if ctx.use_item_syntax.is_some() {
32 if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
33 if name_ref.syntax().text() == name.to_string().as_str() {
34 cov_mark::hit!(self_fulfilling_completion);
39 acc.add_resolution(ctx, name.to_string(), &res);
45 use expect_test::{expect, Expect};
48 test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
49 CompletionConfig, CompletionKind,
52 fn check(ra_fixture: &str, expect: Expect) {
53 check_with_config(TEST_CONFIG, ra_fixture, expect);
56 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
57 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
58 expect.assert_eq(&actual)
62 fn self_fulfilling_completion() {
63 cov_mark::check!(self_fulfilling_completion);
76 fn bind_pat_and_path_ignore_at() {
80 fn quux(x: Option<Enum>) {
83 Some(en$0 @ Enum::A) => (),
92 fn bind_pat_and_path_ignore_ref() {
96 fn quux(x: Option<Enum>) {
108 fn bind_pat_and_path() {
112 fn quux(x: Option<Enum>) {
126 fn completes_bindings_from_let() {
144 fn completes_bindings_from_if_let() {
148 if let Some(x) = foo() {
151 if let Some(a) = bar() {
166 fn completes_bindings_from_for() {
170 for x in &[1, 2, 3] { $0 }
181 fn completes_if_prefix_is_keyword() {
182 cov_mark::check!(completes_if_prefix_is_keyword);
201 fn completes_generic_params() {
203 r#"fn quux<T>() { $0 }"#,
210 r#"fn quux<const C: usize>() { $0 }"#,
219 fn does_not_complete_lifetimes() {
220 cov_mark::check!(skip_lifetime_completion);
222 r#"fn quux<'a>() { $0 }"#,
230 fn completes_generic_params_in_struct() {
232 r#"struct S<T> { x: $0}"#,
242 fn completes_self_in_enum() {
244 r#"enum X { Y($0) }"#,
253 fn completes_module_items() {
268 /// Regression test for issue #6091.
270 fn correctly_completes_module_items_prefixed_with_underscore() {
289 fn completes_extern_prelude() {
292 //- /lib.rs crate:main deps:other_crate
295 //- /other_crate/lib.rs crate:other_crate
305 fn completes_module_items_in_nested_modules() {
322 fn completes_return_type() {
336 fn dont_show_both_completions_for_shadowing() {
347 // FIXME: should be only one bar here
357 fn completes_self_in_methods() {
359 r#"impl S { fn foo(&self) { $0 } }"#,
368 fn completes_prelude() {
371 //- /main.rs crate:main deps:std
372 fn foo() { let x: $0 }
374 //- /std/lib.rs crate:std
378 mod prelude { struct Option; }
389 fn completes_prelude_macros() {
392 //- /main.rs crate:main deps:std
395 //- /std/lib.rs crate:std
401 pub use crate::concat;
405 #[rustc_builtin_macro]
407 macro_rules! concat { }
412 ma concat!(…) #[macro_export] macro_rules! concat
419 fn completes_std_prelude_if_core_is_defined() {
422 //- /main.rs crate:main deps:core,std
423 fn foo() { let x: $0 }
425 //- /core/lib.rs crate:core
429 mod prelude { struct Option; }
431 //- /std/lib.rs crate:std deps:core
435 mod prelude { struct String; }
447 fn completes_macros_as_value() {
450 macro_rules! foo { () => {} }
454 macro_rules! bar { () => {} }
458 macro_rules! nope { () => {} }
461 macro_rules! baz { () => {} }
464 fn main() { let v = $0 }
468 ma baz!(…) #[macro_export] macro_rules! baz
471 ma bar!(…) macro_rules! bar
472 ma foo!(…) macro_rules! foo
478 fn completes_both_macro_and_value() {
481 macro_rules! foo { () => {} }
486 ma foo!(…) macro_rules! foo
492 fn completes_macros_as_type() {
495 macro_rules! foo { () => {} }
496 fn main() { let x: $0 }
500 ma foo!(…) macro_rules! foo
506 fn completes_macros_as_stmt() {
509 macro_rules! foo { () => {} }
514 ma foo!(…) macro_rules! foo
520 fn completes_local_item() {
529 fn frobnicate() -> ()
536 fn completes_in_simple_macro_1() {
539 macro_rules! m { ($e:expr) => { $e } }
549 ma m!(…) macro_rules! m
555 fn completes_in_simple_macro_2() {
558 macro_rules! m { ($e:expr) => { $e } }
568 ma m!(…) macro_rules! m
574 fn completes_in_simple_macro_without_closing_parens() {
577 macro_rules! m { ($e:expr) => { $e } }
587 ma m!(…) macro_rules! m
593 fn completes_unresolved_uses() {
608 fn completes_enum_variant_basic_expr() {
611 enum Foo { Bar, Baz, Quux }
612 fn main() { let foo: Foo = Q$0 }
625 fn completes_enum_variant_from_module() {
628 mod m { pub enum E { V } }
629 fn f() -> m::E { V$0 }
640 fn dont_complete_attr() {
652 fn completes_type_or_trait_in_impl_block() {