Show case-insensitive exact matches instead of fuzzy flyimport for short paths

This commit is contained in:
Lukas Wirth 2021-12-09 19:18:11 +01:00
parent c469f8abcb
commit 143a30aa51
6 changed files with 55 additions and 33 deletions

View file

@ -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 @@ impl ImportAssets {
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 @@ impl ImportAssets {
self.search_for(sema, None)
}
pub fn path_fuzzy_name_to_exact(&mut self, case_sensitive: bool) {
if let ImportCandidate::Path(PathImportCandidate { name: to_import, .. }) =
&mut self.import_candidate
{
let name = match to_import {
NameToImport::Fuzzy(name) => std::mem::take(name),
_ => return,
};
*to_import = NameToImport::Exact(name, case_sensitive);
}
}
fn search_for(
&self,
sema: &Semantics<RootDatabase>,
@ -561,7 +579,9 @@ impl ImportCandidate {
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 @@ impl ImportCandidate {
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 @@ impl ImportCandidate {
}
Some(ImportCandidate::Path(PathImportCandidate {
qualifier: None,
name: NameToImport::Exact(name.to_string()),
name: NameToImport::exact_case_sensitive(name.to_string()),
}))
}