From 8c4d2770362f3c2950f110f8e116ed8f537ec1a1 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 5 Jan 2019 12:13:16 +0300 Subject: switch source-binders to Module --- crates/ra_hir/src/code_model_impl.rs | 3 +-- crates/ra_hir/src/source_binder.rs | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 9 deletions(-) (limited to 'crates/ra_hir/src') diff --git a/crates/ra_hir/src/code_model_impl.rs b/crates/ra_hir/src/code_model_impl.rs index 22079ba33..659af548c 100644 --- a/crates/ra_hir/src/code_model_impl.rs +++ b/crates/ra_hir/src/code_model_impl.rs @@ -4,7 +4,6 @@ use crate::{HirFileId, db::HirDatabase, Crate, CrateDependency, AsName, DefId, D use crate::code_model_api::Module; - impl Crate { pub(crate) fn new(crate_id: CrateId) -> Crate { Crate { crate_id } @@ -45,7 +44,7 @@ impl Crate { } impl Module { - fn new(def_id: DefId) -> Self { + pub(crate) fn new(def_id: DefId) -> Self { crate::code_model_api::Module { def_id } } diff --git a/crates/ra_hir/src/source_binder.rs b/crates/ra_hir/src/source_binder.rs index 29a3960e9..ac097e81a 100644 --- a/crates/ra_hir/src/source_binder.rs +++ b/crates/ra_hir/src/source_binder.rs @@ -13,11 +13,13 @@ use ra_syntax::{ }; use crate::{ - HirDatabase, Module, Function, SourceItemId, + HirDatabase, Function, SourceItemId, module::ModuleSource, DefKind, DefLoc, AsName, }; +use crate::code_model_api::Module; + /// Locates the module by `FileId`. Picks topmost module in the file. pub fn module_from_file_id(db: &impl HirDatabase, file_id: FileId) -> Cancelable> { let module_source = ModuleSource::new_file(file_id.into()); @@ -34,7 +36,7 @@ pub fn module_from_declaration( let child_name = decl.name(); match (parent_module, child_name) { (Some(parent_module), Some(child_name)) => { - if let Some(child) = parent_module.child(&child_name.as_name()) { + if let Some(child) = parent_module.child(db, &child_name.as_name())? { return Ok(Some(child)); } } @@ -84,7 +86,15 @@ fn module_from_source( .modules_with_sources() .find(|(_id, src)| src == &module_source); let module_id = ctry!(m).0; - Ok(Some(Module::new(db, source_root_id, module_id)?)) + let def_loc = DefLoc { + kind: DefKind::Module, + source_root_id, + module_id, + source_item_id: module_source.0, + }; + let def_id = def_loc.id(db); + + Ok(Some(Module::new(def_id))) } pub fn function_from_position( @@ -114,7 +124,8 @@ pub fn function_from_module( module: &Module, fn_def: ast::FnDef, ) -> Function { - let file_id = module.source().file_id(); + let loc = module.def_id.loc(db); + let file_id = loc.source_item_id.file_id; let file_items = db.file_items(file_id); let item_id = file_items.id_of(file_id, fn_def.syntax()); let source_item_id = SourceItemId { @@ -123,8 +134,8 @@ pub fn function_from_module( }; let def_loc = DefLoc { kind: DefKind::Function, - source_root_id: module.source_root_id, - module_id: module.module_id, + source_root_id: loc.source_root_id, + module_id: loc.module_id, source_item_id, }; Function::new(def_loc.id(db)) @@ -147,7 +158,8 @@ pub fn macro_symbols( Some(it) => it, None => return Ok(Vec::new()), }; - let items = db.input_module_items(module.source_root_id, module.module_id)?; + let loc = module.def_id.loc(db); + let items = db.input_module_items(loc.source_root_id, loc.module_id)?; let mut res = Vec::new(); for macro_call_id in items -- cgit v1.2.3