mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-28 11:07:42 +00:00
Do not query item search by name eagerly
This commit is contained in:
parent
81961dc035
commit
a631108d2d
@ -1,5 +1,5 @@
|
|||||||
use hir::ModuleDef;
|
use hir::ModuleDef;
|
||||||
use ide_db::helpers::mod_path_to_ast;
|
use ide_db::helpers::{import_assets::NameToImport, mod_path_to_ast};
|
||||||
use ide_db::items_locator;
|
use ide_db::items_locator;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
@ -65,20 +65,25 @@ pub(crate) fn replace_derive_with_manual_impl(
|
|||||||
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
|
let current_module = ctx.sema.scope(annotated_name.syntax()).module()?;
|
||||||
let current_crate = current_module.krate();
|
let current_crate = current_module.krate();
|
||||||
|
|
||||||
let found_traits =
|
let found_traits = items_locator::locate_for_name(
|
||||||
items_locator::with_exact_name(&ctx.sema, current_crate, trait_token.text().to_string())
|
&ctx.sema,
|
||||||
.into_iter()
|
current_crate,
|
||||||
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
|
NameToImport::Exact(trait_token.text().to_string()),
|
||||||
ModuleDef::Trait(trait_) => Some(trait_),
|
items_locator::AssocItemSearch::Exclude,
|
||||||
_ => None,
|
None,
|
||||||
})
|
)
|
||||||
.flat_map(|trait_| {
|
.into_iter()
|
||||||
current_module
|
.filter_map(|item| match ModuleDef::from(item.as_module_def_id()?) {
|
||||||
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
|
ModuleDef::Trait(trait_) => Some(trait_),
|
||||||
.as_ref()
|
_ => None,
|
||||||
.map(mod_path_to_ast)
|
})
|
||||||
.zip(Some(trait_))
|
.flat_map(|trait_| {
|
||||||
});
|
current_module
|
||||||
|
.find_use_path(ctx.sema.db, hir::ModuleDef::Trait(trait_))
|
||||||
|
.as_ref()
|
||||||
|
.map(mod_path_to_ast)
|
||||||
|
.zip(Some(trait_))
|
||||||
|
});
|
||||||
|
|
||||||
let mut no_traits_found = true;
|
let mut no_traits_found = true;
|
||||||
for (trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {
|
for (trait_path, trait_) in found_traits.inspect(|_| no_traits_found = false) {
|
||||||
|
@ -14,7 +14,10 @@ mod completions;
|
|||||||
use completions::flyimport::position_for_import;
|
use completions::flyimport::position_for_import;
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::FilePosition,
|
base_db::FilePosition,
|
||||||
helpers::{import_assets::LocatedImport, insert_use::ImportScope},
|
helpers::{
|
||||||
|
import_assets::{LocatedImport, NameToImport},
|
||||||
|
insert_use::ImportScope,
|
||||||
|
},
|
||||||
items_locator, RootDatabase,
|
items_locator, RootDatabase,
|
||||||
};
|
};
|
||||||
use text_edit::TextEdit;
|
use text_edit::TextEdit;
|
||||||
@ -149,15 +152,20 @@ pub fn resolve_completion_edits(
|
|||||||
let current_module = ctx.sema.scope(position_for_import).module()?;
|
let current_module = ctx.sema.scope(position_for_import).module()?;
|
||||||
let current_crate = current_module.krate();
|
let current_crate = current_module.krate();
|
||||||
|
|
||||||
let (import_path, item_to_import) =
|
let (import_path, item_to_import) = items_locator::locate_for_name(
|
||||||
items_locator::with_exact_name(&ctx.sema, current_crate, imported_name)
|
&ctx.sema,
|
||||||
.into_iter()
|
current_crate,
|
||||||
.filter_map(|candidate| {
|
NameToImport::Exact(imported_name),
|
||||||
current_module
|
items_locator::AssocItemSearch::Include,
|
||||||
.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
|
None,
|
||||||
.zip(Some(candidate))
|
)
|
||||||
})
|
.into_iter()
|
||||||
.find(|(mod_path, _)| mod_path.to_string() == full_import_path)?;
|
.filter_map(|candidate| {
|
||||||
|
current_module
|
||||||
|
.find_use_path_prefixed(db, candidate, config.insert_use.prefix_kind)
|
||||||
|
.zip(Some(candidate))
|
||||||
|
})
|
||||||
|
.find(|(mod_path, _)| mod_path.to_string() == full_import_path)?;
|
||||||
let import =
|
let import =
|
||||||
LocatedImport::new(import_path.clone(), item_to_import, item_to_import, Some(import_path));
|
LocatedImport::new(import_path.clone(), item_to_import, item_to_import, Some(import_path));
|
||||||
|
|
||||||
|
@ -61,7 +61,7 @@ pub struct FirstSegmentUnresolved {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// A name that will be used during item lookups.
|
/// A name that will be used during item lookups.
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum NameToImport {
|
pub enum NameToImport {
|
||||||
/// Requires items with names that exactly match the given string, case-sensitive.
|
/// Requires items with names that exactly match the given string, case-sensitive.
|
||||||
Exact(String),
|
Exact(String),
|
||||||
@ -201,131 +201,96 @@ impl ImportAssets {
|
|||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
prefixed: Option<PrefixKind>,
|
prefixed: Option<PrefixKind>,
|
||||||
) -> Vec<LocatedImport> {
|
) -> Vec<LocatedImport> {
|
||||||
let items_with_candidate_name = match self.name_to_import() {
|
let _p = profile::span("import_assets::search_for");
|
||||||
NameToImport::Exact(exact_name) => items_locator::with_exact_name(
|
|
||||||
sema,
|
|
||||||
self.module_with_candidate.krate(),
|
|
||||||
exact_name.clone(),
|
|
||||||
),
|
|
||||||
// FIXME: ideally, we should avoid using `fst` for seacrhing trait imports for assoc items:
|
|
||||||
// instead, we need to look up all trait impls for a certain struct and search through them only
|
|
||||||
// see https://github.com/rust-analyzer/rust-analyzer/pull/7293#issuecomment-761585032
|
|
||||||
// and https://rust-lang.zulipchat.com/#narrow/stream/185405-t-compiler.2Fwg-rls-2.2E0/topic/Blanket.20trait.20impls.20lookup
|
|
||||||
// for the details
|
|
||||||
NameToImport::Fuzzy(fuzzy_name) => {
|
|
||||||
let (assoc_item_search, limit) = if self.import_candidate.is_trait_candidate() {
|
|
||||||
(AssocItemSearch::AssocItemsOnly, None)
|
|
||||||
} else {
|
|
||||||
(AssocItemSearch::Include, Some(DEFAULT_QUERY_SEARCH_LIMIT))
|
|
||||||
};
|
|
||||||
|
|
||||||
items_locator::with_similar_name(
|
|
||||||
sema,
|
|
||||||
self.module_with_candidate.krate(),
|
|
||||||
fuzzy_name.clone(),
|
|
||||||
assoc_item_search,
|
|
||||||
limit,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let scope_definitions = self.scope_definitions(sema);
|
let scope_definitions = self.scope_definitions(sema);
|
||||||
self.applicable_defs(sema.db, prefixed, items_with_candidate_name)
|
let current_crate = self.module_with_candidate.krate();
|
||||||
.into_iter()
|
let mod_path = |item| {
|
||||||
.filter(|import| import.import_path.len() > 1)
|
get_mod_path(
|
||||||
.filter(|import| !scope_definitions.contains(&ScopeDef::from(import.item_to_import)))
|
sema.db,
|
||||||
.sorted_by_key(|import| import.import_path.clone())
|
item_for_path_search(sema.db, item)?,
|
||||||
.collect()
|
&self.module_with_candidate,
|
||||||
|
prefixed,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
match &self.import_candidate {
|
||||||
|
ImportCandidate::Path(path_candidate) => {
|
||||||
|
path_applicable_imports(sema, current_crate, path_candidate, mod_path)
|
||||||
|
}
|
||||||
|
ImportCandidate::TraitAssocItem(trait_candidate) => {
|
||||||
|
trait_applicable_items(sema, current_crate, trait_candidate, true, mod_path)
|
||||||
|
}
|
||||||
|
ImportCandidate::TraitMethod(trait_candidate) => {
|
||||||
|
trait_applicable_items(sema, current_crate, trait_candidate, false, mod_path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.into_iter()
|
||||||
|
.filter(|import| import.import_path.len() > 1)
|
||||||
|
.filter(|import| !scope_definitions.contains(&ScopeDef::from(import.item_to_import)))
|
||||||
|
.sorted_by_key(|import| import.import_path.clone())
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn scope_definitions(&self, sema: &Semantics<RootDatabase>) -> FxHashSet<ScopeDef> {
|
fn scope_definitions(&self, sema: &Semantics<RootDatabase>) -> FxHashSet<ScopeDef> {
|
||||||
|
let _p = profile::span("import_assets::scope_definitions");
|
||||||
let mut scope_definitions = FxHashSet::default();
|
let mut scope_definitions = FxHashSet::default();
|
||||||
sema.scope(&self.candidate_node).process_all_names(&mut |_, scope_def| {
|
sema.scope(&self.candidate_node).process_all_names(&mut |_, scope_def| {
|
||||||
scope_definitions.insert(scope_def);
|
scope_definitions.insert(scope_def);
|
||||||
});
|
});
|
||||||
scope_definitions
|
scope_definitions
|
||||||
}
|
}
|
||||||
|
|
||||||
fn name_to_import(&self) -> &NameToImport {
|
|
||||||
match &self.import_candidate {
|
|
||||||
ImportCandidate::Path(candidate) => &candidate.name,
|
|
||||||
ImportCandidate::TraitAssocItem(candidate)
|
|
||||||
| ImportCandidate::TraitMethod(candidate) => &candidate.assoc_item_name,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn applicable_defs(
|
|
||||||
&self,
|
|
||||||
db: &RootDatabase,
|
|
||||||
prefixed: Option<PrefixKind>,
|
|
||||||
items_with_candidate_name: FxHashSet<ItemInNs>,
|
|
||||||
) -> FxHashSet<LocatedImport> {
|
|
||||||
let _p = profile::span("import_assets::applicable_defs");
|
|
||||||
let current_crate = self.module_with_candidate.krate();
|
|
||||||
|
|
||||||
let mod_path = |item| {
|
|
||||||
get_mod_path(db, item_for_path_search(db, item)?, &self.module_with_candidate, prefixed)
|
|
||||||
};
|
|
||||||
|
|
||||||
match &self.import_candidate {
|
|
||||||
ImportCandidate::Path(path_candidate) => {
|
|
||||||
path_applicable_imports(db, path_candidate, mod_path, items_with_candidate_name)
|
|
||||||
}
|
|
||||||
ImportCandidate::TraitAssocItem(trait_candidate) => trait_applicable_items(
|
|
||||||
db,
|
|
||||||
current_crate,
|
|
||||||
trait_candidate,
|
|
||||||
true,
|
|
||||||
mod_path,
|
|
||||||
items_with_candidate_name,
|
|
||||||
),
|
|
||||||
ImportCandidate::TraitMethod(trait_candidate) => trait_applicable_items(
|
|
||||||
db,
|
|
||||||
current_crate,
|
|
||||||
trait_candidate,
|
|
||||||
false,
|
|
||||||
mod_path,
|
|
||||||
items_with_candidate_name,
|
|
||||||
),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_applicable_imports(
|
fn path_applicable_imports(
|
||||||
db: &RootDatabase,
|
sema: &Semantics<RootDatabase>,
|
||||||
|
current_crate: Crate,
|
||||||
path_candidate: &PathImportCandidate,
|
path_candidate: &PathImportCandidate,
|
||||||
mod_path: impl Fn(ItemInNs) -> Option<ModPath> + Copy,
|
mod_path: impl Fn(ItemInNs) -> Option<ModPath> + Copy,
|
||||||
items_with_candidate_name: FxHashSet<ItemInNs>,
|
|
||||||
) -> FxHashSet<LocatedImport> {
|
) -> FxHashSet<LocatedImport> {
|
||||||
let _p = profile::span("import_assets::path_applicable_imports");
|
let _p = profile::span("import_assets::path_applicable_imports");
|
||||||
|
|
||||||
let (unresolved_first_segment, unresolved_qualifier) = match &path_candidate.qualifier {
|
match &path_candidate.qualifier {
|
||||||
None => {
|
None => {
|
||||||
return items_with_candidate_name
|
items_locator::locate_for_name(
|
||||||
.into_iter()
|
sema,
|
||||||
.filter_map(|item| {
|
current_crate,
|
||||||
if item_as_assoc(db, item).is_some() {
|
path_candidate.name.clone(),
|
||||||
// unqualified assoc items are not valid syntax
|
// unqualified assoc items are not valid syntax
|
||||||
return None;
|
AssocItemSearch::Exclude,
|
||||||
}
|
Some(DEFAULT_QUERY_SEARCH_LIMIT),
|
||||||
|
)
|
||||||
let mod_path = mod_path(item)?;
|
.into_iter()
|
||||||
Some(LocatedImport::new(mod_path.clone(), item, item, Some(mod_path)))
|
.filter_map(|item| {
|
||||||
})
|
let mod_path = mod_path(item)?;
|
||||||
.collect();
|
Some(LocatedImport::new(mod_path.clone(), item, item, Some(mod_path)))
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
Some(first_segment_unresolved) => (
|
Some(first_segment_unresolved) => {
|
||||||
first_segment_unresolved.fist_segment.to_string(),
|
let unresolved_qualifier =
|
||||||
path_to_string_stripping_turbo_fish(&first_segment_unresolved.full_qualifier),
|
path_to_string_stripping_turbo_fish(&first_segment_unresolved.full_qualifier);
|
||||||
),
|
let unresolved_first_segment = first_segment_unresolved.fist_segment.text();
|
||||||
};
|
items_locator::locate_for_name(
|
||||||
|
sema,
|
||||||
items_with_candidate_name
|
current_crate,
|
||||||
.into_iter()
|
path_candidate.name.clone(),
|
||||||
.filter_map(|item| {
|
AssocItemSearch::Include,
|
||||||
import_for_item(db, mod_path, &unresolved_first_segment, &unresolved_qualifier, item)
|
Some(DEFAULT_QUERY_SEARCH_LIMIT),
|
||||||
})
|
)
|
||||||
.collect()
|
.into_iter()
|
||||||
|
.filter_map(|item| {
|
||||||
|
import_for_item(
|
||||||
|
sema.db,
|
||||||
|
mod_path,
|
||||||
|
unresolved_first_segment,
|
||||||
|
&unresolved_qualifier,
|
||||||
|
item,
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn import_for_item(
|
fn import_for_item(
|
||||||
@ -440,25 +405,32 @@ fn module_with_segment_name(
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn trait_applicable_items(
|
fn trait_applicable_items(
|
||||||
db: &RootDatabase,
|
sema: &Semantics<RootDatabase>,
|
||||||
current_crate: Crate,
|
current_crate: Crate,
|
||||||
trait_candidate: &TraitImportCandidate,
|
trait_candidate: &TraitImportCandidate,
|
||||||
trait_assoc_item: bool,
|
trait_assoc_item: bool,
|
||||||
mod_path: impl Fn(ItemInNs) -> Option<ModPath>,
|
mod_path: impl Fn(ItemInNs) -> Option<ModPath>,
|
||||||
items_with_candidate_name: FxHashSet<ItemInNs>,
|
|
||||||
) -> FxHashSet<LocatedImport> {
|
) -> FxHashSet<LocatedImport> {
|
||||||
let _p = profile::span("import_assets::trait_applicable_items");
|
let _p = profile::span("import_assets::trait_applicable_items");
|
||||||
let mut required_assoc_items = FxHashSet::default();
|
|
||||||
|
|
||||||
let trait_candidates = items_with_candidate_name
|
let db = sema.db;
|
||||||
.into_iter()
|
|
||||||
.filter_map(|input| item_as_assoc(db, input))
|
let mut required_assoc_items = FxHashSet::default();
|
||||||
.filter_map(|assoc| {
|
let trait_candidates = items_locator::locate_for_name(
|
||||||
let assoc_item_trait = assoc.containing_trait(db)?;
|
sema,
|
||||||
required_assoc_items.insert(assoc);
|
current_crate,
|
||||||
Some(assoc_item_trait.into())
|
trait_candidate.assoc_item_name.clone(),
|
||||||
})
|
AssocItemSearch::AssocItemsOnly,
|
||||||
.collect();
|
Some(DEFAULT_QUERY_SEARCH_LIMIT),
|
||||||
|
)
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|input| item_as_assoc(db, input))
|
||||||
|
.filter_map(|assoc| {
|
||||||
|
let assoc_item_trait = assoc.containing_trait(db)?;
|
||||||
|
required_assoc_items.insert(assoc);
|
||||||
|
Some(assoc_item_trait.into())
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
|
||||||
let mut located_imports = FxHashSet::default();
|
let mut located_imports = FxHashSet::default();
|
||||||
|
|
||||||
@ -567,10 +539,6 @@ impl ImportCandidate {
|
|||||||
) -> Option<Self> {
|
) -> Option<Self> {
|
||||||
path_import_candidate(sema, qualifier, NameToImport::Fuzzy(fuzzy_name))
|
path_import_candidate(sema, qualifier, NameToImport::Fuzzy(fuzzy_name))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_trait_candidate(&self) -> bool {
|
|
||||||
matches!(self, ImportCandidate::TraitAssocItem(_) | ImportCandidate::TraitMethod(_))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn path_import_candidate(
|
fn path_import_candidate(
|
||||||
|
@ -10,6 +10,7 @@ use syntax::{ast, AstNode, SyntaxKind::NAME};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
defs::{Definition, NameClass},
|
defs::{Definition, NameClass},
|
||||||
|
helpers::import_assets::NameToImport,
|
||||||
symbol_index::{self, FileSymbol},
|
symbol_index::{self, FileSymbol},
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
@ -17,68 +18,99 @@ use rustc_hash::FxHashSet;
|
|||||||
|
|
||||||
pub(crate) const DEFAULT_QUERY_SEARCH_LIMIT: usize = 40;
|
pub(crate) const DEFAULT_QUERY_SEARCH_LIMIT: usize = 40;
|
||||||
|
|
||||||
pub fn with_exact_name(
|
/// TODO kb docs here and around + update the module doc
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
#[derive(Debug, Clone, Copy)]
|
||||||
krate: Crate,
|
|
||||||
exact_name: String,
|
|
||||||
) -> FxHashSet<ItemInNs> {
|
|
||||||
let _p = profile::span("find_exact_imports");
|
|
||||||
find_items(
|
|
||||||
sema,
|
|
||||||
krate,
|
|
||||||
{
|
|
||||||
let mut local_query = symbol_index::Query::new(exact_name.clone());
|
|
||||||
local_query.exact();
|
|
||||||
local_query.limit(DEFAULT_QUERY_SEARCH_LIMIT);
|
|
||||||
local_query
|
|
||||||
},
|
|
||||||
import_map::Query::new(exact_name)
|
|
||||||
.limit(DEFAULT_QUERY_SEARCH_LIMIT)
|
|
||||||
.name_only()
|
|
||||||
.search_mode(import_map::SearchMode::Equals)
|
|
||||||
.case_sensitive(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub enum AssocItemSearch {
|
pub enum AssocItemSearch {
|
||||||
Include,
|
Include,
|
||||||
Exclude,
|
Exclude,
|
||||||
AssocItemsOnly,
|
AssocItemsOnly,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn with_similar_name(
|
pub fn locate_for_name(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
krate: Crate,
|
krate: Crate,
|
||||||
fuzzy_search_string: String,
|
name: NameToImport,
|
||||||
assoc_item_search: AssocItemSearch,
|
assoc_item_search: AssocItemSearch,
|
||||||
limit: Option<usize>,
|
limit: Option<usize>,
|
||||||
) -> FxHashSet<ItemInNs> {
|
) -> FxHashSet<ItemInNs> {
|
||||||
let _p = profile::span("find_similar_imports");
|
let _p = profile::span("locate_for_name").detail(|| {
|
||||||
|
format!(
|
||||||
|
"Name: {} ({:?}), crate: {:?}, limit: {:?}",
|
||||||
|
name.text(),
|
||||||
|
assoc_item_search,
|
||||||
|
krate.display_name(sema.db).map(|name| name.to_string()),
|
||||||
|
limit,
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
|
let (mut local_query, mut external_query) = match name {
|
||||||
.search_mode(import_map::SearchMode::Fuzzy)
|
NameToImport::Exact(exact_name) => {
|
||||||
.name_only();
|
let mut local_query = symbol_index::Query::new(exact_name.clone());
|
||||||
|
local_query.exact();
|
||||||
|
|
||||||
match assoc_item_search {
|
let external_query = import_map::Query::new(exact_name)
|
||||||
AssocItemSearch::Include => {}
|
.name_only()
|
||||||
AssocItemSearch::Exclude => {
|
.search_mode(import_map::SearchMode::Equals)
|
||||||
external_query = external_query.exclude_import_kind(ImportKind::AssociatedItem);
|
.case_sensitive();
|
||||||
|
|
||||||
|
(local_query, external_query)
|
||||||
}
|
}
|
||||||
AssocItemSearch::AssocItemsOnly => {
|
NameToImport::Fuzzy(fuzzy_search_string) => {
|
||||||
external_query = external_query.assoc_items_only();
|
let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
|
||||||
}
|
.search_mode(import_map::SearchMode::Fuzzy)
|
||||||
}
|
.name_only();
|
||||||
|
match assoc_item_search {
|
||||||
|
AssocItemSearch::Include => {}
|
||||||
|
AssocItemSearch::Exclude => {
|
||||||
|
external_query = external_query.exclude_import_kind(ImportKind::AssociatedItem);
|
||||||
|
}
|
||||||
|
AssocItemSearch::AssocItemsOnly => {
|
||||||
|
external_query = external_query.assoc_items_only();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let mut local_query = symbol_index::Query::new(fuzzy_search_string);
|
(symbol_index::Query::new(fuzzy_search_string), external_query)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
if let Some(limit) = limit {
|
if let Some(limit) = limit {
|
||||||
external_query = external_query.limit(limit);
|
external_query = external_query.limit(limit);
|
||||||
local_query.limit(limit);
|
local_query.limit(limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
find_items(sema, krate, local_query, external_query)
|
find_items(sema, krate, assoc_item_search, local_query, external_query)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_items(
|
||||||
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
|
krate: Crate,
|
||||||
|
assoc_item_search: AssocItemSearch,
|
||||||
|
local_query: symbol_index::Query,
|
||||||
|
external_query: import_map::Query,
|
||||||
|
) -> FxHashSet<ItemInNs> {
|
||||||
|
let _p = profile::span("find_items");
|
||||||
|
let db = sema.db;
|
||||||
|
|
||||||
|
let external_importables =
|
||||||
|
krate.query_external_importables(db, external_query).map(|external_importable| {
|
||||||
|
match external_importable {
|
||||||
|
Either::Left(module_def) => ItemInNs::from(module_def),
|
||||||
|
Either::Right(macro_def) => ItemInNs::from(macro_def),
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Query the local crate using the symbol index.
|
||||||
|
let local_results = symbol_index::crate_symbols(db, krate.into(), local_query)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
.filter_map(|local_candidate| get_name_definition(sema, &local_candidate))
|
||||||
|
.filter_map(|name_definition_to_import| match name_definition_to_import {
|
||||||
|
Definition::ModuleDef(module_def) => Some(ItemInNs::from(module_def)),
|
||||||
|
Definition::Macro(macro_def) => Some(ItemInNs::from(macro_def)),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
|
||||||
|
external_importables
|
||||||
|
.chain(local_results)
|
||||||
.filter(move |&item| match assoc_item_search {
|
.filter(move |&item| match assoc_item_search {
|
||||||
AssocItemSearch::Include => true,
|
AssocItemSearch::Include => true,
|
||||||
AssocItemSearch::Exclude => !is_assoc_item(item, sema.db),
|
AssocItemSearch::Exclude => !is_assoc_item(item, sema.db),
|
||||||
@ -87,47 +119,6 @@ pub fn with_similar_name(
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_assoc_item(item: ItemInNs, db: &RootDatabase) -> bool {
|
|
||||||
item.as_module_def_id()
|
|
||||||
.and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db))
|
|
||||||
.is_some()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn find_items(
|
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
|
||||||
krate: Crate,
|
|
||||||
local_query: symbol_index::Query,
|
|
||||||
external_query: import_map::Query,
|
|
||||||
) -> FxHashSet<ItemInNs> {
|
|
||||||
let _p = profile::span("find_similar_imports");
|
|
||||||
let db = sema.db;
|
|
||||||
|
|
||||||
// Query dependencies first.
|
|
||||||
let mut candidates = krate
|
|
||||||
.query_external_importables(db, external_query)
|
|
||||||
.map(|external_importable| match external_importable {
|
|
||||||
Either::Left(module_def) => ItemInNs::from(module_def),
|
|
||||||
Either::Right(macro_def) => ItemInNs::from(macro_def),
|
|
||||||
})
|
|
||||||
.collect::<FxHashSet<_>>();
|
|
||||||
|
|
||||||
// Query the local crate using the symbol index.
|
|
||||||
let local_results = symbol_index::crate_symbols(db, krate.into(), local_query);
|
|
||||||
|
|
||||||
candidates.extend(
|
|
||||||
local_results
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(|local_candidate| get_name_definition(sema, &local_candidate))
|
|
||||||
.filter_map(|name_definition_to_import| match name_definition_to_import {
|
|
||||||
Definition::ModuleDef(module_def) => Some(ItemInNs::from(module_def)),
|
|
||||||
Definition::Macro(macro_def) => Some(ItemInNs::from(macro_def)),
|
|
||||||
_ => None,
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
candidates
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_name_definition(
|
fn get_name_definition(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
import_candidate: &FileSymbol,
|
import_candidate: &FileSymbol,
|
||||||
@ -144,3 +135,9 @@ fn get_name_definition(
|
|||||||
let name = ast::Name::cast(candidate_name_node)?;
|
let name = ast::Name::cast(candidate_name_node)?;
|
||||||
NameClass::classify(sema, &name)?.defined(sema.db)
|
NameClass::classify(sema, &name)?.defined(sema.db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn is_assoc_item(item: ItemInNs, db: &RootDatabase) -> bool {
|
||||||
|
item.as_module_def_id()
|
||||||
|
.and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db))
|
||||||
|
.is_some()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user