diff options
Diffstat (limited to 'crates/ra_hir/src')
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 38 | ||||
-rw-r--r-- | crates/ra_hir/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 47 |
3 files changed, 28 insertions, 58 deletions
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs deleted file mode 100644 index eb76aecb1..000000000 --- a/crates/ra_hir/src/from_source.rs +++ /dev/null | |||
@@ -1,38 +0,0 @@ | |||
1 | //! Finds a corresponding hir data structure for a syntax node in a specific | ||
2 | //! file. | ||
3 | |||
4 | use hir_def::{nameres::ModuleSource, ModuleId}; | ||
5 | use ra_db::FileId; | ||
6 | use ra_prof::profile; | ||
7 | |||
8 | use crate::{ | ||
9 | db::{DefDatabase, HirDatabase}, | ||
10 | InFile, Module, | ||
11 | }; | ||
12 | |||
13 | impl Module { | ||
14 | pub fn from_definition(db: &impl HirDatabase, src: InFile<ModuleSource>) -> Option<Self> { | ||
15 | let _p = profile("Module::from_definition"); | ||
16 | let mut sb = crate::SourceBinder::new(db); | ||
17 | match src.value { | ||
18 | ModuleSource::Module(ref module) => { | ||
19 | assert!(!module.has_semi()); | ||
20 | return sb.to_def(InFile { file_id: src.file_id, value: module.clone() }); | ||
21 | } | ||
22 | ModuleSource::SourceFile(_) => (), | ||
23 | }; | ||
24 | |||
25 | let original_file = src.file_id.original_file(db); | ||
26 | Module::from_file(db, original_file) | ||
27 | } | ||
28 | |||
29 | fn from_file(db: &impl DefDatabase, file: FileId) -> Option<Self> { | ||
30 | let _p = profile("Module::from_file"); | ||
31 | let (krate, local_id) = db.relevant_crates(file).iter().find_map(|&crate_id| { | ||
32 | let crate_def_map = db.crate_def_map(crate_id); | ||
33 | let local_id = crate_def_map.modules_for_file(file).next()?; | ||
34 | Some((crate_id, local_id)) | ||
35 | })?; | ||
36 | Some(Module { id: ModuleId { krate, local_id } }) | ||
37 | } | ||
38 | } | ||
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs index 11829f42a..e1c7b7a20 100644 --- a/crates/ra_hir/src/lib.rs +++ b/crates/ra_hir/src/lib.rs | |||
@@ -36,7 +36,6 @@ mod from_id; | |||
36 | mod code_model; | 36 | mod code_model; |
37 | 37 | ||
38 | mod has_source; | 38 | mod has_source; |
39 | mod from_source; | ||
40 | 39 | ||
41 | pub use crate::{ | 40 | pub use crate::{ |
42 | code_model::{ | 41 | code_model::{ |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 26eedbb2c..fa225a4ed 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -19,7 +19,8 @@ use ra_syntax::{ | |||
19 | }; | 19 | }; |
20 | use rustc_hash::FxHashMap; | 20 | use rustc_hash::FxHashMap; |
21 | 21 | ||
22 | use crate::{db::HirDatabase, Local, Module, ModuleSource, SourceAnalyzer, TypeParam}; | 22 | use crate::{db::HirDatabase, Local, Module, SourceAnalyzer, TypeParam}; |
23 | use ra_db::FileId; | ||
23 | 24 | ||
24 | pub struct SourceBinder<'a, DB> { | 25 | pub struct SourceBinder<'a, DB> { |
25 | pub db: &'a DB, | 26 | pub db: &'a DB, |
@@ -60,6 +61,16 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> { | |||
60 | T::to_def(self, src) | 61 | T::to_def(self, src) |
61 | } | 62 | } |
62 | 63 | ||
64 | pub fn to_module_def(&mut self, file: FileId) -> Option<Module> { | ||
65 | let _p = profile("SourceBinder::to_module_def"); | ||
66 | let (krate, local_id) = self.db.relevant_crates(file).iter().find_map(|&crate_id| { | ||
67 | let crate_def_map = self.db.crate_def_map(crate_id); | ||
68 | let local_id = crate_def_map.modules_for_file(file).next()?; | ||
69 | Some((crate_id, local_id)) | ||
70 | })?; | ||
71 | Some(Module { id: ModuleId { krate, local_id } }) | ||
72 | } | ||
73 | |||
63 | fn to_id<T: ToId>(&mut self, src: InFile<T>) -> Option<T::ID> { | 74 | fn to_id<T: ToId>(&mut self, src: InFile<T>) -> Option<T::ID> { |
64 | T::to_id(self, src) | 75 | T::to_id(self, src) |
65 | } | 76 | } |
@@ -107,8 +118,7 @@ impl<DB: HirDatabase> SourceBinder<'_, DB> { | |||
107 | return Some(res); | 118 | return Some(res); |
108 | } | 119 | } |
109 | 120 | ||
110 | let module_source = ModuleSource::from_child_node(self.db, src); | 121 | let c = self.to_module_def(src.file_id.original_file(self.db))?; |
111 | let c = crate::Module::from_definition(self.db, src.with_value(module_source))?; | ||
112 | Some(c.id.into()) | 122 | Some(c.id.into()) |
113 | } | 123 | } |
114 | 124 | ||
@@ -248,14 +258,12 @@ impl ToId for ast::MacroCall { | |||
248 | ) -> Option<Self::ID> { | 258 | ) -> Option<Self::ID> { |
249 | let kind = MacroDefKind::Declarative; | 259 | let kind = MacroDefKind::Declarative; |
250 | 260 | ||
251 | let module_src = ModuleSource::from_child_node(sb.db, src.as_ref().map(|it| it.syntax())); | 261 | let krate = sb.to_module_def(src.file_id.original_file(sb.db))?.id.krate; |
252 | let module = crate::Module::from_definition(sb.db, InFile::new(src.file_id, module_src))?; | ||
253 | let krate = Some(module.krate().id); | ||
254 | 262 | ||
255 | let ast_id = | 263 | let ast_id = |
256 | Some(AstId::new(src.file_id, sb.db.ast_id_map(src.file_id).ast_id(&src.value))); | 264 | Some(AstId::new(src.file_id, sb.db.ast_id_map(src.file_id).ast_id(&src.value))); |
257 | 265 | ||
258 | Some(MacroDefId { krate, ast_id, kind }) | 266 | Some(MacroDefId { krate: Some(krate), ast_id, kind }) |
259 | } | 267 | } |
260 | } | 268 | } |
261 | 269 | ||
@@ -319,21 +327,22 @@ impl ToDef for ast::Module { | |||
319 | ) -> Option<Module> { | 327 | ) -> Option<Module> { |
320 | { | 328 | { |
321 | let _p = profile("ast::Module::to_def"); | 329 | let _p = profile("ast::Module::to_def"); |
322 | let parent_declaration = | 330 | let parent_declaration = src |
323 | src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); | 331 | .as_ref() |
332 | .map(|it| it.syntax()) | ||
333 | .cloned() | ||
334 | .ancestors_with_macros(sb.db) | ||
335 | .skip(1) | ||
336 | .find_map(|it| { | ||
337 | let m = ast::Module::cast(it.value.clone())?; | ||
338 | Some(it.with_value(m)) | ||
339 | }); | ||
324 | 340 | ||
325 | let parent_module = match parent_declaration { | 341 | let parent_module = match parent_declaration { |
326 | Some(parent_declaration) => { | 342 | Some(parent_declaration) => sb.to_def(parent_declaration), |
327 | let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; | ||
328 | sb.to_def(src_parent) | ||
329 | } | ||
330 | None => { | 343 | None => { |
331 | let source_file = sb.db.parse(src.file_id.original_file(sb.db)).tree(); | 344 | let file_id = src.file_id.original_file(sb.db); |
332 | let src_parent = InFile { | 345 | sb.to_module_def(file_id) |
333 | file_id: src.file_id, | ||
334 | value: ModuleSource::SourceFile(source_file), | ||
335 | }; | ||
336 | Module::from_definition(sb.db, src_parent) | ||
337 | } | 346 | } |
338 | }?; | 347 | }?; |
339 | 348 | ||