Add docs and optimisations

This commit is contained in:
Kirill Bulatov 2020-12-28 14:54:31 +02:00
parent c4995cfbd5
commit e4c3f753d2
3 changed files with 18 additions and 9 deletions

View File

@ -101,8 +101,8 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &T
//
// .Fuzzy search details
//
// To avoid an excessive amount of the results returned, completion input is checked for inclusion in the identifiers only
// (i.e. in `HashMap` in the `std::collections::HashMap` path), also not in the module indentifiers.
// To avoid an excessive amount of the results returned, completion input is checked for inclusion in the names only
// (i.e. in `HashMap` in the `std::collections::HashMap` path).
//
// .Merge Behavior
//

View File

@ -238,11 +238,15 @@ pub enum ImportKind {
BuiltinType,
}
/// todo kb
/// A way to match import map contents against the search query.
#[derive(Debug)]
pub enum SearchMode {
/// Import map entry should strictly match the query string.
Equals,
/// Import map entry should contain the query string.
Contains,
/// Import map entry should contain all letters from the query string,
/// in the same order, but not necessary adjacent.
Fuzzy,
}
@ -270,11 +274,14 @@ pub fn new(query: &str) -> Self {
}
}
/// Matches entries' names only, ignoring the rest of
/// the qualifier.
/// Example: for `std::marker::PhantomData`, the name is `PhantomData`.
pub fn name_only(self) -> Self {
Self { name_only: true, ..self }
}
/// todo kb
/// Specifies the way to search for the entries using the query.
pub fn search_mode(self, search_mode: SearchMode) -> Self {
Self { search_mode, ..self }
}
@ -296,7 +303,6 @@ pub fn exclude_import_kind(mut self, import_kind: ImportKind) -> Self {
}
}
// TODO kb: ugly with a special `return true` case and the `enforce_lowercase` one.
fn contains_query(query: &Query, input_path: &ImportPath, enforce_lowercase: bool) -> bool {
let mut input = if query.name_only {
input_path.segments.last().unwrap().to_string()
@ -378,7 +384,10 @@ pub fn search_dependencies<'a>(
Some(import_kind) => !query.exclude_import_kinds.contains(&import_kind),
None => true,
})
.filter(|item| contains_query(&query, &import_map.map[item].path, false));
.filter(|item| {
!query.case_sensitive // we've already checked the common importables path case-insensitively
|| contains_query(&query, &import_map.map[item].path, false)
});
res.extend(iter);
if res.len() >= query.limit {

View File

@ -39,18 +39,18 @@ pub fn find_similar_imports<'a>(
sema: &Semantics<'a, RootDatabase>,
krate: Crate,
limit: Option<usize>,
name_to_import: &str,
fuzzy_search_string: &str,
name_only: bool,
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
let _p = profile::span("find_similar_imports");
let mut external_query =
import_map::Query::new(name_to_import).search_mode(import_map::SearchMode::Fuzzy);
import_map::Query::new(fuzzy_search_string).search_mode(import_map::SearchMode::Fuzzy);
if name_only {
external_query = external_query.name_only();
}
let mut local_query = symbol_index::Query::new(name_to_import.to_string());
let mut local_query = symbol_index::Query::new(fuzzy_search_string.to_string());
if let Some(limit) = limit {
local_query.limit(limit);