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(hir::Adt::Enum(e)) =
21 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
23 super::complete_enum_variants(acc, ctx, e, |acc, ctx, variant, path| {
24 acc.add_qualified_enum_variant(ctx, variant, path)
28 ctx.scope.process_all_names(&mut |name, res| {
29 if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
30 cov_mark::hit!(skip_lifetime_completion);
33 if ctx.use_item_syntax.is_some() {
34 if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
35 if name_ref.syntax().text() == name.to_string().as_str() {
36 cov_mark::hit!(self_fulfilling_completion);
41 acc.add_resolution(ctx, name.to_string(), &res);
47 use expect_test::{expect, Expect};
50 test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
51 CompletionConfig, CompletionKind,
54 fn check(ra_fixture: &str, expect: Expect) {
55 check_with_config(TEST_CONFIG, ra_fixture, expect);
58 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
59 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
60 expect.assert_eq(&actual)
64 fn self_fulfilling_completion() {
65 cov_mark::check!(self_fulfilling_completion);
78 fn bind_pat_and_path_ignore_at() {
82 fn quux(x: Option<Enum>) {
85 Some(en$0 @ Enum::A) => (),
94 fn bind_pat_and_path_ignore_ref() {
98 fn quux(x: Option<Enum>) {
101 Some(ref en$0) => (),
110 fn bind_pat_and_path() {
114 fn quux(x: Option<Enum>) {
128 fn completes_bindings_from_let() {
146 fn completes_bindings_from_if_let() {
150 if let Some(x) = foo() {
153 if let Some(a) = bar() {
168 fn completes_bindings_from_for() {
172 for x in &[1, 2, 3] { $0 }
183 fn completes_if_prefix_is_keyword() {
184 cov_mark::check!(completes_if_prefix_is_keyword);
203 fn completes_generic_params() {
205 r#"fn quux<T>() { $0 }"#,
212 r#"fn quux<const C: usize>() { $0 }"#,
221 fn does_not_complete_lifetimes() {
222 cov_mark::check!(skip_lifetime_completion);
224 r#"fn quux<'a>() { $0 }"#,
232 fn completes_generic_params_in_struct() {
234 r#"struct S<T> { x: $0}"#,
244 fn completes_self_in_enum() {
246 r#"enum X { Y($0) }"#,
255 fn completes_module_items() {
270 /// Regression test for issue #6091.
272 fn correctly_completes_module_items_prefixed_with_underscore() {
291 fn completes_extern_prelude() {
294 //- /lib.rs crate:main deps:other_crate
297 //- /other_crate/lib.rs crate:other_crate
307 fn completes_module_items_in_nested_modules() {
324 fn completes_return_type() {
338 fn dont_show_both_completions_for_shadowing() {
349 // FIXME: should be only one bar here
359 fn completes_self_in_methods() {
361 r#"impl S { fn foo(&self) { $0 } }"#,
370 fn completes_prelude() {
373 //- /main.rs crate:main deps:std
374 fn foo() { let x: $0 }
376 //- /std/lib.rs crate:std
380 mod prelude { struct Option; }
391 fn completes_prelude_macros() {
394 //- /main.rs crate:main deps:std
397 //- /std/lib.rs crate:std
403 pub use crate::concat;
407 #[rustc_builtin_macro]
409 macro_rules! concat { }
414 ma concat!(…) #[macro_export] macro_rules! concat
421 fn completes_std_prelude_if_core_is_defined() {
424 //- /main.rs crate:main deps:core,std
425 fn foo() { let x: $0 }
427 //- /core/lib.rs crate:core
431 mod prelude { struct Option; }
433 //- /std/lib.rs crate:std deps:core
437 mod prelude { struct String; }
449 fn completes_macros_as_value() {
452 macro_rules! foo { () => {} }
456 macro_rules! bar { () => {} }
460 macro_rules! nope { () => {} }
463 macro_rules! baz { () => {} }
466 fn main() { let v = $0 }
470 ma baz!(…) #[macro_export] macro_rules! baz
473 ma bar!(…) macro_rules! bar
474 ma foo!(…) macro_rules! foo
480 fn completes_both_macro_and_value() {
483 macro_rules! foo { () => {} }
488 ma foo!(…) macro_rules! foo
494 fn completes_macros_as_type() {
497 macro_rules! foo { () => {} }
498 fn main() { let x: $0 }
502 ma foo!(…) macro_rules! foo
508 fn completes_macros_as_stmt() {
511 macro_rules! foo { () => {} }
516 ma foo!(…) macro_rules! foo
522 fn completes_local_item() {
538 fn completes_in_simple_macro_1() {
541 macro_rules! m { ($e:expr) => { $e } }
551 ma m!(…) macro_rules! m
557 fn completes_in_simple_macro_2() {
560 macro_rules! m { ($e:expr) => { $e } }
570 ma m!(…) macro_rules! m
576 fn completes_in_simple_macro_without_closing_parens() {
579 macro_rules! m { ($e:expr) => { $e } }
589 ma m!(…) macro_rules! m
595 fn completes_unresolved_uses() {
610 fn completes_enum_variant_basic_expr() {
613 enum Foo { Bar, Baz, Quux }
614 fn main() { let foo: Foo = Q$0 }
627 fn completes_enum_variant_from_module() {
630 mod m { pub enum E { V } }
631 fn f() -> m::E { V$0 }
642 fn dont_complete_attr() {
654 fn completes_type_or_trait_in_impl_block() {