1 //! Completion of names from the current scope, e.g. locals and imported items.
6 use hir::{Adt, ModPath, ModuleDef, ScopeDef, Type};
7 use ide_db::helpers::insert_use::ImportScope;
8 use ide_db::imports_locator;
13 render::{render_resolution_with_import, RenderContext},
14 CompletionContext, Completions, ImportEdit,
17 pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
18 if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) {
21 if ctx.record_lit_syntax.is_some()
22 || ctx.record_pat_syntax.is_some()
23 || ctx.attribute_under_caret.is_some()
24 || ctx.mod_declaration_under_caret.is_some()
29 if let Some(ty) = &ctx.expected_type {
30 complete_enum_variants(acc, ctx, ty);
33 if ctx.is_pat_binding_or_const {
37 ctx.scope.process_all_names(&mut |name, res| {
38 if ctx.use_item_syntax.is_some() {
39 if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
40 if name_ref.syntax().text() == name.to_string().as_str() {
41 mark::hit!(self_fulfilling_completion);
46 acc.add_resolution(ctx, name.to_string(), &res)
49 if ctx.config.enable_autoimport_completions {
50 fuzzy_completion(acc, ctx);
54 fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
55 if let Some(Adt::Enum(enum_data)) =
56 iter::successors(Some(ty.clone()), |ty| ty.remove_ref()).last().and_then(|ty| ty.as_adt())
58 let variants = enum_data.variants(ctx.db);
60 let module = if let Some(module) = ctx.scope.module() {
61 // Compute path from the completion site if available.
64 // Otherwise fall back to the enum's definition site.
65 enum_data.module(ctx.db)
68 for variant in variants {
69 if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
70 // Variants with trivial paths are already added by the existing completion logic,
71 // so we should avoid adding these twice
72 if path.segments.len() > 1 {
73 acc.add_qualified_enum_variant(ctx, variant, path);
80 // Feature: Fuzzy Completion and Autoimports
82 // When completing names in the current scope, proposes additional imports from other modules or crates,
83 // if they can be qualified in the scope and their name contains all symbols from the completion input
84 // (case-insensitive, in any order or places).
90 // # pub mod std { pub mod marker { pub struct PhantomData { } } }
94 // use std::marker::PhantomData;
99 // # pub mod std { pub mod marker { pub struct PhantomData { } } }
102 // .Fuzzy search details
104 // To avoid an excessive amount of the results returned, completion input is checked for inclusion in the names only
105 // (i.e. in `HashMap` in the `std::collections::HashMap` path).
106 // For the same reasons, avoids searching for any imports for inputs with their length less that 2 symbols.
110 // It is possible to configure how use-trees are merged with the `importMergeBehavior` setting.
111 // Mimics the corresponding behavior of the `Auto Import` feature.
113 // .LSP and performance implications
115 // The feature is enabled only if the LSP client supports LSP protocol version 3.16+ and reports the `additionalTextEdits`
116 // (case sensitive) resolve client capability in its client capabilities.
117 // This way the server is able to defer the costly computations, doing them for a selected completion item only.
118 // For clients with no such support, all edits have to be calculated on the completion request, including the fuzzy search completion ones,
119 // which might be slow ergo the feature is automatically disabled.
123 // The feature can be forcefully turned off in the settings with the `rust-analyzer.completion.enableAutoimportCompletions` flag.
124 // Note that having this flag set to `true` does not guarantee that the feature is enabled: your client needs to have the corredponding
125 // capability enabled.
126 fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
127 let potential_import_name = ctx.token.to_string();
128 let _p = profile::span("fuzzy_completion").detail(|| potential_import_name.clone());
130 if potential_import_name.len() < 2 {
134 let current_module = ctx.scope.module()?;
135 let anchor = ctx.name_ref_syntax.as_ref()?;
136 let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
138 let user_input_lowercased = potential_import_name.to_lowercase();
139 let mut all_mod_paths = imports_locator::find_similar_imports(
143 potential_import_name,
147 .filter_map(|import_candidate| {
148 Some(match import_candidate {
149 Either::Left(module_def) => {
150 (current_module.find_use_path(ctx.db, module_def)?, ScopeDef::ModuleDef(module_def))
152 Either::Right(macro_def) => {
153 (current_module.find_use_path(ctx.db, macro_def)?, ScopeDef::MacroDef(macro_def))
157 .filter(|(mod_path, _)| mod_path.len() > 1)
158 .collect::<Vec<_>>();
160 all_mod_paths.sort_by_cached_key(|(mod_path, _)| {
161 compute_fuzzy_completion_order_key(mod_path, &user_input_lowercased)
164 acc.add_all(all_mod_paths.into_iter().filter_map(|(import_path, definition)| {
165 render_resolution_with_import(
166 RenderContext::new(ctx),
167 ImportEdit { import_path, import_scope: import_scope.clone() },
174 fn compute_fuzzy_completion_order_key(
175 proposed_mod_path: &ModPath,
176 user_input_lowercased: &str,
178 mark::hit!(certain_fuzzy_order_test);
179 let proposed_import_name = match proposed_mod_path.segments.last() {
180 Some(name) => name.to_string().to_lowercase(),
181 None => return usize::MAX,
183 match proposed_import_name.match_indices(user_input_lowercased).next() {
184 Some((first_matching_index, _)) => first_matching_index,
191 use expect_test::{expect, Expect};
192 use test_utils::mark;
195 test_utils::{check_edit, check_edit_with_config, completion_list_with_config},
196 CompletionConfig, CompletionKind,
199 fn check(ra_fixture: &str, expect: Expect) {
200 check_with_config(CompletionConfig::default(), ra_fixture, expect);
203 fn check_with_config(config: CompletionConfig, ra_fixture: &str, expect: Expect) {
204 let actual = completion_list_with_config(config, ra_fixture, CompletionKind::Reference);
205 expect.assert_eq(&actual)
208 fn fuzzy_completion_config() -> CompletionConfig {
209 CompletionConfig::default()
213 fn self_fulfilling_completion() {
214 mark::check!(self_fulfilling_completion);
218 use std::collections;
227 fn bind_pat_and_path_ignore_at() {
231 fn quux(x: Option<Enum>) {
234 Some(en<|> @ Enum::A) => (),
243 fn bind_pat_and_path_ignore_ref() {
247 fn quux(x: Option<Enum>) {
250 Some(ref en<|>) => (),
259 fn bind_pat_and_path() {
263 fn quux(x: Option<Enum>) {
277 fn completes_bindings_from_let() {
289 fn quux(…) fn quux(x: i32)
295 fn completes_bindings_from_if_let() {
299 if let Some(x) = foo() {
302 if let Some(a) = bar() {
317 fn completes_bindings_from_for() {
321 for x in &[1, 2, 3] { <|> }
332 fn completes_if_prefix_is_keyword() {
333 mark::check!(completes_if_prefix_is_keyword);
352 fn completes_generic_params() {
354 r#"fn quux<T>() { <|> }"#,
357 fn quux() fn quux<T>()
363 fn completes_generic_params_in_struct() {
365 r#"struct S<T> { x: <|>}"#,
375 fn completes_self_in_enum() {
377 r#"enum X { Y(<|>) }"#,
386 fn completes_module_items() {
401 /// Regression test for issue #6091.
403 fn correctly_completes_module_items_prefixed_with_underscore() {
422 fn completes_extern_prelude() {
425 //- /lib.rs crate:main deps:other_crate
428 //- /other_crate/lib.rs crate:other_crate
438 fn completes_module_items_in_nested_modules() {
455 fn completes_return_type() {
469 fn dont_show_both_completions_for_shadowing() {
480 // FIXME: should be only one bar here
490 fn completes_self_in_methods() {
492 r#"impl S { fn foo(&self) { <|> } }"#,
501 fn completes_prelude() {
504 //- /main.rs crate:main deps:std
505 fn foo() { let x: <|> }
507 //- /std/lib.rs crate:std
511 mod prelude { struct Option; }
522 fn completes_std_prelude_if_core_is_defined() {
525 //- /main.rs crate:main deps:core,std
526 fn foo() { let x: <|> }
528 //- /core/lib.rs crate:core
532 mod prelude { struct Option; }
534 //- /std/lib.rs crate:std deps:core
538 mod prelude { struct String; }
550 fn completes_macros_as_value() {
553 macro_rules! foo { () => {} }
557 macro_rules! bar { () => {} }
561 macro_rules! nope { () => {} }
564 macro_rules! baz { () => {} }
567 fn main() { let v = <|> }
571 ma baz!(…) #[macro_export]
575 ma bar!(…) macro_rules! bar
576 ma foo!(…) macro_rules! foo
582 fn completes_both_macro_and_value() {
585 macro_rules! foo { () => {} }
590 ma foo!(…) macro_rules! foo
596 fn completes_macros_as_type() {
599 macro_rules! foo { () => {} }
600 fn main() { let x: <|> }
604 ma foo!(…) macro_rules! foo
610 fn completes_macros_as_stmt() {
613 macro_rules! foo { () => {} }
618 ma foo!(…) macro_rules! foo
624 fn completes_local_item() {
633 fn frobnicate() fn frobnicate()
640 fn completes_in_simple_macro_1() {
643 macro_rules! m { ($e:expr) => { $e } }
652 fn quux(…) fn quux(x: i32)
653 ma m!(…) macro_rules! m
659 fn completes_in_simple_macro_2() {
662 macro_rules! m { ($e:expr) => { $e } }
671 fn quux(…) fn quux(x: i32)
672 ma m!(…) macro_rules! m
678 fn completes_in_simple_macro_without_closing_parens() {
681 macro_rules! m { ($e:expr) => { $e } }
690 fn quux(…) fn quux(x: i32)
691 ma m!(…) macro_rules! m
697 fn completes_unresolved_uses() {
712 fn completes_enum_variant_matcharm() {
715 enum Foo { Bar, Baz, Quux }
732 fn completes_enum_variant_matcharm_ref() {
735 enum Foo { Bar, Baz, Quux }
752 fn completes_enum_variant_iflet() {
755 enum Foo { Bar, Baz, Quux }
759 if let Qu<|> = foo { }
772 fn completes_enum_variant_basic_expr() {
775 enum Foo { Bar, Baz, Quux }
776 fn main() { let foo: Foo = Q<|> }
789 fn completes_enum_variant_from_module() {
792 mod m { pub enum E { V } }
793 fn f() -> m::E { V<|> }
798 fn f() fn f() -> m::E
804 fn dont_complete_attr() {
816 fn completes_type_or_trait_in_impl_block() {
833 fn function_fuzzy_completion() {
834 check_edit_with_config(
835 fuzzy_completion_config(),
838 //- /lib.rs crate:dep
843 //- /main.rs crate:main deps:dep
859 fn macro_fuzzy_completion() {
860 check_edit_with_config(
861 fuzzy_completion_config(),
862 "macro_with_curlies!",
864 //- /lib.rs crate:dep
865 /// Please call me as macro_with_curlies! {}
867 macro_rules! macro_with_curlies {
871 //- /main.rs crate:main deps:dep
877 use dep::macro_with_curlies;
880 macro_with_curlies! {$0}
887 fn struct_fuzzy_completion() {
888 check_edit_with_config(
889 fuzzy_completion_config(),
892 //- /lib.rs crate:dep
893 pub struct FirstStruct;
894 pub mod some_module {
895 pub struct SecondStruct;
896 pub struct ThirdStruct;
899 //- /main.rs crate:main deps:dep
900 use dep::{FirstStruct, some_module::SecondStruct};
907 use dep::{FirstStruct, some_module::{SecondStruct, ThirdStruct}};
917 fn fuzzy_completions_come_in_specific_order() {
918 mark::check!(certain_fuzzy_order_test);
920 fuzzy_completion_config(),
922 //- /lib.rs crate:dep
923 pub struct FirstStruct;
924 pub mod some_module {
925 // already imported, omitted
926 pub struct SecondStruct;
927 // does not contain all letters from the query, omitted
928 pub struct UnrelatedOne;
929 // contains all letters from the query, but not in sequence, displayed last
930 pub struct ThiiiiiirdStruct;
931 // contains all letters from the query, but not in the beginning, displayed second
932 pub struct AfterThirdStruct;
933 // contains all letters from the query in the begginning, displayed first
934 pub struct ThirdStruct;
937 //- /main.rs crate:main deps:dep
938 use dep::{FirstStruct, some_module::SecondStruct};
949 st dep::some_module::ThirdStruct
950 st dep::some_module::AfterThirdStruct
951 st dep::some_module::ThiiiiiirdStruct