diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/assists/src/handlers/auto_import.rs | 269 | ||||
-rw-r--r-- | crates/assists/src/utils.rs | 1 | ||||
-rw-r--r-- | crates/assists/src/utils/import_assets.rs | 268 | ||||
-rw-r--r-- | crates/base_db/Cargo.toml | 2 |
4 files changed, 298 insertions, 242 deletions
diff --git a/crates/assists/src/handlers/auto_import.rs b/crates/assists/src/handlers/auto_import.rs index d3ee98e5f..e595b5b93 100644 --- a/crates/assists/src/handlers/auto_import.rs +++ b/crates/assists/src/handlers/auto_import.rs | |||
@@ -1,21 +1,9 @@ | |||
1 | use std::collections::BTreeSet; | 1 | use syntax::ast; |
2 | |||
3 | use either::Either; | ||
4 | use hir::{ | ||
5 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, | ||
6 | Type, | ||
7 | }; | ||
8 | use ide_db::{imports_locator, RootDatabase}; | ||
9 | use insert_use::ImportScope; | ||
10 | use rustc_hash::FxHashSet; | ||
11 | use syntax::{ | ||
12 | ast::{self, AstNode}, | ||
13 | SyntaxNode, | ||
14 | }; | ||
15 | 2 | ||
16 | use crate::{ | 3 | use crate::{ |
17 | utils::insert_use, utils::mod_path_to_ast, AssistContext, AssistId, AssistKind, Assists, | 4 | utils::import_assets::{ImportAssets, ImportCandidate}, |
18 | GroupLabel, | 5 | utils::{insert_use, mod_path_to_ast, ImportScope}, |
6 | AssistContext, AssistId, AssistKind, Assists, GroupLabel, | ||
19 | }; | 7 | }; |
20 | 8 | ||
21 | // Assist: auto_import | 9 | // Assist: auto_import |
@@ -38,16 +26,24 @@ use crate::{ | |||
38 | // # pub mod std { pub mod collections { pub struct HashMap { } } } | 26 | // # pub mod std { pub mod collections { pub struct HashMap { } } } |
39 | // ``` | 27 | // ``` |
40 | pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 28 | pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
41 | let auto_import_assets = AutoImportAssets::new(ctx)?; | 29 | let import_assets = |
42 | let proposed_imports = auto_import_assets.search_for_imports(ctx); | 30 | if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() { |
31 | ImportAssets::for_regular_path(path_under_caret, &ctx.sema) | ||
32 | } else if let Some(method_under_caret) = | ||
33 | ctx.find_node_at_offset_with_descend::<ast::MethodCallExpr>() | ||
34 | { | ||
35 | ImportAssets::for_method_call(method_under_caret, &ctx.sema) | ||
36 | } else { | ||
37 | None | ||
38 | }?; | ||
39 | let proposed_imports = import_assets.search_for_imports(&ctx.sema, &ctx.config.insert_use); | ||
43 | if proposed_imports.is_empty() { | 40 | if proposed_imports.is_empty() { |
44 | return None; | 41 | return None; |
45 | } | 42 | } |
46 | 43 | ||
47 | let range = ctx.sema.original_range(&auto_import_assets.syntax_under_caret).range; | 44 | let range = ctx.sema.original_range(import_assets.syntax_under_caret()).range; |
48 | let group = auto_import_assets.get_import_group_message(); | 45 | let group = import_group_message(import_assets.import_candidate()); |
49 | let scope = | 46 | let scope = ImportScope::find_insert_use_container(import_assets.syntax_under_caret(), ctx)?; |
50 | ImportScope::find_insert_use_container(&auto_import_assets.syntax_under_caret, ctx)?; | ||
51 | let syntax = scope.as_syntax_node(); | 47 | let syntax = scope.as_syntax_node(); |
52 | for import in proposed_imports { | 48 | for import in proposed_imports { |
53 | acc.add_group( | 49 | acc.add_group( |
@@ -65,227 +61,18 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext) -> Option<()> | |||
65 | Some(()) | 61 | Some(()) |
66 | } | 62 | } |
67 | 63 | ||
68 | #[derive(Debug)] | 64 | fn import_group_message(import_candidate: &ImportCandidate) -> GroupLabel { |
69 | struct AutoImportAssets { | 65 | let name = match import_candidate { |
70 | import_candidate: ImportCandidate, | 66 | ImportCandidate::UnqualifiedName(candidate) |
71 | module_with_name_to_import: Module, | 67 | | ImportCandidate::QualifierStart(candidate) => format!("Import {}", &candidate.name), |
72 | syntax_under_caret: SyntaxNode, | 68 | ImportCandidate::TraitAssocItem(candidate) => { |
73 | } | 69 | format!("Import a trait for item {}", &candidate.name) |
74 | |||
75 | impl AutoImportAssets { | ||
76 | fn new(ctx: &AssistContext) -> Option<Self> { | ||
77 | if let Some(path_under_caret) = ctx.find_node_at_offset_with_descend::<ast::Path>() { | ||
78 | Self::for_regular_path(path_under_caret, &ctx) | ||
79 | } else { | ||
80 | Self::for_method_call(ctx.find_node_at_offset_with_descend()?, &ctx) | ||
81 | } | ||
82 | } | ||
83 | |||
84 | fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistContext) -> Option<Self> { | ||
85 | let syntax_under_caret = method_call.syntax().to_owned(); | ||
86 | let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?; | ||
87 | Some(Self { | ||
88 | import_candidate: ImportCandidate::for_method_call(&ctx.sema, &method_call)?, | ||
89 | module_with_name_to_import, | ||
90 | syntax_under_caret, | ||
91 | }) | ||
92 | } | ||
93 | |||
94 | fn for_regular_path(path_under_caret: ast::Path, ctx: &AssistContext) -> Option<Self> { | ||
95 | let syntax_under_caret = path_under_caret.syntax().to_owned(); | ||
96 | if syntax_under_caret.ancestors().find_map(ast::Use::cast).is_some() { | ||
97 | return None; | ||
98 | } | ||
99 | |||
100 | let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?; | ||
101 | Some(Self { | ||
102 | import_candidate: ImportCandidate::for_regular_path(&ctx.sema, &path_under_caret)?, | ||
103 | module_with_name_to_import, | ||
104 | syntax_under_caret, | ||
105 | }) | ||
106 | } | ||
107 | |||
108 | fn get_search_query(&self) -> &str { | ||
109 | match &self.import_candidate { | ||
110 | ImportCandidate::UnqualifiedName(name) => name, | ||
111 | ImportCandidate::QualifierStart(qualifier_start) => qualifier_start, | ||
112 | ImportCandidate::TraitAssocItem(_, trait_assoc_item_name) => trait_assoc_item_name, | ||
113 | ImportCandidate::TraitMethod(_, trait_method_name) => trait_method_name, | ||
114 | } | ||
115 | } | ||
116 | |||
117 | fn get_import_group_message(&self) -> GroupLabel { | ||
118 | let name = match &self.import_candidate { | ||
119 | ImportCandidate::UnqualifiedName(name) => format!("Import {}", name), | ||
120 | ImportCandidate::QualifierStart(qualifier_start) => { | ||
121 | format!("Import {}", qualifier_start) | ||
122 | } | ||
123 | ImportCandidate::TraitAssocItem(_, trait_assoc_item_name) => { | ||
124 | format!("Import a trait for item {}", trait_assoc_item_name) | ||
125 | } | ||
126 | ImportCandidate::TraitMethod(_, trait_method_name) => { | ||
127 | format!("Import a trait for method {}", trait_method_name) | ||
128 | } | ||
129 | }; | ||
130 | GroupLabel(name) | ||
131 | } | ||
132 | |||
133 | fn search_for_imports(&self, ctx: &AssistContext) -> BTreeSet<ModPath> { | ||
134 | let _p = profile::span("auto_import::search_for_imports"); | ||
135 | let db = ctx.db(); | ||
136 | let current_crate = self.module_with_name_to_import.krate(); | ||
137 | imports_locator::find_imports(&ctx.sema, current_crate, &self.get_search_query()) | ||
138 | .into_iter() | ||
139 | .filter_map(|candidate| match &self.import_candidate { | ||
140 | ImportCandidate::TraitAssocItem(assoc_item_type, _) => { | ||
141 | let located_assoc_item = match candidate { | ||
142 | Either::Left(ModuleDef::Function(located_function)) => located_function | ||
143 | .as_assoc_item(db) | ||
144 | .map(|assoc| assoc.container(db)) | ||
145 | .and_then(Self::assoc_to_trait), | ||
146 | Either::Left(ModuleDef::Const(located_const)) => located_const | ||
147 | .as_assoc_item(db) | ||
148 | .map(|assoc| assoc.container(db)) | ||
149 | .and_then(Self::assoc_to_trait), | ||
150 | _ => None, | ||
151 | }?; | ||
152 | |||
153 | let mut trait_candidates = FxHashSet::default(); | ||
154 | trait_candidates.insert(located_assoc_item.into()); | ||
155 | |||
156 | assoc_item_type | ||
157 | .iterate_path_candidates( | ||
158 | db, | ||
159 | current_crate, | ||
160 | &trait_candidates, | ||
161 | None, | ||
162 | |_, assoc| Self::assoc_to_trait(assoc.container(db)), | ||
163 | ) | ||
164 | .map(ModuleDef::from) | ||
165 | .map(Either::Left) | ||
166 | } | ||
167 | ImportCandidate::TraitMethod(function_callee, _) => { | ||
168 | let located_assoc_item = | ||
169 | if let Either::Left(ModuleDef::Function(located_function)) = candidate { | ||
170 | located_function | ||
171 | .as_assoc_item(db) | ||
172 | .map(|assoc| assoc.container(db)) | ||
173 | .and_then(Self::assoc_to_trait) | ||
174 | } else { | ||
175 | None | ||
176 | }?; | ||
177 | |||
178 | let mut trait_candidates = FxHashSet::default(); | ||
179 | trait_candidates.insert(located_assoc_item.into()); | ||
180 | |||
181 | function_callee | ||
182 | .iterate_method_candidates( | ||
183 | db, | ||
184 | current_crate, | ||
185 | &trait_candidates, | ||
186 | None, | ||
187 | |_, function| { | ||
188 | Self::assoc_to_trait(function.as_assoc_item(db)?.container(db)) | ||
189 | }, | ||
190 | ) | ||
191 | .map(ModuleDef::from) | ||
192 | .map(Either::Left) | ||
193 | } | ||
194 | _ => Some(candidate), | ||
195 | }) | ||
196 | .filter_map(|candidate| match candidate { | ||
197 | Either::Left(module_def) => self.module_with_name_to_import.find_use_path_prefixed( | ||
198 | db, | ||
199 | module_def, | ||
200 | ctx.config.insert_use.prefix_kind, | ||
201 | ), | ||
202 | Either::Right(macro_def) => self.module_with_name_to_import.find_use_path_prefixed( | ||
203 | db, | ||
204 | macro_def, | ||
205 | ctx.config.insert_use.prefix_kind, | ||
206 | ), | ||
207 | }) | ||
208 | .filter(|use_path| !use_path.segments.is_empty()) | ||
209 | .take(20) | ||
210 | .collect::<BTreeSet<_>>() | ||
211 | } | ||
212 | |||
213 | fn assoc_to_trait(assoc: AssocItemContainer) -> Option<Trait> { | ||
214 | if let AssocItemContainer::Trait(extracted_trait) = assoc { | ||
215 | Some(extracted_trait) | ||
216 | } else { | ||
217 | None | ||
218 | } | ||
219 | } | ||
220 | } | ||
221 | |||
222 | #[derive(Debug)] | ||
223 | enum ImportCandidate { | ||
224 | /// Simple name like 'HashMap' | ||
225 | UnqualifiedName(String), | ||
226 | /// First part of the qualified name. | ||
227 | /// For 'std::collections::HashMap', that will be 'std'. | ||
228 | QualifierStart(String), | ||
229 | /// A trait associated function (with no self parameter) or associated constant. | ||
230 | /// For 'test_mod::TestEnum::test_function', `Type` is the `test_mod::TestEnum` expression type | ||
231 | /// and `String` is the `test_function` | ||
232 | TraitAssocItem(Type, String), | ||
233 | /// A trait method with self parameter. | ||
234 | /// For 'test_enum.test_method()', `Type` is the `test_enum` expression type | ||
235 | /// and `String` is the `test_method` | ||
236 | TraitMethod(Type, String), | ||
237 | } | ||
238 | |||
239 | impl ImportCandidate { | ||
240 | fn for_method_call( | ||
241 | sema: &Semantics<RootDatabase>, | ||
242 | method_call: &ast::MethodCallExpr, | ||
243 | ) -> Option<Self> { | ||
244 | if sema.resolve_method_call(method_call).is_some() { | ||
245 | return None; | ||
246 | } | ||
247 | Some(Self::TraitMethod( | ||
248 | sema.type_of_expr(&method_call.receiver()?)?, | ||
249 | method_call.name_ref()?.syntax().to_string(), | ||
250 | )) | ||
251 | } | ||
252 | |||
253 | fn for_regular_path( | ||
254 | sema: &Semantics<RootDatabase>, | ||
255 | path_under_caret: &ast::Path, | ||
256 | ) -> Option<Self> { | ||
257 | if sema.resolve_path(path_under_caret).is_some() { | ||
258 | return None; | ||
259 | } | 70 | } |
260 | 71 | ImportCandidate::TraitMethod(candidate) => { | |
261 | let segment = path_under_caret.segment()?; | 72 | format!("Import a trait for method {}", &candidate.name) |
262 | if let Some(qualifier) = path_under_caret.qualifier() { | ||
263 | let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?; | ||
264 | let qualifier_start_path = | ||
265 | qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?; | ||
266 | if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) { | ||
267 | let qualifier_resolution = if qualifier_start_path == qualifier { | ||
268 | qualifier_start_resolution | ||
269 | } else { | ||
270 | sema.resolve_path(&qualifier)? | ||
271 | }; | ||
272 | if let PathResolution::Def(ModuleDef::Adt(assoc_item_path)) = qualifier_resolution { | ||
273 | Some(ImportCandidate::TraitAssocItem( | ||
274 | assoc_item_path.ty(sema.db), | ||
275 | segment.syntax().to_string(), | ||
276 | )) | ||
277 | } else { | ||
278 | None | ||
279 | } | ||
280 | } else { | ||
281 | Some(ImportCandidate::QualifierStart(qualifier_start.syntax().to_string())) | ||
282 | } | ||
283 | } else { | ||
284 | Some(ImportCandidate::UnqualifiedName( | ||
285 | segment.syntax().descendants().find_map(ast::NameRef::cast)?.syntax().to_string(), | ||
286 | )) | ||
287 | } | 73 | } |
288 | } | 74 | }; |
75 | GroupLabel(name) | ||
289 | } | 76 | } |
290 | 77 | ||
291 | #[cfg(test)] | 78 | #[cfg(test)] |
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index c1847f601..b37b0d2b6 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs | |||
@@ -1,5 +1,6 @@ | |||
1 | //! Assorted functions shared by several assists. | 1 | //! Assorted functions shared by several assists. |
2 | pub(crate) mod insert_use; | 2 | pub(crate) mod insert_use; |
3 | pub(crate) mod import_assets; | ||
3 | 4 | ||
4 | use std::{iter, ops}; | 5 | use std::{iter, ops}; |
5 | 6 | ||
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. | ||
2 | use std::collections::BTreeSet; | ||
3 | |||
4 | use either::Either; | ||
5 | use hir::{AsAssocItem, AssocItemContainer, ModuleDef, Semantics}; | ||
6 | use ide_db::{imports_locator, RootDatabase}; | ||
7 | use rustc_hash::FxHashSet; | ||
8 | use syntax::{ast, AstNode, SyntaxNode}; | ||
9 | |||
10 | use crate::assist_config::InsertUseConfig; | ||
11 | |||
12 | #[derive(Debug)] | ||
13 | pub(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)] | ||
30 | pub(crate) struct TraitImportCandidate { | ||
31 | pub ty: hir::Type, | ||
32 | pub name: String, | ||
33 | } | ||
34 | |||
35 | #[derive(Debug)] | ||
36 | pub(crate) struct PathImportCandidate { | ||
37 | pub name: String, | ||
38 | } | ||
39 | |||
40 | #[derive(Debug)] | ||
41 | pub(crate) struct ImportAssets { | ||
42 | import_candidate: ImportCandidate, | ||
43 | module_with_name_to_import: hir::Module, | ||
44 | syntax_under_caret: SyntaxNode, | ||
45 | } | ||
46 | |||
47 | impl 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 | |||
209 | impl 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 | } | ||
diff --git a/crates/base_db/Cargo.toml b/crates/base_db/Cargo.toml index f7bfcb0d7..1724d2f85 100644 --- a/crates/base_db/Cargo.toml +++ b/crates/base_db/Cargo.toml | |||
@@ -10,7 +10,7 @@ edition = "2018" | |||
10 | doctest = false | 10 | doctest = false |
11 | 11 | ||
12 | [dependencies] | 12 | [dependencies] |
13 | salsa = "0.15.2" | 13 | salsa = "0.16.0" |
14 | rustc-hash = "1.1.0" | 14 | rustc-hash = "1.1.0" |
15 | 15 | ||
16 | syntax = { path = "../syntax", version = "0.0.0" } | 16 | syntax = { path = "../syntax", version = "0.0.0" } |