diff options
author | Aleksey Kladov <[email protected]> | 2020-01-16 15:53:11 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-01-16 15:53:11 +0000 |
commit | 9a6c26e34806a05260170029ace4b64adf484a23 (patch) | |
tree | 2fc837f43e859e569b8b156c1d090d1c5fec63c1 /crates/ra_hir | |
parent | 16cfc8d50c9b5b4deb1065b9394e7663df7e9500 (diff) |
Move module to SourceBinder
Diffstat (limited to 'crates/ra_hir')
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 38 | ||||
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 44 |
2 files changed, 48 insertions, 34 deletions
diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index c766c3f0b..eb76aecb1 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs | |||
@@ -2,46 +2,22 @@ | |||
2 | //! file. | 2 | //! file. |
3 | 3 | ||
4 | use hir_def::{nameres::ModuleSource, ModuleId}; | 4 | use hir_def::{nameres::ModuleSource, ModuleId}; |
5 | use hir_expand::name::AsName; | ||
6 | use ra_db::FileId; | 5 | use ra_db::FileId; |
7 | use ra_prof::profile; | 6 | use ra_prof::profile; |
8 | use ra_syntax::ast::{self, AstNode, NameOwner}; | ||
9 | 7 | ||
10 | use crate::{db::DefDatabase, InFile, Module}; | 8 | use crate::{ |
9 | db::{DefDatabase, HirDatabase}, | ||
10 | InFile, Module, | ||
11 | }; | ||
11 | 12 | ||
12 | impl Module { | 13 | impl Module { |
13 | pub fn from_declaration(db: &impl DefDatabase, src: InFile<ast::Module>) -> Option<Self> { | 14 | pub fn from_definition(db: &impl HirDatabase, src: InFile<ModuleSource>) -> Option<Self> { |
14 | let _p = profile("Module::from_declaration"); | ||
15 | let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); | ||
16 | |||
17 | let parent_module = match parent_declaration { | ||
18 | Some(parent_declaration) => { | ||
19 | let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; | ||
20 | Module::from_declaration(db, src_parent) | ||
21 | } | ||
22 | None => { | ||
23 | let source_file = db.parse(src.file_id.original_file(db)).tree(); | ||
24 | let src_parent = | ||
25 | InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) }; | ||
26 | Module::from_definition(db, src_parent) | ||
27 | } | ||
28 | }?; | ||
29 | |||
30 | let child_name = src.value.name()?.as_name(); | ||
31 | let def_map = db.crate_def_map(parent_module.id.krate); | ||
32 | let child_id = def_map[parent_module.id.local_id].children.get(&child_name)?; | ||
33 | Some(parent_module.with_module_id(*child_id)) | ||
34 | } | ||
35 | |||
36 | pub fn from_definition(db: &impl DefDatabase, src: InFile<ModuleSource>) -> Option<Self> { | ||
37 | let _p = profile("Module::from_definition"); | 15 | let _p = profile("Module::from_definition"); |
16 | let mut sb = crate::SourceBinder::new(db); | ||
38 | match src.value { | 17 | match src.value { |
39 | ModuleSource::Module(ref module) => { | 18 | ModuleSource::Module(ref module) => { |
40 | assert!(!module.has_semi()); | 19 | assert!(!module.has_semi()); |
41 | return Module::from_declaration( | 20 | return sb.to_def(InFile { file_id: src.file_id, value: module.clone() }); |
42 | db, | ||
43 | InFile { file_id: src.file_id, value: module.clone() }, | ||
44 | ); | ||
45 | } | 21 | } |
46 | ModuleSource::SourceFile(_) => (), | 22 | ModuleSource::SourceFile(_) => (), |
47 | }; | 23 | }; |
diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 00f48177b..26eedbb2c 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs | |||
@@ -11,12 +11,15 @@ use hir_def::{ | |||
11 | ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, | 11 | ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, |
12 | StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, | 12 | StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, |
13 | }; | 13 | }; |
14 | use hir_expand::{AstId, InFile, MacroDefId, MacroDefKind}; | 14 | use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind}; |
15 | use ra_prof::profile; | 15 | use ra_prof::profile; |
16 | use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, TextUnit}; | 16 | use ra_syntax::{ |
17 | ast::{self, NameOwner}, | ||
18 | match_ast, AstNode, SyntaxNode, TextUnit, | ||
19 | }; | ||
17 | use rustc_hash::FxHashMap; | 20 | use rustc_hash::FxHashMap; |
18 | 21 | ||
19 | use crate::{db::HirDatabase, Local, ModuleSource, SourceAnalyzer, TypeParam}; | 22 | use crate::{db::HirDatabase, Local, Module, ModuleSource, SourceAnalyzer, TypeParam}; |
20 | 23 | ||
21 | pub struct SourceBinder<'a, DB> { | 24 | pub struct SourceBinder<'a, DB> { |
22 | pub db: &'a DB, | 25 | pub db: &'a DB, |
@@ -306,3 +309,38 @@ impl ToDef for ast::TypeParam { | |||
306 | Some(TypeParam { id }) | 309 | Some(TypeParam { id }) |
307 | } | 310 | } |
308 | } | 311 | } |
312 | |||
313 | impl ToDef for ast::Module { | ||
314 | type Def = Module; | ||
315 | |||
316 | fn to_def<DB: HirDatabase>( | ||
317 | sb: &mut SourceBinder<'_, DB>, | ||
318 | src: InFile<ast::Module>, | ||
319 | ) -> Option<Module> { | ||
320 | { | ||
321 | let _p = profile("ast::Module::to_def"); | ||
322 | let parent_declaration = | ||
323 | src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); | ||
324 | |||
325 | let parent_module = match parent_declaration { | ||
326 | Some(parent_declaration) => { | ||
327 | let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; | ||
328 | sb.to_def(src_parent) | ||
329 | } | ||
330 | None => { | ||
331 | let source_file = sb.db.parse(src.file_id.original_file(sb.db)).tree(); | ||
332 | let src_parent = InFile { | ||
333 | file_id: src.file_id, | ||
334 | value: ModuleSource::SourceFile(source_file), | ||
335 | }; | ||
336 | Module::from_definition(sb.db, src_parent) | ||
337 | } | ||
338 | }?; | ||
339 | |||
340 | let child_name = src.value.name()?.as_name(); | ||
341 | let def_map = sb.db.crate_def_map(parent_module.id.krate); | ||
342 | let child_id = def_map[parent_module.id.local_id].children.get(&child_name)?; | ||
343 | Some(parent_module.with_module_id(*child_id)) | ||
344 | } | ||
345 | } | ||
346 | } | ||