]> git.lizzy.rs Git - rust.git/blob - crates/ide_db/src/imports_locator.rs
Add flyimport completion for trait assoc items
[rust.git] / crates / ide_db / src / imports_locator.rs
1 //! This module contains an import search functionality that is provided to the assists module.
2 //! Later, this should be moved away to a separate crate that is accessible from the assists module.
3
4 use hir::{
5     import_map::{self, ImportKind},
6     AsAssocItem, Crate, MacroDef, ModuleDef, Semantics,
7 };
8 use syntax::{ast, AstNode, SyntaxKind::NAME};
9
10 use crate::{
11     defs::{Definition, NameClass},
12     symbol_index::{self, FileSymbol},
13     RootDatabase,
14 };
15 use either::Either;
16 use rustc_hash::FxHashSet;
17
18 const QUERY_SEARCH_LIMIT: usize = 40;
19
20 pub fn find_exact_imports<'a>(
21     sema: &Semantics<'a, RootDatabase>,
22     krate: Crate,
23     name_to_import: String,
24 ) -> Box<dyn Iterator<Item = Either<ModuleDef, MacroDef>>> {
25     let _p = profile::span("find_exact_imports");
26     Box::new(find_imports(
27         sema,
28         krate,
29         {
30             let mut local_query = symbol_index::Query::new(name_to_import.clone());
31             local_query.exact();
32             local_query.limit(QUERY_SEARCH_LIMIT);
33             local_query
34         },
35         import_map::Query::new(name_to_import)
36             .limit(QUERY_SEARCH_LIMIT)
37             .name_only()
38             .search_mode(import_map::SearchMode::Equals)
39             .case_sensitive(),
40     ))
41 }
42
43 pub enum AssocItemSearch {
44     Include,
45     Exclude,
46     AssocItemsOnly,
47 }
48
49 pub fn find_similar_imports<'a>(
50     sema: &Semantics<'a, RootDatabase>,
51     krate: Crate,
52     fuzzy_search_string: String,
53     assoc_item_search: AssocItemSearch,
54 ) -> Box<dyn Iterator<Item = Either<ModuleDef, MacroDef>> + 'a> {
55     let _p = profile::span("find_similar_imports");
56
57     let mut external_query = import_map::Query::new(fuzzy_search_string.clone())
58         .search_mode(import_map::SearchMode::Fuzzy)
59         .name_only()
60         .limit(QUERY_SEARCH_LIMIT);
61
62     match assoc_item_search {
63         AssocItemSearch::Include => {}
64         AssocItemSearch::Exclude => {
65             external_query = external_query.exclude_import_kind(ImportKind::AssociatedItem);
66         }
67         AssocItemSearch::AssocItemsOnly => {
68             external_query = external_query.assoc_items_only();
69         }
70     }
71
72     let mut local_query = symbol_index::Query::new(fuzzy_search_string);
73     local_query.limit(QUERY_SEARCH_LIMIT);
74
75     let db = sema.db;
76     Box::new(find_imports(sema, krate, local_query, external_query).filter(
77         move |import_candidate| match assoc_item_search {
78             AssocItemSearch::Include => true,
79             AssocItemSearch::Exclude => !is_assoc_item(import_candidate, db),
80             AssocItemSearch::AssocItemsOnly => is_assoc_item(import_candidate, db),
81         },
82     ))
83 }
84
85 fn is_assoc_item(import_candidate: &Either<ModuleDef, MacroDef>, db: &RootDatabase) -> bool {
86     match import_candidate {
87         Either::Left(ModuleDef::Function(function)) => function.as_assoc_item(db).is_some(),
88         Either::Left(ModuleDef::Const(const_)) => const_.as_assoc_item(db).is_some(),
89         Either::Left(ModuleDef::TypeAlias(type_alias)) => type_alias.as_assoc_item(db).is_some(),
90         _ => false,
91     }
92 }
93
94 fn find_imports<'a>(
95     sema: &Semantics<'a, RootDatabase>,
96     krate: Crate,
97     local_query: symbol_index::Query,
98     external_query: import_map::Query,
99 ) -> impl Iterator<Item = Either<ModuleDef, MacroDef>> {
100     let _p = profile::span("find_similar_imports");
101     let db = sema.db;
102
103     // Query dependencies first.
104     let mut candidates: FxHashSet<_> =
105         krate.query_external_importables(db, external_query).collect();
106
107     // Query the local crate using the symbol index.
108     let local_results = symbol_index::crate_symbols(db, krate.into(), local_query);
109
110     candidates.extend(
111         local_results
112             .into_iter()
113             .filter_map(|import_candidate| get_name_definition(sema, &import_candidate))
114             .filter_map(|name_definition_to_import| match name_definition_to_import {
115                 Definition::ModuleDef(module_def) => Some(Either::Left(module_def)),
116                 Definition::Macro(macro_def) => Some(Either::Right(macro_def)),
117                 _ => None,
118             }),
119     );
120
121     candidates.into_iter()
122 }
123
124 fn get_name_definition<'a>(
125     sema: &Semantics<'a, RootDatabase>,
126     import_candidate: &FileSymbol,
127 ) -> Option<Definition> {
128     let _p = profile::span("get_name_definition");
129     let file_id = import_candidate.file_id;
130
131     let candidate_node = import_candidate.ptr.to_node(sema.parse(file_id).syntax());
132     let candidate_name_node = if candidate_node.kind() != NAME {
133         candidate_node.children().find(|it| it.kind() == NAME)?
134     } else {
135         candidate_node
136     };
137     let name = ast::Name::cast(candidate_name_node)?;
138     NameClass::classify(sema, &name)?.defined(sema.db)
139 }