Tweak the search query params for better lookup speed

This commit is contained in:
Kirill Bulatov 2020-12-08 14:38:43 +02:00
parent cbd3717f2c
commit bf24cb3e8d
2 changed files with 38 additions and 32 deletions

View file

@ -99,7 +99,6 @@ fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &T
// //
// To avoid an excessive amount of the results returned, completion input is checked for inclusion in the identifiers only // 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. // (i.e. in `HashMap` in the `std::collections::HashMap` path), also not in the module indentifiers.
// It also avoids searching for any imports for inputs with their length less that 3 symbols.
// //
// .Merge Behaviour // .Merge Behaviour
// //
@ -123,26 +122,25 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
let _p = profile::span("fuzzy_completion"); let _p = profile::span("fuzzy_completion");
let potential_import_name = ctx.token.to_string(); let potential_import_name = ctx.token.to_string();
if potential_import_name.len() < 3 {
return None;
}
let current_module = ctx.scope.module()?; let current_module = ctx.scope.module()?;
let anchor = ctx.name_ref_syntax.as_ref()?; let anchor = ctx.name_ref_syntax.as_ref()?;
let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?;
let possible_imports = let possible_imports = imports_locator::find_similar_imports(
imports_locator::find_similar_imports(&ctx.sema, ctx.krate?, &potential_import_name, true) &ctx.sema,
ctx.krate?,
Some(100),
&potential_import_name,
true,
)
.filter_map(|import_candidate| { .filter_map(|import_candidate| {
Some(match import_candidate { Some(match import_candidate {
Either::Left(module_def) => ( Either::Left(module_def) => {
current_module.find_use_path(ctx.db, module_def)?, (current_module.find_use_path(ctx.db, module_def)?, ScopeDef::ModuleDef(module_def))
ScopeDef::ModuleDef(module_def), }
), Either::Right(macro_def) => {
Either::Right(macro_def) => ( (current_module.find_use_path(ctx.db, macro_def)?, ScopeDef::MacroDef(macro_def))
current_module.find_use_path(ctx.db, macro_def)?, }
ScopeDef::MacroDef(macro_def),
),
}) })
}) })
.filter(|(mod_path, _)| mod_path.len() > 1) .filter(|(mod_path, _)| mod_path.len() > 1)

View file

@ -34,6 +34,7 @@ pub fn find_exact_imports<'a>(
pub fn find_similar_imports<'a>( pub fn find_similar_imports<'a>(
sema: &Semantics<'a, RootDatabase>, sema: &Semantics<'a, RootDatabase>,
krate: Crate, krate: Crate,
limit: Option<usize>,
name_to_import: &str, name_to_import: &str,
ignore_modules: bool, ignore_modules: bool,
) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> { ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
@ -44,7 +45,14 @@ pub fn find_similar_imports<'a>(
external_query = external_query.exclude_import_kind(import_map::ImportKind::Module); external_query = external_query.exclude_import_kind(import_map::ImportKind::Module);
} }
find_imports(sema, krate, symbol_index::Query::new(name_to_import.to_string()), external_query) let mut local_query = symbol_index::Query::new(name_to_import.to_string());
if let Some(limit) = limit {
local_query.limit(limit);
external_query = external_query.limit(limit);
}
find_imports(sema, krate, local_query, external_query)
} }
fn find_imports<'a>( fn find_imports<'a>(