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()
16 || ctx.has_impl_or_trait_parent()
21 if let Some(hir::Adt::Enum(e)) =
22 ctx.expected_type.as_ref().and_then(|ty| ty.strip_references().as_adt())
24 super::complete_enum_variants(acc, ctx, e, |acc, ctx, variant, path| {
25 acc.add_qualified_enum_variant(ctx, variant, path)
29 ctx.scope.process_all_names(&mut |name, res| {
30 if let ScopeDef::GenericParam(hir::GenericParam::LifetimeParam(_)) = res {
31 cov_mark::hit!(skip_lifetime_completion);
34 if ctx.use_item_syntax.is_some() {
35 if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
36 if name_ref.syntax().text() == name.to_string().as_str() {
37 cov_mark::hit!(self_fulfilling_completion);
42 acc.add_resolution(ctx, name.to_string(), &res);
48 use expect_test::{expect, Expect};
51 test_utils::{check_edit, completion_list_with_config, TEST_CONFIG},
52 CompletionConfig, CompletionKind,
55 fn check(ra_fixture: &str, expect: Expect) {
56 check_with_config(TEST_CONFIG, ra_fixture, expect);
59 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
60 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
61 expect.assert_eq(&actual)
65 fn self_fulfilling_completion() {
66 cov_mark::check!(self_fulfilling_completion);
79 fn bind_pat_and_path_ignore_at() {
83 fn quux(x: Option<Enum>) {
86 Some(en$0 @ Enum::A) => (),
95 fn bind_pat_and_path_ignore_ref() {
99 fn quux(x: Option<Enum>) {
102 Some(ref en$0) => (),
111 fn bind_pat_and_path() {
115 fn quux(x: Option<Enum>) {
129 fn completes_bindings_from_let() {
147 fn completes_bindings_from_if_let() {
151 if let Some(x) = foo() {
154 if let Some(a) = bar() {
169 fn completes_bindings_from_for() {
173 for x in &[1, 2, 3] { $0 }
184 fn completes_if_prefix_is_keyword() {
185 cov_mark::check!(completes_if_prefix_is_keyword);
204 fn completes_generic_params() {
206 r#"fn quux<T>() { $0 }"#,
213 r#"fn quux<const C: usize>() { $0 }"#,
222 fn does_not_complete_lifetimes() {
223 cov_mark::check!(skip_lifetime_completion);
225 r#"fn quux<'a>() { $0 }"#,
233 fn completes_generic_params_in_struct() {
235 r#"struct S<T> { x: $0}"#,
245 fn completes_self_in_enum() {
247 r#"enum X { Y($0) }"#,
256 fn completes_module_items() {
271 /// Regression test for issue #6091.
273 fn correctly_completes_module_items_prefixed_with_underscore() {
292 fn completes_extern_prelude() {
295 //- /lib.rs crate:main deps:other_crate
298 //- /other_crate/lib.rs crate:other_crate
308 fn completes_module_items_in_nested_modules() {
325 fn completes_return_type() {
339 fn dont_show_both_completions_for_shadowing() {
350 // FIXME: should be only one bar here
360 fn completes_self_in_methods() {
362 r#"impl S { fn foo(&self) { $0 } }"#,
371 fn completes_prelude() {
374 //- /main.rs crate:main deps:std
375 fn foo() { let x: $0 }
377 //- /std/lib.rs crate:std
381 mod prelude { struct Option; }
392 fn completes_prelude_macros() {
395 //- /main.rs crate:main deps:std
398 //- /std/lib.rs crate:std
404 pub use crate::concat;
408 #[rustc_builtin_macro]
410 macro_rules! concat { }
415 ma concat!(…) #[macro_export] macro_rules! concat
422 fn completes_std_prelude_if_core_is_defined() {
425 //- /main.rs crate:main deps:core,std
426 fn foo() { let x: $0 }
428 //- /core/lib.rs crate:core
432 mod prelude { struct Option; }
434 //- /std/lib.rs crate:std deps:core
438 mod prelude { struct String; }
450 fn completes_macros_as_value() {
453 macro_rules! foo { () => {} }
457 macro_rules! bar { () => {} }
461 macro_rules! nope { () => {} }
464 macro_rules! baz { () => {} }
467 fn main() { let v = $0 }
471 ma baz!(…) #[macro_export] macro_rules! baz
474 ma bar!(…) macro_rules! bar
475 ma foo!(…) macro_rules! foo
481 fn completes_both_macro_and_value() {
484 macro_rules! foo { () => {} }
489 ma foo!(…) macro_rules! foo
495 fn completes_macros_as_type() {
498 macro_rules! foo { () => {} }
499 fn main() { let x: $0 }
503 ma foo!(…) macro_rules! foo
509 fn completes_macros_as_stmt() {
512 macro_rules! foo { () => {} }
517 ma foo!(…) macro_rules! foo
523 fn completes_local_item() {
539 fn completes_in_simple_macro_1() {
542 macro_rules! m { ($e:expr) => { $e } }
552 ma m!(…) macro_rules! m
558 fn completes_in_simple_macro_2() {
561 macro_rules! m { ($e:expr) => { $e } }
571 ma m!(…) macro_rules! m
577 fn completes_in_simple_macro_without_closing_parens() {
580 macro_rules! m { ($e:expr) => { $e } }
590 ma m!(…) macro_rules! m
596 fn completes_unresolved_uses() {
611 fn completes_enum_variant_basic_expr() {
614 enum Foo { Bar, Baz, Quux }
615 fn main() { let foo: Foo = Q$0 }
628 fn completes_enum_variant_from_module() {
631 mod m { pub enum E { V } }
632 fn f() -> m::E { V$0 }
643 fn dont_complete_attr() {
655 fn completes_type_or_trait_in_impl_block() {