2021-03-20 20:54:04 +00:00
|
|
|
//! This module has the functionality to search the project and its dependencies for a certain item,
|
|
|
|
//! by its name and a few criteria.
|
|
|
|
//! The main reason for this module to exist is the fact that project's items and dependencies' items
|
|
|
|
//! are located in different caches, with different APIs.
|
2021-03-02 23:26:53 +00:00
|
|
|
use either::Either;
|
2023-12-12 11:03:37 +00:00
|
|
|
use hir::{import_map, Crate, ItemInNs, Semantics};
|
2021-07-10 20:49:17 +00:00
|
|
|
use limit::Limit;
|
2019-12-24 00:19:09 +00:00
|
|
|
|
2023-05-02 09:56:48 +00:00
|
|
|
use crate::{imports::import_assets::NameToImport, symbol_index, RootDatabase};
|
2020-02-06 11:52:32 +00:00
|
|
|
|
2021-03-20 20:54:04 +00:00
|
|
|
/// A value to use, when uncertain which limit to pick.
|
2023-12-12 11:03:37 +00:00
|
|
|
pub static DEFAULT_QUERY_SEARCH_LIMIT: Limit = Limit::new(100);
|
2021-01-16 17:33:36 +00:00
|
|
|
|
2023-06-29 09:39:37 +00:00
|
|
|
pub use import_map::AssocSearchMode;
|
2020-11-13 17:16:56 +00:00
|
|
|
|
2021-03-20 20:54:04 +00:00
|
|
|
/// Searches for importable items with the given name in the crate and its dependencies.
|
2021-03-20 22:17:09 +00:00
|
|
|
pub fn items_with_name<'a>(
|
|
|
|
sema: &'a Semantics<'_, RootDatabase>,
|
2020-11-13 17:16:56 +00:00
|
|
|
krate: Crate,
|
2021-03-20 13:02:52 +00:00
|
|
|
name: NameToImport,
|
2023-06-29 09:39:37 +00:00
|
|
|
assoc_item_search: AssocSearchMode,
|
2021-03-20 22:17:09 +00:00
|
|
|
) -> impl Iterator<Item = ItemInNs> + 'a {
|
2024-01-18 02:27:38 +00:00
|
|
|
let krate_name = krate.display_name(sema.db).map(|name| name.to_string());
|
2024-06-06 23:52:25 +00:00
|
|
|
let _p = tracing::info_span!("items_with_name", name = name.text(), assoc_item_search = ?assoc_item_search, crate = ?krate_name)
|
2024-01-18 02:27:38 +00:00
|
|
|
.entered();
|
2021-03-20 13:02:52 +00:00
|
|
|
|
2023-10-05 11:21:12 +00:00
|
|
|
let prefix = matches!(name, NameToImport::Prefix(..));
|
2024-01-04 18:20:10 +00:00
|
|
|
let (local_query, external_query) = match name {
|
2023-10-05 11:21:12 +00:00
|
|
|
NameToImport::Prefix(exact_name, case_sensitive)
|
|
|
|
| NameToImport::Exact(exact_name, case_sensitive) => {
|
2021-03-20 13:02:52 +00:00
|
|
|
let mut local_query = symbol_index::Query::new(exact_name.clone());
|
2024-01-04 17:34:01 +00:00
|
|
|
local_query.assoc_search_mode(assoc_item_search);
|
2023-12-12 14:44:27 +00:00
|
|
|
let mut external_query =
|
2024-01-04 17:34:01 +00:00
|
|
|
import_map::Query::new(exact_name).assoc_search_mode(assoc_item_search);
|
2023-10-05 11:21:12 +00:00
|
|
|
if prefix {
|
|
|
|
local_query.prefix();
|
|
|
|
external_query = external_query.prefix();
|
|
|
|
} else {
|
|
|
|
local_query.exact();
|
|
|
|
external_query = external_query.exact();
|
|
|
|
}
|
|
|
|
if case_sensitive {
|
|
|
|
local_query.case_sensitive();
|
|
|
|
external_query = external_query.case_sensitive();
|
|
|
|
}
|
|
|
|
(local_query, external_query)
|
2021-01-05 08:34:03 +00:00
|
|
|
}
|
2023-10-05 11:21:12 +00:00
|
|
|
NameToImport::Fuzzy(fuzzy_search_string, case_sensitive) => {
|
2021-03-20 21:55:16 +00:00
|
|
|
let mut local_query = symbol_index::Query::new(fuzzy_search_string.clone());
|
2023-10-05 11:21:12 +00:00
|
|
|
local_query.fuzzy();
|
2024-01-04 17:34:01 +00:00
|
|
|
local_query.assoc_search_mode(assoc_item_search);
|
2021-03-20 21:55:16 +00:00
|
|
|
|
2024-01-06 23:17:48 +00:00
|
|
|
let mut external_query = import_map::Query::new(fuzzy_search_string)
|
2023-06-29 09:53:59 +00:00
|
|
|
.fuzzy()
|
2023-06-29 09:39:37 +00:00
|
|
|
.assoc_search_mode(assoc_item_search);
|
2021-03-20 13:02:52 +00:00
|
|
|
|
2023-10-05 11:21:12 +00:00
|
|
|
if case_sensitive {
|
2021-03-20 21:55:16 +00:00
|
|
|
local_query.case_sensitive();
|
|
|
|
external_query = external_query.case_sensitive();
|
|
|
|
}
|
|
|
|
|
|
|
|
(local_query, external_query)
|
2021-01-05 08:34:03 +00:00
|
|
|
}
|
2021-03-20 13:02:52 +00:00
|
|
|
};
|
2021-01-16 22:53:15 +00:00
|
|
|
|
2024-01-04 17:34:01 +00:00
|
|
|
find_items(sema, krate, local_query, external_query)
|
2020-11-13 17:16:56 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 22:17:09 +00:00
|
|
|
fn find_items<'a>(
|
|
|
|
sema: &'a Semantics<'_, RootDatabase>,
|
2020-11-13 17:16:56 +00:00
|
|
|
krate: Crate,
|
2020-11-16 19:24:54 +00:00
|
|
|
local_query: symbol_index::Query,
|
|
|
|
external_query: import_map::Query,
|
2021-03-20 22:17:09 +00:00
|
|
|
) -> impl Iterator<Item = ItemInNs> + 'a {
|
2024-06-06 23:52:25 +00:00
|
|
|
let _p = tracing::info_span!("find_items").entered();
|
2020-07-01 08:48:15 +00:00
|
|
|
let db = sema.db;
|
2019-12-24 00:19:09 +00:00
|
|
|
|
2023-06-29 09:39:37 +00:00
|
|
|
// NOTE: `external_query` includes `assoc_item_search`, so we don't need to
|
|
|
|
// filter on our own.
|
2021-03-20 13:02:52 +00:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
});
|
2019-12-24 00:19:09 +00:00
|
|
|
|
2020-07-01 08:48:15 +00:00
|
|
|
// Query the local crate using the symbol index.
|
2024-01-04 17:34:01 +00:00
|
|
|
let mut local_results = Vec::new();
|
|
|
|
local_query.search(&symbol_index::crate_symbols(db, krate), |local_candidate| {
|
|
|
|
local_results.push(match local_candidate.def {
|
2023-06-29 09:39:37 +00:00
|
|
|
hir::ModuleDef::Macro(macro_def) => ItemInNs::Macros(macro_def),
|
|
|
|
def => ItemInNs::from(def),
|
2024-01-04 17:34:01 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
local_results.into_iter().chain(external_importables)
|
2021-03-20 13:02:52 +00:00
|
|
|
}
|