diff options
author | Aleksey Kladov <[email protected]> | 2020-02-18 17:35:10 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-02-26 11:55:50 +0000 |
commit | c3a4c4429de83450654795534e64e878a774a088 (patch) | |
tree | 12d89798f61b276f8bd640db07276a7d4e92b1c2 /crates/ra_assists/src/handlers/auto_import.rs | |
parent | 04deae3dba7c9b7054f7a1d64e4b93a05aecc132 (diff) |
Refactor primary IDE API
This introduces the new type -- Semantics.
Semantics maps SyntaxNodes to various semantic info, such as type,
name resolution or macro expansions.
To do so, Semantics maintains a HashMap which maps every node it saw
to the file from which the node originated. This is enough to get all
the necessary hir bits just from syntax.
Diffstat (limited to 'crates/ra_assists/src/handlers/auto_import.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/auto_import.rs | 42 |
1 files changed, 14 insertions, 28 deletions
diff --git a/crates/ra_assists/src/handlers/auto_import.rs b/crates/ra_assists/src/handlers/auto_import.rs index c4aea2a06..edf0cf6d0 100644 --- a/crates/ra_assists/src/handlers/auto_import.rs +++ b/crates/ra_assists/src/handlers/auto_import.rs | |||
@@ -3,8 +3,8 @@ use crate::{ | |||
3 | insert_use_statement, AssistId, | 3 | insert_use_statement, AssistId, |
4 | }; | 4 | }; |
5 | use hir::{ | 5 | use hir::{ |
6 | db::HirDatabase, AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, | 6 | AsAssocItem, AssocItemContainer, ModPath, Module, ModuleDef, PathResolution, Semantics, Trait, |
7 | SourceAnalyzer, Trait, Type, | 7 | Type, |
8 | }; | 8 | }; |
9 | use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase}; | 9 | use ra_ide_db::{imports_locator::ImportsLocator, RootDatabase}; |
10 | use ra_prof::profile; | 10 | use ra_prof::profile; |
@@ -78,14 +78,9 @@ impl AutoImportAssets { | |||
78 | 78 | ||
79 | fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistCtx) -> Option<Self> { | 79 | fn for_method_call(method_call: ast::MethodCallExpr, ctx: &AssistCtx) -> Option<Self> { |
80 | let syntax_under_caret = method_call.syntax().to_owned(); | 80 | let syntax_under_caret = method_call.syntax().to_owned(); |
81 | let source_analyzer = ctx.source_analyzer(&syntax_under_caret, None); | 81 | let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?; |
82 | let module_with_name_to_import = source_analyzer.module()?; | ||
83 | Some(Self { | 82 | Some(Self { |
84 | import_candidate: ImportCandidate::for_method_call( | 83 | import_candidate: ImportCandidate::for_method_call(&ctx.sema, &method_call)?, |
85 | &method_call, | ||
86 | &source_analyzer, | ||
87 | ctx.db, | ||
88 | )?, | ||
89 | module_with_name_to_import, | 84 | module_with_name_to_import, |
90 | syntax_under_caret, | 85 | syntax_under_caret, |
91 | }) | 86 | }) |
@@ -97,14 +92,9 @@ impl AutoImportAssets { | |||
97 | return None; | 92 | return None; |
98 | } | 93 | } |
99 | 94 | ||
100 | let source_analyzer = ctx.source_analyzer(&syntax_under_caret, None); | 95 | let module_with_name_to_import = ctx.sema.scope(&syntax_under_caret).module()?; |
101 | let module_with_name_to_import = source_analyzer.module()?; | ||
102 | Some(Self { | 96 | Some(Self { |
103 | import_candidate: ImportCandidate::for_regular_path( | 97 | import_candidate: ImportCandidate::for_regular_path(&ctx.sema, &path_under_caret)?, |
104 | &path_under_caret, | ||
105 | &source_analyzer, | ||
106 | ctx.db, | ||
107 | )?, | ||
108 | module_with_name_to_import, | 98 | module_with_name_to_import, |
109 | syntax_under_caret, | 99 | syntax_under_caret, |
110 | }) | 100 | }) |
@@ -229,25 +219,23 @@ enum ImportCandidate { | |||
229 | 219 | ||
230 | impl ImportCandidate { | 220 | impl ImportCandidate { |
231 | fn for_method_call( | 221 | fn for_method_call( |
222 | sema: &Semantics<RootDatabase>, | ||
232 | method_call: &ast::MethodCallExpr, | 223 | method_call: &ast::MethodCallExpr, |
233 | source_analyzer: &SourceAnalyzer, | ||
234 | db: &impl HirDatabase, | ||
235 | ) -> Option<Self> { | 224 | ) -> Option<Self> { |
236 | if source_analyzer.resolve_method_call(method_call).is_some() { | 225 | if sema.resolve_method_call(method_call).is_some() { |
237 | return None; | 226 | return None; |
238 | } | 227 | } |
239 | Some(Self::TraitMethod( | 228 | Some(Self::TraitMethod( |
240 | source_analyzer.type_of(db, &method_call.expr()?)?, | 229 | sema.type_of_expr(&method_call.expr()?)?, |
241 | method_call.name_ref()?.syntax().to_string(), | 230 | method_call.name_ref()?.syntax().to_string(), |
242 | )) | 231 | )) |
243 | } | 232 | } |
244 | 233 | ||
245 | fn for_regular_path( | 234 | fn for_regular_path( |
235 | sema: &Semantics<RootDatabase>, | ||
246 | path_under_caret: &ast::Path, | 236 | path_under_caret: &ast::Path, |
247 | source_analyzer: &SourceAnalyzer, | ||
248 | db: &impl HirDatabase, | ||
249 | ) -> Option<Self> { | 237 | ) -> Option<Self> { |
250 | if source_analyzer.resolve_path(db, path_under_caret).is_some() { | 238 | if sema.resolve_path(path_under_caret).is_some() { |
251 | return None; | 239 | return None; |
252 | } | 240 | } |
253 | 241 | ||
@@ -256,17 +244,15 @@ impl ImportCandidate { | |||
256 | let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?; | 244 | let qualifier_start = qualifier.syntax().descendants().find_map(ast::NameRef::cast)?; |
257 | let qualifier_start_path = | 245 | let qualifier_start_path = |
258 | qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?; | 246 | qualifier_start.syntax().ancestors().find_map(ast::Path::cast)?; |
259 | if let Some(qualifier_start_resolution) = | 247 | if let Some(qualifier_start_resolution) = sema.resolve_path(&qualifier_start_path) { |
260 | source_analyzer.resolve_path(db, &qualifier_start_path) | ||
261 | { | ||
262 | let qualifier_resolution = if qualifier_start_path == qualifier { | 248 | let qualifier_resolution = if qualifier_start_path == qualifier { |
263 | qualifier_start_resolution | 249 | qualifier_start_resolution |
264 | } else { | 250 | } else { |
265 | source_analyzer.resolve_path(db, &qualifier)? | 251 | sema.resolve_path(&qualifier)? |
266 | }; | 252 | }; |
267 | if let PathResolution::Def(ModuleDef::Adt(assoc_item_path)) = qualifier_resolution { | 253 | if let PathResolution::Def(ModuleDef::Adt(assoc_item_path)) = qualifier_resolution { |
268 | Some(ImportCandidate::TraitAssocItem( | 254 | Some(ImportCandidate::TraitAssocItem( |
269 | assoc_item_path.ty(db), | 255 | assoc_item_path.ty(sema.db), |
270 | segment.syntax().to_string(), | 256 | segment.syntax().to_string(), |
271 | )) | 257 | )) |
272 | } else { | 258 | } else { |