aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_db/src/items_locator.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_db/src/items_locator.rs')
-rw-r--r--crates/ide_db/src/items_locator.rs163
1 files changed, 83 insertions, 80 deletions
diff --git a/crates/ide_db/src/items_locator.rs b/crates/ide_db/src/items_locator.rs
index 8a7f02935..518cddd74 100644
--- a/crates/ide_db/src/items_locator.rs
+++ b/crates/ide_db/src/items_locator.rs
@@ -1,6 +1,7 @@
1//! This module contains an import search functionality that is provided to the assists module. 1//! This module has the functionality to search the project and its dependencies for a certain item,
2//! Later, this should be moved away to a separate crate that is accessible from the assists module. 2//! by its name and a few criteria.
3 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.
4use either::Either; 5use either::Either;
5use hir::{ 6use hir::{
6 import_map::{self, ImportKind}, 7 import_map::{self, ImportKind},
@@ -10,122 +11,118 @@ use syntax::{ast, AstNode, SyntaxKind::NAME};
10 11
11use crate::{ 12use crate::{
12 defs::{Definition, NameClass}, 13 defs::{Definition, NameClass},
14 helpers::import_assets::NameToImport,
13 symbol_index::{self, FileSymbol}, 15 symbol_index::{self, FileSymbol},
14 RootDatabase, 16 RootDatabase,
15}; 17};
16use rustc_hash::FxHashSet; 18use rustc_hash::FxHashSet;
17 19
18pub(crate) const DEFAULT_QUERY_SEARCH_LIMIT: usize = 40; 20/// A value to use, when uncertain which limit to pick.
21pub const DEFAULT_QUERY_SEARCH_LIMIT: usize = 40;
19 22
20pub fn with_exact_name( 23/// Three possible ways to search for the name in associated and/or other items.
21 sema: &Semantics<'_, RootDatabase>, 24#[derive(Debug, Clone, Copy)]
22 krate: Crate,
23 exact_name: String,
24) -> FxHashSet<ItemInNs> {
25 let _p = profile::span("find_exact_imports");
26 find_items(
27 sema,
28 krate,
29 {
30 let mut local_query = symbol_index::Query::new(exact_name.clone());
31 local_query.exact();
32 local_query.limit(DEFAULT_QUERY_SEARCH_LIMIT);
33 local_query
34 },
35 import_map::Query::new(exact_name)
36 .limit(DEFAULT_QUERY_SEARCH_LIMIT)
37 .name_only()
38 .search_mode(import_map::SearchMode::Equals)
39 .case_sensitive(),
40 )
41}
42
43#[derive(Debug)]
44pub enum AssocItemSearch { 25pub enum AssocItemSearch {
26 /// Search for the name in both associated and other items.
45 Include, 27 Include,
28 /// Search for the name in other items only.
46 Exclude, 29 Exclude,
30 /// Search for the name in the associated items only.
47 AssocItemsOnly, 31 AssocItemsOnly,
48} 32}
49 33
50pub fn with_similar_name( 34/// Searches for importable items with the given name in the crate and its dependencies.
35pub fn items_with_name(
51 sema: &Semantics<'_, RootDatabase>, 36 sema: &Semantics<'_, RootDatabase>,
52 krate: Crate, 37 krate: Crate,
53 fuzzy_search_string: String, 38 name: NameToImport,
54 assoc_item_search: AssocItemSearch, 39 assoc_item_search: AssocItemSearch,
55 limit: Option<usize>, 40 limit: Option<usize>,
56) -> FxHashSet<ItemInNs> { 41) -> FxHashSet<ItemInNs> {
57 let _p = profile::span("find_similar_imports"); 42 let _p = profile::span("items_with_name").detail(|| {
43 format!(
44 "Name: {} ({:?}), crate: {:?}, limit: {:?}",
45 name.text(),
46 assoc_item_search,
47 krate.display_name(sema.db).map(|name| name.to_string()),
48 limit,
49 )
50 });
51
52 let (mut local_query, mut external_query) = match name {
53 NameToImport::Exact(exact_name) => {
54 let mut local_query = symbol_index::Query::new(exact_name.clone());
55 local_query.exact();
58 56
59 let mut external_query = import_map::Query::new(fuzzy_search_string.clone()) 57 let external_query = import_map::Query::new(exact_name)
60 .search_mode(import_map::SearchMode::Fuzzy) 58 .name_only()
61 .name_only(); 59 .search_mode(import_map::SearchMode::Equals)
60 .case_sensitive();
62 61
63 match assoc_item_search { 62 (local_query, external_query)
64 AssocItemSearch::Include => {}
65 AssocItemSearch::Exclude => {
66 external_query = external_query.exclude_import_kind(ImportKind::AssociatedItem);
67 } 63 }
68 AssocItemSearch::AssocItemsOnly => { 64 NameToImport::Fuzzy(fuzzy_search_string) => {
69 external_query = external_query.assoc_items_only(); 65 let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
66 .search_mode(import_map::SearchMode::Fuzzy)
67 .name_only();
68 match assoc_item_search {
69 AssocItemSearch::Include => {}
70 AssocItemSearch::Exclude => {
71 external_query = external_query.exclude_import_kind(ImportKind::AssociatedItem);
72 }
73 AssocItemSearch::AssocItemsOnly => {
74 external_query = external_query.assoc_items_only();
75 }
76 }
77
78 (symbol_index::Query::new(fuzzy_search_string), external_query)
70 } 79 }
71 } 80 };
72
73 let mut local_query = symbol_index::Query::new(fuzzy_search_string);
74 81
75 if let Some(limit) = limit { 82 if let Some(limit) = limit {
76 external_query = external_query.limit(limit); 83 external_query = external_query.limit(limit);
77 local_query.limit(limit); 84 local_query.limit(limit);
78 } 85 }
79 86
80 find_items(sema, krate, local_query, external_query) 87 find_items(sema, krate, assoc_item_search, local_query, external_query)
81 .into_iter()
82 .filter(move |&item| match assoc_item_search {
83 AssocItemSearch::Include => true,
84 AssocItemSearch::Exclude => !is_assoc_item(item, sema.db),
85 AssocItemSearch::AssocItemsOnly => is_assoc_item(item, sema.db),
86 })
87 .collect()
88}
89
90fn is_assoc_item(item: ItemInNs, db: &RootDatabase) -> bool {
91 item.as_module_def_id()
92 .and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db))
93 .is_some()
94} 88}
95 89
96fn find_items( 90fn find_items(
97 sema: &Semantics<'_, RootDatabase>, 91 sema: &Semantics<'_, RootDatabase>,
98 krate: Crate, 92 krate: Crate,
93 assoc_item_search: AssocItemSearch,
99 local_query: symbol_index::Query, 94 local_query: symbol_index::Query,
100 external_query: import_map::Query, 95 external_query: import_map::Query,
101) -> FxHashSet<ItemInNs> { 96) -> FxHashSet<ItemInNs> {
102 let _p = profile::span("find_similar_imports"); 97 let _p = profile::span("find_items");
103 let db = sema.db; 98 let db = sema.db;
104 99
105 // Query dependencies first. 100 let external_importables =
106 let mut candidates = krate 101 krate.query_external_importables(db, external_query).map(|external_importable| {
107 .query_external_importables(db, external_query) 102 match external_importable {
108 .map(|external_importable| match external_importable { 103 Either::Left(module_def) => ItemInNs::from(module_def),
109 Either::Left(module_def) => ItemInNs::from(module_def), 104 Either::Right(macro_def) => ItemInNs::from(macro_def),
110 Either::Right(macro_def) => ItemInNs::from(macro_def), 105 }
111 }) 106 });
112 .collect::<FxHashSet<_>>();
113 107
114 // Query the local crate using the symbol index. 108 // Query the local crate using the symbol index.
115 let local_results = symbol_index::crate_symbols(db, krate.into(), local_query); 109 let local_results = symbol_index::crate_symbols(db, krate.into(), local_query)
116 110 .into_iter()
117 candidates.extend( 111 .filter_map(|local_candidate| get_name_definition(sema, &local_candidate))
118 local_results 112 .filter_map(|name_definition_to_import| match name_definition_to_import {
119 .into_iter() 113 Definition::ModuleDef(module_def) => Some(ItemInNs::from(module_def)),
120 .filter_map(|local_candidate| get_name_definition(sema, &local_candidate)) 114 Definition::Macro(macro_def) => Some(ItemInNs::from(macro_def)),
121 .filter_map(|name_definition_to_import| match name_definition_to_import { 115 _ => None,
122 Definition::ModuleDef(module_def) => Some(ItemInNs::from(module_def)), 116 });
123 Definition::Macro(macro_def) => Some(ItemInNs::from(macro_def)), 117
124 _ => None, 118 external_importables
125 }), 119 .chain(local_results)
126 ); 120 .filter(move |&item| match assoc_item_search {
127 121 AssocItemSearch::Include => true,
128 candidates 122 AssocItemSearch::Exclude => !is_assoc_item(item, sema.db),
123 AssocItemSearch::AssocItemsOnly => is_assoc_item(item, sema.db),
124 })
125 .collect()
129} 126}
130 127
131fn get_name_definition( 128fn get_name_definition(
@@ -144,3 +141,9 @@ fn get_name_definition(
144 let name = ast::Name::cast(candidate_name_node)?; 141 let name = ast::Name::cast(candidate_name_node)?;
145 NameClass::classify(sema, &name)?.defined(sema.db) 142 NameClass::classify(sema, &name)?.defined(sema.db)
146} 143}
144
145fn is_assoc_item(item: ItemInNs, db: &RootDatabase) -> bool {
146 item.as_module_def_id()
147 .and_then(|module_def_id| ModuleDef::from(module_def_id).as_assoc_item(db))
148 .is_some()
149}