From: Lukas Wirth Date: Thu, 9 Dec 2021 18:18:11 +0000 (+0100) Subject: Show case-insensitive exact matches instead of fuzzy flyimport for short paths X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=143a30aa519ecdb8b98bf6bf2c625f77920e7921;p=rust.git Show case-insensitive exact matches instead of fuzzy flyimport for short paths --- diff --git a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs index 3e33c62144e..46bf4d7b463 100644 --- a/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide_assists/src/handlers/replace_derive_with_manual_impl.rs @@ -65,7 +65,7 @@ pub(crate) fn replace_derive_with_manual_impl( let found_traits = items_locator::items_with_name( &ctx.sema, current_crate, - NameToImport::Exact(trait_path.segments().last()?.to_string()), + NameToImport::exact_case_sensitive(trait_path.segments().last()?.to_string()), items_locator::AssocItemSearch::Exclude, Some(items_locator::DEFAULT_QUERY_SEARCH_LIMIT.inner()), ) diff --git a/crates/ide_completion/src/completions/flyimport.rs b/crates/ide_completion/src/completions/flyimport.rs index 3a9c1b3beb7..8994c25475b 100644 --- a/crates/ide_completion/src/completions/flyimport.rs +++ b/crates/ide_completion/src/completions/flyimport.rs @@ -227,22 +227,18 @@ fn import_assets(ctx: &CompletionContext, fuzzy_name: String) -> Option i32 { } fn main() { - TE$0 + TES$0 }"#, expect![[r#" ct TEST_CONST (use foo::TEST_CONST) @@ -789,7 +793,7 @@ pub fn test_function() -> i32 { } fn main() { - te$0 + tes$0 }"#, expect![[r#" ct TEST_CONST (use foo::TEST_CONST) @@ -846,7 +850,7 @@ struct Foo { some_field: i32, } fn main() { - let _ = Foo { some_field: so$0 }; + let _ = Foo { some_field: som$0 }; } "#, expect![[r#" diff --git a/crates/ide_db/src/helpers/import_assets.rs b/crates/ide_db/src/helpers/import_assets.rs index 9a8adf167c8..024e0c9f81f 100644 --- a/crates/ide_db/src/helpers/import_assets.rs +++ b/crates/ide_db/src/helpers/import_assets.rs @@ -68,17 +68,23 @@ pub struct FirstSegmentUnresolved { /// A name that will be used during item lookups. #[derive(Debug, Clone)] pub enum NameToImport { - /// Requires items with names that exactly match the given string, case-sensitive. - Exact(String), + /// Requires items with names that exactly match the given string, bool indicatse case-sensitivity. + Exact(String, bool), /// Requires items with names that case-insensitively contain all letters from the string, /// in the same order, but not necessary adjacent. Fuzzy(String), } +impl NameToImport { + pub fn exact_case_sensitive(s: String) -> NameToImport { + NameToImport::Exact(s, true) + } +} + impl NameToImport { pub fn text(&self) -> &str { match self { - NameToImport::Exact(text) => text.as_str(), + NameToImport::Exact(text, _) => text.as_str(), NameToImport::Fuzzy(text) => text.as_str(), } } @@ -140,7 +146,7 @@ pub fn for_derive_ident(sema: &Semantics, ident: &ast::Ident) -> O if let Some(_) = path.qualifier() { return None; } - let name = NameToImport::Exact(path.segment()?.name_ref()?.to_string()); + let name = NameToImport::exact_case_sensitive(path.segment()?.name_ref()?.to_string()); let candidate_node = attr.syntax().clone(); Some(Self { import_candidate: ImportCandidate::Path(PathImportCandidate { qualifier: None, name }), @@ -230,6 +236,18 @@ pub fn search_for_relative_paths(&self, sema: &Semantics) -> Vec std::mem::take(name), + _ => return, + }; + *to_import = NameToImport::Exact(name, case_sensitive); + } + } + fn search_for( &self, sema: &Semantics, @@ -561,7 +579,9 @@ fn for_method_call( Some(_) => None, None => Some(Self::TraitMethod(TraitImportCandidate { receiver_ty: sema.type_of_expr(&method_call.receiver()?)?.adjusted(), - assoc_item_name: NameToImport::Exact(method_call.name_ref()?.to_string()), + assoc_item_name: NameToImport::exact_case_sensitive( + method_call.name_ref()?.to_string(), + ), })), } } @@ -573,7 +593,7 @@ fn for_regular_path(sema: &Semantics, path: &ast::Path) -> Option< path_import_candidate( sema, path.qualifier(), - NameToImport::Exact(path.segment()?.name_ref()?.to_string()), + NameToImport::exact_case_sensitive(path.segment()?.name_ref()?.to_string()), ) } @@ -587,7 +607,7 @@ fn for_name(sema: &Semantics, name: &ast::Name) -> Option { } Some(ImportCandidate::Path(PathImportCandidate { qualifier: None, - name: NameToImport::Exact(name.to_string()), + name: NameToImport::exact_case_sensitive(name.to_string()), })) } diff --git a/crates/ide_db/src/items_locator.rs b/crates/ide_db/src/items_locator.rs index e0dbe6caf0a..ca8266b6d24 100644 --- a/crates/ide_db/src/items_locator.rs +++ b/crates/ide_db/src/items_locator.rs @@ -50,16 +50,18 @@ pub fn items_with_name<'a>( }); let (mut local_query, mut external_query) = match name { - NameToImport::Exact(exact_name) => { + NameToImport::Exact(exact_name, case_sensitive) => { let mut local_query = symbol_index::Query::new(exact_name.clone()); local_query.exact(); let external_query = import_map::Query::new(exact_name) .name_only() - .search_mode(import_map::SearchMode::Equals) - .case_sensitive(); + .search_mode(import_map::SearchMode::Equals); - (local_query, external_query) + ( + local_query, + if case_sensitive { external_query.case_sensitive() } else { external_query }, + ) } NameToImport::Fuzzy(fuzzy_search_string) => { let mut local_query = symbol_index::Query::new(fuzzy_search_string.clone());