From 2195d1db6d70d64383bec82819fab02891d09744 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 13 Mar 2019 16:38:02 +0300 Subject: Replace module_tree with CrateDefMap --- crates/ra_hir/src/code_model_impl/krate.rs | 4 +- crates/ra_hir/src/code_model_impl/module.rs | 97 ++++++++++++++++++++--------- 2 files changed, 67 insertions(+), 34 deletions(-) (limited to 'crates/ra_hir/src/code_model_impl') diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs index 161ae6e18..cc87c6f14 100644 --- a/crates/ra_hir/src/code_model_impl/krate.rs +++ b/crates/ra_hir/src/code_model_impl/krate.rs @@ -18,9 +18,7 @@ impl Crate { .collect() } pub(crate) fn root_module_impl(&self, db: &impl PersistentHirDatabase) -> Option { - let module_tree = db.module_tree(*self); - let module_id = module_tree.modules().next()?; - + let module_id = db.crate_def_map(*self).root(); let module = Module { krate: *self, module_id }; Some(module) } diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs index 437f96942..5d8f738b5 100644 --- a/crates/ra_hir/src/code_model_impl/module.rs +++ b/crates/ra_hir/src/code_model_impl/module.rs @@ -1,33 +1,62 @@ -use ra_syntax::{ast, SyntaxNode, TreeArc}; +use ra_db::FileId; +use ra_syntax::{ast, SyntaxNode, TreeArc, AstNode}; use crate::{ - Module, ModuleSource, Problem, - Name, - module_tree::ModuleId, + Module, ModuleSource, Problem, Name, + nameres::crate_def_map::ModuleId, nameres::lower::ImportId, HirDatabase, PersistentHirDatabase, - HirFileId + HirFileId, SourceItemId, }; +impl ModuleSource { + pub(crate) fn new( + db: &impl PersistentHirDatabase, + file_id: Option, + decl_id: Option, + ) -> ModuleSource { + match (file_id, decl_id) { + (Some(file_id), _) => { + let source_file = db.parse(file_id); + ModuleSource::SourceFile(source_file) + } + (None, Some(item_id)) => { + let module = db.file_item(item_id); + let module = ast::Module::cast(&*module).unwrap(); + assert!(module.item_list().is_some(), "expected inline module"); + ModuleSource::Module(module.to_owned()) + } + (None, None) => panic!(), + } + } +} + impl Module { fn with_module_id(&self, module_id: ModuleId) -> Module { Module { module_id, krate: self.krate } } pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Option { - let module_tree = db.module_tree(self.krate); - let link = self.module_id.parent_link(&module_tree)?; - Some(link.name(&module_tree).clone()) + let def_map = db.crate_def_map(self.krate); + let parent = def_map[self.module_id].parent?; + def_map[parent].children.iter().find_map(|(name, module_id)| { + if *module_id == self.module_id { + Some(name.clone()) + } else { + None + } + }) } pub(crate) fn definition_source_impl( &self, db: &impl PersistentHirDatabase, ) -> (HirFileId, ModuleSource) { - let module_tree = db.module_tree(self.krate); - let file_id = self.module_id.file_id(&module_tree); - let decl_id = self.module_id.decl_id(&module_tree); + let def_map = db.crate_def_map(self.krate); + let decl_id = def_map[self.module_id].declaration; + let file_id = def_map[self.module_id].definition; let module_source = ModuleSource::new(db, file_id, decl_id); + let file_id = file_id.map(HirFileId::from).unwrap_or_else(|| decl_id.unwrap().file_id); (file_id, module_source) } @@ -35,11 +64,11 @@ impl Module { &self, db: &impl HirDatabase, ) -> Option<(HirFileId, TreeArc)> { - let module_tree = db.module_tree(self.krate); - let link = self.module_id.parent_link(&module_tree)?; - let file_id = link.owner(&module_tree).file_id(&module_tree); - let src = link.source(&module_tree, db); - Some((file_id, src)) + let def_map = db.crate_def_map(self.krate); + let decl = def_map[self.module_id].declaration?; + let syntax_node = db.file_item(decl); + let ast = ast::Module::cast(&syntax_node).unwrap().to_owned(); + Some((decl.file_id, ast)) } pub(crate) fn import_source_impl( @@ -53,16 +82,15 @@ impl Module { } pub(crate) fn crate_root_impl(&self, db: &impl PersistentHirDatabase) -> Module { - let module_tree = db.module_tree(self.krate); - let module_id = self.module_id.crate_root(&module_tree); - self.with_module_id(module_id) + let def_map = db.crate_def_map(self.krate); + self.with_module_id(def_map.root()) } /// Finds a child module with the specified name. pub(crate) fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Option { - let module_tree = db.module_tree(self.krate); - let child_id = self.module_id.child(&module_tree, name)?; - Some(self.with_module_id(child_id)) + let def_map = db.crate_def_map(self.krate); + let child_id = def_map[self.module_id].children.get(name)?; + Some(self.with_module_id(*child_id)) } /// Iterates over all child modules. @@ -70,18 +98,18 @@ impl Module { &self, db: &impl PersistentHirDatabase, ) -> impl Iterator { - let module_tree = db.module_tree(self.krate); - let children = self - .module_id - .children(&module_tree) - .map(|(_, module_id)| self.with_module_id(module_id)) + let def_map = db.crate_def_map(self.krate); + let children = def_map[self.module_id] + .children + .iter() + .map(|(_, module_id)| self.with_module_id(*module_id)) .collect::>(); children.into_iter() } pub(crate) fn parent_impl(&self, db: &impl PersistentHirDatabase) -> Option { - let module_tree = db.module_tree(self.krate); - let parent_id = self.module_id.parent(&module_tree)?; + let def_map = db.crate_def_map(self.krate); + let parent_id = def_map[self.module_id].parent?; Some(self.with_module_id(parent_id)) } @@ -89,7 +117,14 @@ impl Module { &self, db: &impl HirDatabase, ) -> Vec<(TreeArc, Problem)> { - let module_tree = db.module_tree(self.krate); - self.module_id.problems(&module_tree, db) + let def_map = db.crate_def_map(self.krate); + let (my_file_id, _) = self.definition_source(db); + // FIXME: not entirely corret filterint by module + def_map + .problems() + .iter() + .filter(|(source_item_id, _problem)| my_file_id == source_item_id.file_id) + .map(|(source_item_id, problem)| (db.file_item(*source_item_id), problem.clone())) + .collect() } } -- cgit v1.2.3