From 9a6c26e34806a05260170029ace4b64adf484a23 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jan 2020 16:53:11 +0100 Subject: Move module to SourceBinder --- crates/ra_hir/src/from_source.rs | 38 ++++++-------------------------- crates/ra_hir/src/source_binder.rs | 44 +++++++++++++++++++++++++++++++++++--- 2 files changed, 48 insertions(+), 34 deletions(-) (limited to 'crates/ra_hir') 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 @@ //! file. use hir_def::{nameres::ModuleSource, ModuleId}; -use hir_expand::name::AsName; use ra_db::FileId; use ra_prof::profile; -use ra_syntax::ast::{self, AstNode, NameOwner}; -use crate::{db::DefDatabase, InFile, Module}; +use crate::{ + db::{DefDatabase, HirDatabase}, + InFile, Module, +}; impl Module { - pub fn from_declaration(db: &impl DefDatabase, src: InFile) -> Option { - let _p = profile("Module::from_declaration"); - let parent_declaration = src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); - - let parent_module = match parent_declaration { - Some(parent_declaration) => { - let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; - Module::from_declaration(db, src_parent) - } - None => { - let source_file = db.parse(src.file_id.original_file(db)).tree(); - let src_parent = - InFile { file_id: src.file_id, value: ModuleSource::SourceFile(source_file) }; - Module::from_definition(db, src_parent) - } - }?; - - let child_name = src.value.name()?.as_name(); - let def_map = db.crate_def_map(parent_module.id.krate); - let child_id = def_map[parent_module.id.local_id].children.get(&child_name)?; - Some(parent_module.with_module_id(*child_id)) - } - - pub fn from_definition(db: &impl DefDatabase, src: InFile) -> Option { + pub fn from_definition(db: &impl HirDatabase, src: InFile) -> Option { let _p = profile("Module::from_definition"); + let mut sb = crate::SourceBinder::new(db); match src.value { ModuleSource::Module(ref module) => { assert!(!module.has_semi()); - return Module::from_declaration( - db, - InFile { file_id: src.file_id, value: module.clone() }, - ); + return sb.to_def(InFile { file_id: src.file_id, value: module.clone() }); } ModuleSource::SourceFile(_) => (), }; 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::{ ConstId, DefWithBodyId, EnumId, EnumVariantId, FunctionId, GenericDefId, ImplId, ModuleId, StaticId, StructFieldId, StructId, TraitId, TypeAliasId, UnionId, VariantId, }; -use hir_expand::{AstId, InFile, MacroDefId, MacroDefKind}; +use hir_expand::{name::AsName, AstId, InFile, MacroDefId, MacroDefKind}; use ra_prof::profile; -use ra_syntax::{ast, match_ast, AstNode, SyntaxNode, TextUnit}; +use ra_syntax::{ + ast::{self, NameOwner}, + match_ast, AstNode, SyntaxNode, TextUnit, +}; use rustc_hash::FxHashMap; -use crate::{db::HirDatabase, Local, ModuleSource, SourceAnalyzer, TypeParam}; +use crate::{db::HirDatabase, Local, Module, ModuleSource, SourceAnalyzer, TypeParam}; pub struct SourceBinder<'a, DB> { pub db: &'a DB, @@ -306,3 +309,38 @@ impl ToDef for ast::TypeParam { Some(TypeParam { id }) } } + +impl ToDef for ast::Module { + type Def = Module; + + fn to_def( + sb: &mut SourceBinder<'_, DB>, + src: InFile, + ) -> Option { + { + let _p = profile("ast::Module::to_def"); + let parent_declaration = + src.value.syntax().ancestors().skip(1).find_map(ast::Module::cast); + + let parent_module = match parent_declaration { + Some(parent_declaration) => { + let src_parent = InFile { file_id: src.file_id, value: parent_declaration }; + sb.to_def(src_parent) + } + None => { + let source_file = sb.db.parse(src.file_id.original_file(sb.db)).tree(); + let src_parent = InFile { + file_id: src.file_id, + value: ModuleSource::SourceFile(source_file), + }; + Module::from_definition(sb.db, src_parent) + } + }?; + + let child_name = src.value.name()?.as_name(); + let def_map = sb.db.crate_def_map(parent_module.id.krate); + let child_id = def_map[parent_module.id.local_id].children.get(&child_name)?; + Some(parent_module.with_module_id(*child_id)) + } + } +} -- cgit v1.2.3