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 || ctx.is_pat_binding_or_const) {
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 if ctx.is_pat_binding_or_const {
30 ctx.scope.process_all_names(&mut |name, res| {
31 if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
32 cov_mark::hit!(skip_lifetime_completion);
35 if ctx.use_item_syntax.is_some() {
36 if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
37 if name_ref.syntax().text() == name.to_string().as_str() {
38 cov_mark::hit!(self_fulfilling_completion);
43 acc.add_resolution(ctx, name.to_string(), &res);
49 use expect_test::{expect, Expect};
52 test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
53 CompletionConfig, CompletionKind,
56 fn check(ra_fixture: &str, expect: Expect) {
57 check_with_config(TEST_CONFIG, ra_fixture, expect);
60 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
61 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
62 expect.assert_eq(&actual)
66 fn self_fulfilling_completion() {
67 cov_mark::check!(self_fulfilling_completion);
80 fn bind_pat_and_path_ignore_at() {
84 fn quux(x: Option<Enum>) {
87 Some(en$0 @ Enum::A) => (),
96 fn bind_pat_and_path_ignore_ref() {
100 fn quux(x: Option<Enum>) {
103 Some(ref en$0) => (),
112 fn bind_pat_and_path() {
116 fn quux(x: Option<Enum>) {
130 fn completes_bindings_from_let() {
148 fn completes_bindings_from_if_let() {
152 if let Some(x) = foo() {
155 if let Some(a) = bar() {
170 fn completes_bindings_from_for() {
174 for x in &[1, 2, 3] { $0 }
185 fn completes_if_prefix_is_keyword() {
186 cov_mark::check!(completes_if_prefix_is_keyword);
205 fn completes_generic_params() {
207 r#"fn quux<T>() { $0 }"#,
214 r#"fn quux<const C: usize>() { $0 }"#,
223 fn does_not_complete_lifetimes() {
224 cov_mark::check!(skip_lifetime_completion);
226 r#"fn quux<'a>() { $0 }"#,
234 fn completes_generic_params_in_struct() {
236 r#"struct S<T> { x: $0}"#,
246 fn completes_self_in_enum() {
248 r#"enum X { Y($0) }"#,
257 fn completes_module_items() {
272 /// Regression test for issue #6091.
274 fn correctly_completes_module_items_prefixed_with_underscore() {
293 fn completes_extern_prelude() {
296 //- /lib.rs crate:main deps:other_crate
299 //- /other_crate/lib.rs crate:other_crate
309 fn completes_module_items_in_nested_modules() {
326 fn completes_return_type() {
340 fn dont_show_both_completions_for_shadowing() {
351 // FIXME: should be only one bar here
361 fn completes_self_in_methods() {
363 r#"impl S { fn foo(&self) { $0 } }"#,
372 fn completes_prelude() {
375 //- /main.rs crate:main deps:std
376 fn foo() { let x: $0 }
378 //- /std/lib.rs crate:std
382 mod prelude { struct Option; }
393 fn completes_prelude_macros() {
396 //- /main.rs crate:main deps:std
399 //- /std/lib.rs crate:std
405 pub use crate::concat;
409 #[rustc_builtin_macro]
411 macro_rules! concat { }
416 ma concat!(…) #[macro_export] macro_rules! concat
423 fn completes_std_prelude_if_core_is_defined() {
426 //- /main.rs crate:main deps:core,std
427 fn foo() { let x: $0 }
429 //- /core/lib.rs crate:core
433 mod prelude { struct Option; }
435 //- /std/lib.rs crate:std deps:core
439 mod prelude { struct String; }
451 fn completes_macros_as_value() {
454 macro_rules! foo { () => {} }
458 macro_rules! bar { () => {} }
462 macro_rules! nope { () => {} }
465 macro_rules! baz { () => {} }
468 fn main() { let v = $0 }
472 ma baz!(…) #[macro_export] macro_rules! baz
475 ma bar!(…) macro_rules! bar
476 ma foo!(…) macro_rules! foo
482 fn completes_both_macro_and_value() {
485 macro_rules! foo { () => {} }
490 ma foo!(…) macro_rules! foo
496 fn completes_macros_as_type() {
499 macro_rules! foo { () => {} }
500 fn main() { let x: $0 }
504 ma foo!(…) macro_rules! foo
510 fn completes_macros_as_stmt() {
513 macro_rules! foo { () => {} }
518 ma foo!(…) macro_rules! foo
524 fn completes_local_item() {
540 fn completes_in_simple_macro_1() {
543 macro_rules! m { ($e:expr) => { $e } }
553 ma m!(…) macro_rules! m
559 fn completes_in_simple_macro_2() {
562 macro_rules! m { ($e:expr) => { $e } }
572 ma m!(…) macro_rules! m
578 fn completes_in_simple_macro_without_closing_parens() {
581 macro_rules! m { ($e:expr) => { $e } }
591 ma m!(…) macro_rules! m
597 fn completes_unresolved_uses() {
612 fn completes_enum_variant_matcharm() {
615 enum Foo { Bar, Baz, Quux }
632 fn completes_enum_variant_matcharm_ref() {
635 enum Foo { Bar, Baz, Quux }
652 fn completes_enum_variant_iflet() {
655 enum Foo { Bar, Baz, Quux }
659 if let Qu$0 = foo { }
672 fn completes_enum_variant_basic_expr() {
675 enum Foo { Bar, Baz, Quux }
676 fn main() { let foo: Foo = Q$0 }
689 fn completes_enum_variant_from_module() {
692 mod m { pub enum E { V } }
693 fn f() -> m::E { V$0 }
704 fn completes_enum_variant_impl() {
707 enum Foo { Bar, Baz, Quux }
709 fn foo() { match Foo::Bar { Q$0 } }
726 fn dont_complete_attr() {
738 fn completes_type_or_trait_in_impl_block() {