ide_db/
items_locator.rs

1//! This module has the functionality to search the project and its dependencies for a certain item,
2//! by its name and a few criteria.
3//! The main reason for this module to exist is the fact that project's items and dependencies' items
4//! are located in different caches, with different APIs.
5use std::ops::ControlFlow;
6
7use either::Either;
8use hir::{Complete, Crate, ItemInNs, Module, import_map};
9
10use crate::{
11    RootDatabase,
12    imports::import_assets::NameToImport,
13    symbol_index::{self, SymbolsDatabase as _},
14};
15
16/// A value to use, when uncertain which limit to pick.
17pub const DEFAULT_QUERY_SEARCH_LIMIT: usize = 100;
18
19pub use import_map::AssocSearchMode;
20
21// FIXME: Do callbacks instead to avoid allocations.
22/// Searches for importable items with the given name in the crate and its dependencies.
23pub fn items_with_name(
24    db: &RootDatabase,
25    krate: Crate,
26    name: NameToImport,
27    assoc_item_search: AssocSearchMode,
28) -> impl Iterator<Item = (ItemInNs, Complete)> {
29    let _p = tracing::info_span!("items_with_name", name = name.text(), assoc_item_search = ?assoc_item_search, crate = ?krate.display_name(db).map(|name| name.to_string()))
30        .entered();
31
32    let prefix = matches!(name, NameToImport::Prefix(..));
33    let (local_query, external_query) = match name {
34        NameToImport::Prefix(exact_name, case_sensitive)
35        | NameToImport::Exact(exact_name, case_sensitive) => {
36            let mut local_query = symbol_index::Query::new(exact_name.clone());
37            local_query.assoc_search_mode(assoc_item_search);
38            let mut external_query =
39                import_map::Query::new(exact_name).assoc_search_mode(assoc_item_search);
40            if prefix {
41                local_query.prefix();
42                external_query = external_query.prefix();
43            } else {
44                local_query.exact();
45                external_query = external_query.exact();
46            }
47            if case_sensitive {
48                local_query.case_sensitive();
49                external_query = external_query.case_sensitive();
50            }
51            (local_query, external_query)
52        }
53        NameToImport::Fuzzy(fuzzy_search_string, case_sensitive) => {
54            let mut local_query = symbol_index::Query::new(fuzzy_search_string.clone());
55            local_query.fuzzy();
56            local_query.assoc_search_mode(assoc_item_search);
57
58            let mut external_query = import_map::Query::new(fuzzy_search_string)
59                .fuzzy()
60                .assoc_search_mode(assoc_item_search);
61
62            if case_sensitive {
63                local_query.case_sensitive();
64                external_query = external_query.case_sensitive();
65            }
66
67            (local_query, external_query)
68        }
69    };
70
71    find_items(db, krate, local_query, external_query)
72}
73
74/// Searches for importable items with the given name in the crate and its dependencies.
75pub fn items_with_name_in_module<T>(
76    db: &RootDatabase,
77    module: Module,
78    name: NameToImport,
79    assoc_item_search: AssocSearchMode,
80    mut cb: impl FnMut(ItemInNs) -> ControlFlow<T>,
81) -> Option<T> {
82    let _p = tracing::info_span!("items_with_name_in", name = name.text(), assoc_item_search = ?assoc_item_search, ?module)
83        .entered();
84
85    let prefix = matches!(name, NameToImport::Prefix(..));
86    let local_query = match name {
87        NameToImport::Prefix(exact_name, case_sensitive)
88        | NameToImport::Exact(exact_name, case_sensitive) => {
89            let mut local_query = symbol_index::Query::new(exact_name);
90            local_query.assoc_search_mode(assoc_item_search);
91            if prefix {
92                local_query.prefix();
93            } else {
94                local_query.exact();
95            }
96            if case_sensitive {
97                local_query.case_sensitive();
98            }
99            local_query
100        }
101        NameToImport::Fuzzy(fuzzy_search_string, case_sensitive) => {
102            let mut local_query = symbol_index::Query::new(fuzzy_search_string);
103            local_query.fuzzy();
104            local_query.assoc_search_mode(assoc_item_search);
105
106            if case_sensitive {
107                local_query.case_sensitive();
108            }
109
110            local_query
111        }
112    };
113    local_query.search(&[db.module_symbols(module)], |local_candidate| {
114        cb(match local_candidate.def {
115            hir::ModuleDef::Macro(macro_def) => ItemInNs::Macros(macro_def),
116            def => ItemInNs::from(def),
117        })
118    })
119}
120
121fn find_items(
122    db: &RootDatabase,
123    krate: Crate,
124    local_query: symbol_index::Query,
125    external_query: import_map::Query,
126) -> impl Iterator<Item = (ItemInNs, Complete)> {
127    let _p = tracing::info_span!("find_items").entered();
128
129    // NOTE: `external_query` includes `assoc_item_search`, so we don't need to
130    // filter on our own.
131    let external_importables = krate.query_external_importables(db, external_query).map(
132        |(external_importable, do_not_complete)| {
133            let external_importable = match external_importable {
134                Either::Left(module_def) => ItemInNs::from(module_def),
135                Either::Right(macro_def) => ItemInNs::from(macro_def),
136            };
137            (external_importable, do_not_complete)
138        },
139    );
140
141    // Query the local crate using the symbol index.
142    let mut local_results = Vec::new();
143    local_query.search(&symbol_index::crate_symbols(db, krate), |local_candidate| {
144        let def = match local_candidate.def {
145            hir::ModuleDef::Macro(macro_def) => ItemInNs::Macros(macro_def),
146            def => ItemInNs::from(def),
147        };
148        local_results.push((def, local_candidate.do_not_complete));
149        ControlFlow::<()>::Continue(())
150    });
151    local_results.into_iter().chain(external_importables)
152}