aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-11-15 07:26:31 +0000
committerAleksey Kladov <[email protected]>2019-11-15 07:26:31 +0000
commit9c7a2aef30cb35347af646dea5a5611af1224676 (patch)
tree6134c1475e717ef72cc7b5542485f7bfbd11eb14 /crates/ra_hir
parentbbb022d3999b3038549ec6c309efb065231c896a (diff)
Refactor Module::from_source to properly descend from root file
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/code_model.rs2
-rw-r--r--crates/ra_hir/src/from_source.rs68
2 files changed, 37 insertions, 33 deletions
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs
index 962d5a8c1..dd43271f4 100644
--- a/crates/ra_hir/src/code_model.rs
+++ b/crates/ra_hir/src/code_model.rs
@@ -157,7 +157,7 @@ impl Module {
157 } 157 }
158 158
159 /// Finds a child module with the specified name. 159 /// Finds a child module with the specified name.
160 pub fn child(self, db: &impl HirDatabase, name: &Name) -> Option<Module> { 160 pub fn child(self, db: &impl DefDatabase, name: &Name) -> Option<Module> {
161 let def_map = db.crate_def_map(self.id.krate); 161 let def_map = db.crate_def_map(self.id.krate);
162 let child_id = def_map[self.id.module_id].children.get(name)?; 162 let child_id = def_map[self.id.module_id].children.get(name)?;
163 Some(self.with_module_id(*child_id)) 163 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 9793af858..7e5523c54 100644
--- a/crates/ra_hir/src/from_source.rs
+++ b/crates/ra_hir/src/from_source.rs
@@ -1,6 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use hir_def::{StructId, StructOrUnionId, UnionId}; 3use hir_def::{ModuleId, StructId, StructOrUnionId, UnionId};
4use hir_expand::name::AsName; 4use hir_expand::name::AsName;
5use ra_syntax::{ 5use ra_syntax::{
6 ast::{self, AstNode, NameOwner}, 6 ast::{self, AstNode, NameOwner},
@@ -10,9 +10,9 @@ use ra_syntax::{
10use crate::{ 10use crate::{
11 db::{AstDatabase, DefDatabase, HirDatabase}, 11 db::{AstDatabase, DefDatabase, HirDatabase},
12 ids::{AstItemDef, LocationCtx}, 12 ids::{AstItemDef, LocationCtx},
13 AstId, Const, Crate, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource, 13 Const, DefWithBody, Enum, EnumVariant, FieldSource, Function, HasBody, HasSource, ImplBlock,
14 ImplBlock, Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, 14 Local, Module, ModuleSource, Source, Static, Struct, StructField, Trait, TypeAlias, Union,
15 Union, VariantDef, 15 VariantDef,
16}; 16};
17 17
18pub trait FromSource: Sized { 18pub trait FromSource: Sized {
@@ -152,44 +152,48 @@ impl Local {
152} 152}
153 153
154impl Module { 154impl Module {
155 pub fn from_declaration(db: &impl HirDatabase, src: Source<ast::Module>) -> Option<Self> { 155 pub fn from_declaration(db: &impl DefDatabase, src: Source<ast::Module>) -> Option<Self> {
156 let src_parent = Source { 156 let parent_declaration = src.ast.syntax().ancestors().skip(1).find_map(ast::Module::cast);
157 file_id: src.file_id, 157
158 ast: ModuleSource::new(db, Some(src.file_id.original_file(db)), None), 158 let parent_module = match parent_declaration {
159 }; 159 Some(parent_declaration) => {
160 let parent_module = Module::from_definition(db, src_parent)?; 160 let src_parent = Source { file_id: src.file_id, ast: parent_declaration };
161 Module::from_declaration(db, src_parent)
162 }
163 _ => {
164 let src_parent = Source {
165 file_id: src.file_id,
166 ast: ModuleSource::new(db, Some(src.file_id.original_file(db)), None),
167 };
168 Module::from_definition(db, src_parent)
169 }
170 }?;
171
161 let child_name = src.ast.name()?; 172 let child_name = src.ast.name()?;
162 parent_module.child(db, &child_name.as_name()) 173 parent_module.child(db, &child_name.as_name())
163 } 174 }
164 175
165 pub fn from_definition( 176 pub fn from_definition(db: &impl DefDatabase, src: Source<ModuleSource>) -> Option<Self> {
166 db: &(impl DefDatabase + AstDatabase), 177 match src.ast {
167 src: Source<ModuleSource>,
168 ) -> Option<Self> {
169 let decl_id = match src.ast {
170 ModuleSource::Module(ref module) => { 178 ModuleSource::Module(ref module) => {
171 assert!(!module.has_semi()); 179 assert!(!module.has_semi());
172 let ast_id_map = db.ast_id_map(src.file_id); 180 return Module::from_declaration(
173 let item_id = AstId::new(src.file_id, ast_id_map.ast_id(module)); 181 db,
174 Some(item_id) 182 Source { file_id: src.file_id, ast: module.clone() },
183 );
175 } 184 }
176 ModuleSource::SourceFile(_) => None, 185 ModuleSource::SourceFile(_) => (),
177 }; 186 };
178 187
179 db.relevant_crates(src.file_id.original_file(db)).iter().find_map(|&crate_id| { 188 let original_file = src.file_id.original_file(db);
180 let def_map = db.crate_def_map(crate_id);
181
182 let (module_id, _module_data) =
183 def_map.modules.iter().find(|(_module_id, module_data)| {
184 if decl_id.is_some() {
185 module_data.declaration == decl_id
186 } else {
187 module_data.definition.map(|it| it.into()) == Some(src.file_id)
188 }
189 })?;
190 189
191 Some(Module::new(Crate { crate_id }, module_id)) 190 let (krate, module_id) =
192 }) 191 db.relevant_crates(original_file).iter().find_map(|&crate_id| {
192 let crate_def_map = db.crate_def_map(crate_id);
193 let local_module_id = crate_def_map.modules_for_file(original_file).next()?;
194 Some((crate_id, local_module_id))
195 })?;
196 Some(Module { id: ModuleId { krate, module_id } })
193 } 197 }
194} 198}
195 199