diff options
Diffstat (limited to 'crates/completion/src/completions/unqualified_path.rs')
-rw-r--r-- | crates/completion/src/completions/unqualified_path.rs | 42 |
1 files changed, 36 insertions, 6 deletions
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs index 099ffb4d4..81a6d00e2 100644 --- a/crates/completion/src/completions/unqualified_path.rs +++ b/crates/completion/src/completions/unqualified_path.rs | |||
@@ -1,5 +1,7 @@ | |||
1 | //! Completion of names from the current scope, e.g. locals and imported items. | 1 | //! Completion of names from the current scope, e.g. locals and imported items. |
2 | 2 | ||
3 | use std::iter; | ||
4 | |||
3 | use either::Either; | 5 | use either::Either; |
4 | use hir::{Adt, ModPath, ModuleDef, ScopeDef, Type}; | 6 | use hir::{Adt, ModPath, ModuleDef, ScopeDef, Type}; |
5 | use ide_db::helpers::insert_use::ImportScope; | 7 | use ide_db::helpers::insert_use::ImportScope; |
@@ -50,7 +52,9 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC | |||
50 | } | 52 | } |
51 | 53 | ||
52 | fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) { | 54 | fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) { |
53 | if let Some(Adt::Enum(enum_data)) = ty.as_adt() { | 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()) | ||
57 | { | ||
54 | let variants = enum_data.variants(ctx.db); | 58 | let variants = enum_data.variants(ctx.db); |
55 | 59 | ||
56 | let module = if let Some(module) = ctx.scope.module() { | 60 | let module = if let Some(module) = ctx.scope.module() { |
@@ -97,8 +101,9 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &T | |||
97 | // | 101 | // |
98 | // .Fuzzy search details | 102 | // .Fuzzy search details |
99 | // | 103 | // |
100 | // To avoid an excessive amount of the results returned, completion input is checked for inclusion in the identifiers only | 104 | // To avoid an excessive amount of the results returned, completion input is checked for inclusion in the names only |
101 | // (i.e. in `HashMap` in the `std::collections::HashMap` path), also not in the module indentifiers. | 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. | ||
102 | // | 107 | // |
103 | // .Merge Behavior | 108 | // .Merge Behavior |
104 | // | 109 | // |
@@ -122,15 +127,20 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<() | |||
122 | let _p = profile::span("fuzzy_completion"); | 127 | let _p = profile::span("fuzzy_completion"); |
123 | let potential_import_name = ctx.token.to_string(); | 128 | let potential_import_name = ctx.token.to_string(); |
124 | 129 | ||
130 | if potential_import_name.len() < 2 { | ||
131 | return None; | ||
132 | } | ||
133 | |||
125 | let current_module = ctx.scope.module()?; | 134 | let current_module = ctx.scope.module()?; |
126 | let anchor = ctx.name_ref_syntax.as_ref()?; | 135 | let anchor = ctx.name_ref_syntax.as_ref()?; |
127 | let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; | 136 | let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; |
128 | 137 | ||
138 | let user_input_lowercased = potential_import_name.to_lowercase(); | ||
129 | let mut all_mod_paths = imports_locator::find_similar_imports( | 139 | let mut all_mod_paths = imports_locator::find_similar_imports( |
130 | &ctx.sema, | 140 | &ctx.sema, |
131 | ctx.krate?, | 141 | ctx.krate?, |
132 | Some(100), | 142 | Some(40), |
133 | &potential_import_name, | 143 | potential_import_name, |
134 | true, | 144 | true, |
135 | ) | 145 | ) |
136 | .filter_map(|import_candidate| { | 146 | .filter_map(|import_candidate| { |
@@ -146,7 +156,6 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<() | |||
146 | .filter(|(mod_path, _)| mod_path.len() > 1) | 156 | .filter(|(mod_path, _)| mod_path.len() > 1) |
147 | .collect::<Vec<_>>(); | 157 | .collect::<Vec<_>>(); |
148 | 158 | ||
149 | let user_input_lowercased = potential_import_name.to_lowercase(); | ||
150 | all_mod_paths.sort_by_cached_key(|(mod_path, _)| { | 159 | all_mod_paths.sort_by_cached_key(|(mod_path, _)| { |
151 | compute_fuzzy_completion_order_key(mod_path, &user_input_lowercased) | 160 | compute_fuzzy_completion_order_key(mod_path, &user_input_lowercased) |
152 | }); | 161 | }); |
@@ -701,6 +710,7 @@ fn main() { <|> } | |||
701 | "#]], | 710 | "#]], |
702 | ); | 711 | ); |
703 | } | 712 | } |
713 | |||
704 | #[test] | 714 | #[test] |
705 | fn completes_enum_variant_matcharm() { | 715 | fn completes_enum_variant_matcharm() { |
706 | check( | 716 | check( |
@@ -722,6 +732,26 @@ fn main() { | |||
722 | } | 732 | } |
723 | 733 | ||
724 | #[test] | 734 | #[test] |
735 | fn completes_enum_variant_matcharm_ref() { | ||
736 | check( | ||
737 | r#" | ||
738 | enum Foo { Bar, Baz, Quux } | ||
739 | |||
740 | fn main() { | ||
741 | let foo = Foo::Quux; | ||
742 | match &foo { Qu<|> } | ||
743 | } | ||
744 | "#, | ||
745 | expect![[r#" | ||
746 | ev Foo::Bar () | ||
747 | ev Foo::Baz () | ||
748 | ev Foo::Quux () | ||
749 | en Foo | ||
750 | "#]], | ||
751 | ) | ||
752 | } | ||
753 | |||
754 | #[test] | ||
725 | fn completes_enum_variant_iflet() { | 755 | fn completes_enum_variant_iflet() { |
726 | check( | 756 | check( |
727 | r#" | 757 | r#" |