aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/utils/import_assets.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/assists/src/utils/import_assets.rs')
-rw-r--r--crates/assists/src/utils/import_assets.rs268
1 files changed, 268 insertions, 0 deletions
diff --git a/crates/assists/src/utils/import_assets.rs b/crates/assists/src/utils/import_assets.rs
new file mode 100644
index 000000000..601f51098
--- /dev/null
+++ b/crates/assists/src/utils/import_assets.rs
@@ -0,0 +1,268 @@
1//! Look up accessible paths for items.
2use std::collections::BTreeSet;
3
4use either::Either;
5use hir::{AsAssocItem, AssocItemContainer, ModuleDef, Semantics};
6use ide_db::{imports_locator, RootDatabase};
7use rustc_hash::FxHashSet;
8use syntax::{ast, AstNode, SyntaxNode};
9
10use crate::assist_config::InsertUseConfig;
11
12#[derive(Debug)]
13pub(crate) enum ImportCandidate {
14 /// Simple name like 'HashMap'
15 UnqualifiedName(PathImportCandidate),
16 /// First part of the qualified name.
17 /// For 'std::collections::HashMap', that will be 'std'.
18 QualifierStart(PathImportCandidate),
19 /// A trait associated function (with no self parameter) or associated constant.
20 /// For 'test_mod::TestEnum::test_function', `ty` is the `test_mod::TestEnum` expression type
21 /// and `name` is the `test_function`
22 TraitAssocItem(TraitImportCandidate),
23 /// A trait method with self parameter.
24 /// For 'test_enum.test_method()', `ty` is the `test_enum` expression type
25 /// and `name` is the `test_method`
26 TraitMethod(TraitImportCandidate),
27}
28
29#[derive(Debug)]
30pub(crate) struct TraitImportCandidate {
31 pub ty: hir::Type,
32 pub name: String,
33}
34
35#[derive(Debug)]
36pub(crate) struct PathImportCandidate {
37 pub name: String,
38}
39
40#[derive(Debug)]
41pub(crate) struct ImportAssets {
42 import_candidate: ImportCandidate,
43 module_with_name_to_import: hir::Module,
44 syntax_under_caret: SyntaxNode,
45}
46
47impl ImportAssets {
48 pub(crate) fn for_method_call(
49 method_call: ast::MethodCallExpr,
50 sema: &Semantics<RootDatabase>,
51 ) -> Option<Self> {
52 let syntax_under_caret = method_call.syntax().to_owned();
53 let module_with_name_to_import = sema.scope(&syntax_under_caret).module()?;
54 Some(Self {
55 import_candidate: ImportCandidate::for_method_call(sema, &method_call)?,
56 module_with_name_to_import,
57 syntax_under_caret,
58 })
59 }
60
61 pub(crate) fn for_regular_path(
62 path_under_caret: ast::Path,
63 sema: &Semantics<RootDatabase>,
64 ) -> Option<Self> {
65 let syntax_under_caret = path_under_caret.syntax().to_owned();
66 if syntax_under_caret.ancestors().find_map(ast::Use::cast).is_some() {
67 return None;
68 }
69
70 let module_with_name_to_import = sema.scope(&syntax_under_caret).module()?;
71 Some(Self {
72 import_candidate: ImportCandidate::for_regular_path(sema, &path_under_caret)?,
73 module_with_name_to_import,
74 syntax_under_caret,
75 })
76 }
77
78 pub(crate) fn syntax_under_caret(&self) -> &SyntaxNode {
79 &self.syntax_under_caret
80 }
81
82 pub(crate) fn import_candidate(&self) -> &ImportCandidate {
83 &self.import_candidate
84 }
85
86 fn get_search_query(&self) -> &str {
87 match &self.import_candidate {
88 ImportCandidate::UnqualifiedName(candidate)
89 | ImportCandidate::QualifierStart(candidate) => &candidate.name,
90 ImportCandidate::TraitAssocItem(candidate)
91 | ImportCandidate::TraitMethod(candidate) => &candidate.name,
92 }
93 }
94
95 pub(crate) fn search_for_imports(
96 &self,
97 sema: &Semantics<RootDatabase>,
98 config: &InsertUseConfig,
99 ) -> BTreeSet<hir::ModPath> {
100 let _p = profile::span("import_assists::search_for_imports");
101 self.search_for(sema, Some(config.prefix_kind))
102 }
103
104 /// This may return non-absolute paths if a part of the returned path is already imported into scope.
105 #[allow(dead_code)]
106 pub(crate) fn search_for_relative_paths(
107 &self,
108 sema: &Semantics<RootDatabase>,
109 ) -> BTreeSet<hir::ModPath> {
110 let _p = profile::span("import_assists::search_for_relative_paths");
111 self.search_for(sema, None)
112 }
113
114 fn search_for(
115 &self,
116 sema: &Semantics<RootDatabase>,
117 prefixed: Option<hir::PrefixKind>,
118 ) -> BTreeSet<hir::ModPath> {
119 let db = sema.db;
120 let mut trait_candidates = FxHashSet::default();
121 let current_crate = self.module_with_name_to_import.krate();
122
123 let filter = |candidate: Either<hir::ModuleDef, hir::MacroDef>| {
124 trait_candidates.clear();
125 match &self.import_candidate {
126 ImportCandidate::TraitAssocItem(trait_candidate) => {
127 let located_assoc_item = match candidate {
128 Either::Left(ModuleDef::Function(located_function)) => {
129 located_function.as_assoc_item(db)
130 }
131 Either::Left(ModuleDef::Const(located_const)) => {
132 located_const.as_assoc_item(db)
133 }
134 _ => None,
135 }
136 .map(|assoc| assoc.container(db))
137 .and_then(Self::assoc_to_trait)?;
138
139 trait_candidates.insert(located_assoc_item.into());
140
141 trait_candidate
142 .ty
143 .iterate_path_candidates(
144 db,
145 current_crate,
146 &trait_candidates,
147 None,
148 |_, assoc| Self::assoc_to_trait(assoc.container(db)),
149 )
150 .map(ModuleDef::from)
151 .map(Either::Left)
152 }
153 ImportCandidate::TraitMethod(trait_candidate) => {
154 let located_assoc_item =
155 if let Either::Left(ModuleDef::Function(located_function)) = candidate {
156 located_function
157 .as_assoc_item(db)
158 .map(|assoc| assoc.container(db))
159 .and_then(Self::assoc_to_trait)
160 } else {
161 None
162 }?;
163
164 trait_candidates.insert(located_assoc_item.into());
165
166 trait_candidate
167 .ty
168 .iterate_method_candidates(
169 db,
170 current_crate,
171 &trait_candidates,
172 None,
173 |_, function| {
174 Self::assoc_to_trait(function.as_assoc_item(db)?.container(db))
175 },
176 )
177 .map(ModuleDef::from)
178 .map(Either::Left)
179 }
180 _ => Some(candidate),
181 }
182 };
183
184 imports_locator::find_imports(sema, current_crate, &self.get_search_query())
185 .into_iter()
186 .filter_map(filter)
187 .filter_map(|candidate| {
188 let item: hir::ItemInNs = candidate.either(Into::into, Into::into);
189 if let Some(prefix_kind) = prefixed {
190 self.module_with_name_to_import.find_use_path_prefixed(db, item, prefix_kind)
191 } else {
192 self.module_with_name_to_import.find_use_path(db, item)
193 }
194 })
195 .filter(|use_path| !use_path.segments.is_empty())
196 .take(20)
197 .collect::<BTreeSet<_>>()
198 }
199
200 fn assoc_to_trait(assoc: AssocItemContainer) -> Option<hir::Trait> {
201 if let AssocItemContainer::Trait(extracted_trait) = assoc {
202 Some(extracted_trait)
203 } else {
204 None
205 }
206 }
207}
208
209impl ImportCandidate {
210 fn for_method_call(
211 sema: &Semantics<RootDatabase>,
212 method_call: &ast::MethodCallExpr,
213 ) -> Option<Self> {
214 match sema.resolve_method_call(method_call) {
215 Some(_) => None,
216 None => Some(Self::TraitMethod(TraitImportCandidate {
217 ty: sema.type_of_expr(&method_call.receiver()?)?,
218 name: method_call.name_ref()?.syntax().to_string(),
219 })),
220 }
221 }
222
223 fn for_regular_path(
224 sema: &Semantics<RootDatabase>,
225 path_under_caret: &ast::Path,
226 ) -> Option<Self> {
227 if sema.resolve_path(path_under_caret).is_some() {
228 return None;
229 }
230
231 let segment = path_under_caret.segment()?;
232 let candidate = if let Some(qualifier) = path_under_caret.qualifier() {
233 let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?;
234 let qualifier_start_path =
235 qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?;
236 if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) {
237 let qualifier_resolution = if qualifier_start_path == qualifier {
238 qualifier_start_resolution
239 } else {
240 sema.resolve_path(&qualifier)?
241 };
242 match qualifier_resolution {
243 hir::PathResolution::Def(hir::ModuleDef::Adt(assoc_item_path)) => {
244 ImportCandidate::TraitAssocItem(TraitImportCandidate {
245 ty: assoc_item_path.ty(sema.db),
246 name: segment.syntax().to_string(),
247 })
248 }
249 _ => return None,
250 }
251 } else {
252 ImportCandidate::QualifierStart(PathImportCandidate {
253 name: qualifier_start.syntax().to_string(),
254 })
255 }
256 } else {
257 ImportCandidate::UnqualifiedName(PathImportCandidate {
258 name: segment
259 .syntax()
260 .descendants()
261 .find_map(ast::NameRef::cast)?
262 .syntax()
263 .to_string(),
264 })
265 };
266 Some(candidate)
267 }
268}