From 9c7a2aef30cb35347af646dea5a5611af1224676 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 15 Nov 2019 10:26:31 +0300 Subject: [PATCH 1/2] Refactor Module::from_source to properly descend from root file --- crates/ra_hir/src/code_model.rs | 2 +- crates/ra_hir/src/from_source.rs | 68 +++++++++++++++++--------------- crates/ra_hir_def/src/nameres.rs | 7 ++++ 3 files changed, 44 insertions(+), 33 deletions(-) diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 962d5a8c15..dd43271f4c 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs @@ -157,7 +157,7 @@ impl Module { } /// Finds a child module with the specified name. - pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option { + pub fn child(self, db: &impl DefDatabase, name: &Name) -> Option { let def_map = db.crate_def_map(self.id.krate); let child_id = def_map[self.id.module_id].children.get(name)?; Some(self.with_module_id(*child_id)) diff --git a/crates/ra_hir/src/from_source.rs b/crates/ra_hir/src/from_source.rs index 9793af8587..7e5523c54a 100644 --- a/crates/ra_hir/src/from_source.rs +++ b/crates/ra_hir/src/from_source.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use hir_def::{StructId, StructOrUnionId, UnionId}; +use hir_def::{ModuleId, StructId, StructOrUnionId, UnionId}; use hir_expand::name::AsName; use ra_syntax::{ ast::{self, AstNode, NameOwner}, @@ -10,9 +10,9 @@ use ra_syntax::{ use crate::{ db::{AstDatabase, DefDatabase, HirDatabase}, ids::{AstItemDef, LocationCtx}, - AstId, Const, Crate, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource, - ImplBlock, Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, - Union, VariantDef, + Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource, ImplBlock, + Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union, + VariantDef, }; pub trait FromSource: Sized { @@ -152,44 +152,48 @@ impl Local { } impl Module { - pub fn from_declaration(db: &impl HirDatabase, src: Source) -> Option { - let src_parent = Source { - file_id: src.file_id, - ast: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), - }; - let parent_module = Module::from_definition(db, src_parent)?; + pub fn from_declaration(db: &impl DefDatabase, src: Source) -> Option { + let parent_declaration = src.ast.syntax().ancestors().skip(1).find_map(ast::Module::cast); + + let parent_module = match parent_declaration { + Some(parent_declaration) => { + let src_parent = Source { file_id: src.file_id, ast: parent_declaration }; + Module::from_declaration(db, src_parent) + } + _ => { + let src_parent = Source { + file_id: src.file_id, + ast: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), + }; + Module::from_definition(db, src_parent) + } + }?; + let child_name = src.ast.name()?; parent_module.child(db, &child_name.as_name()) } - pub fn from_definition( - db: &(impl DefDatabase + AstDatabase), - src: Source, - ) -> Option { - let decl_id = match src.ast { + pub fn from_definition(db: &impl DefDatabase, src: Source) -> Option { + match src.ast { ModuleSource::Module(ref module) => { assert!(!module.has_semi()); - let ast_id_map = db.ast_id_map(src.file_id); - let item_id = AstId::new(src.file_id, ast_id_map.ast_id(module)); - Some(item_id) + return Module::from_declaration( + db, + Source { file_id: src.file_id, ast: module.clone() }, + ); } - ModuleSource::SourceFile(_) => None, + ModuleSource::SourceFile(_) => (), }; - db.relevant_crates(src.file_id.original_file(db)).iter().find_map(|&crate_id| { - let def_map = db.crate_def_map(crate_id); + let original_file = src.file_id.original_file(db); - let (module_id, _module_data) = - def_map.modules.iter().find(|(_module_id, module_data)| { - if decl_id.is_some() { - module_data.declaration == decl_id - } else { - module_data.definition.map(|it| it.into()) == Some(src.file_id) - } - })?; - - Some(Module::new(Crate { crate_id }, module_id)) - }) + let (krate, module_id) = + db.relevant_crates(original_file).iter().find_map(|&crate_id| { + let crate_def_map = db.crate_def_map(crate_id); + let local_module_id = crate_def_map.modules_for_file(original_file).next()?; + Some((crate_id, local_module_id)) + })?; + Some(Module { id: ModuleId { krate, module_id } }) } } diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index d3ecabb9be..1331fbe307 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -258,6 +258,13 @@ impl CrateDefMap { let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); (res.resolved_def, res.segment_index) } + + pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator + '_ { + self.modules + .iter() + .filter(move |(_id, data)| data.definition == Some(file_id)) + .map(|(id, _data)| id) + } } mod diagnostics { From a28907af8ca93077601053cf168ac2429855c394 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 15 Nov 2019 10:42:54 +0300 Subject: [PATCH 2/2] Privatize modules --- crates/ra_hir/src/test_db.rs | 2 +- crates/ra_hir_def/src/nameres.rs | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/ra_hir/src/test_db.rs b/crates/ra_hir/src/test_db.rs index 5237b303af..1caa2e8759 100644 --- a/crates/ra_hir/src/test_db.rs +++ b/crates/ra_hir/src/test_db.rs @@ -81,7 +81,7 @@ impl TestDB { let crate_graph = self.crate_graph(); for krate in crate_graph.iter().next() { let crate_def_map = self.crate_def_map(krate); - for (module_id, _) in crate_def_map.modules.iter() { + for module_id in crate_def_map.modules() { let module_id = ModuleId { krate, module_id }; let module = crate::Module::from(module_id); module.diagnostics( diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs index 1331fbe307..5fc5921500 100644 --- a/crates/ra_hir_def/src/nameres.rs +++ b/crates/ra_hir_def/src/nameres.rs @@ -87,7 +87,7 @@ pub struct CrateDefMap { prelude: Option, extern_prelude: FxHashMap, root: CrateModuleId, - pub modules: Arena, + modules: Arena, /// Some macros are not well-behavior, which leads to infinite loop /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } } @@ -259,6 +259,10 @@ impl CrateDefMap { (res.resolved_def, res.segment_index) } + pub fn modules(&self) -> impl Iterator + '_ { + self.modules.iter().map(|(id, _data)| id) + } + pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator + '_ { self.modules .iter()