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/src/source_binder.rs | |
parent | 16cfc8d50c9b5b4deb1065b9394e7663df7e9500 (diff) |
Move module to SourceBinder
Diffstat (limited to 'crates/ra_hir/src/source_binder.rs')
-rw-r--r-- | crates/ra_hir/src/source_binder.rs | 44 |
1 files changed, 41 insertions, 3 deletions
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 | } | ||