aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir/src/code_model.rs2
-rw-r--r--crates/ra_hir/src/from_source.rs68
-rw-r--r--crates/ra_hir/src/test_db.rs2
-rw-r--r--crates/ra_hir_def/src/nameres.rs13
4 files changed, 50 insertions, 35 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
diff --git a/crates/ra_hir/src/test_db.rs b/crates/ra_hir/src/test_db.rs
index 5237b303a..1caa2e875 100644
--- a/crates/ra_hir/src/test_db.rs
+++ b/crates/ra_hir/src/test_db.rs
@@ -81,7 +81,7 @@ impl TestDB {
81 let crate_graph = self.crate_graph(); 81 let crate_graph = self.crate_graph();
82 for krate in crate_graph.iter().next() { 82 for krate in crate_graph.iter().next() {
83 let crate_def_map = self.crate_def_map(krate); 83 let crate_def_map = self.crate_def_map(krate);
84 for (module_id, _) in crate_def_map.modules.iter() { 84 for module_id in crate_def_map.modules() {
85 let module_id = ModuleId { krate, module_id }; 85 let module_id = ModuleId { krate, module_id };
86 let module = crate::Module::from(module_id); 86 let module = crate::Module::from(module_id);
87 module.diagnostics( 87 module.diagnostics(
diff --git a/crates/ra_hir_def/src/nameres.rs b/crates/ra_hir_def/src/nameres.rs
index d3ecabb9b..5fc592150 100644
--- a/crates/ra_hir_def/src/nameres.rs
+++ b/crates/ra_hir_def/src/nameres.rs
@@ -87,7 +87,7 @@ pub struct CrateDefMap {
87 prelude: Option<ModuleId>, 87 prelude: Option<ModuleId>,
88 extern_prelude: FxHashMap<Name, ModuleDefId>, 88 extern_prelude: FxHashMap<Name, ModuleDefId>,
89 root: CrateModuleId, 89 root: CrateModuleId,
90 pub modules: Arena<CrateModuleId, ModuleData>, 90 modules: Arena<CrateModuleId, ModuleData>,
91 91
92 /// Some macros are not well-behavior, which leads to infinite loop 92 /// Some macros are not well-behavior, which leads to infinite loop
93 /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } } 93 /// e.g. macro_rules! foo { ($ty:ty) => { foo!($ty); } }
@@ -258,6 +258,17 @@ impl CrateDefMap {
258 let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path); 258 let res = self.resolve_path_fp_with_macro(db, ResolveMode::Other, original_module, path);
259 (res.resolved_def, res.segment_index) 259 (res.resolved_def, res.segment_index)
260 } 260 }
261
262 pub fn modules(&self) -> impl Iterator<Item = CrateModuleId> + '_ {
263 self.modules.iter().map(|(id, _data)| id)
264 }
265
266 pub fn modules_for_file(&self, file_id: FileId) -> impl Iterator<Item = CrateModuleId> + '_ {
267 self.modules
268 .iter()
269 .filter(move |(_id, data)| data.definition == Some(file_id))
270 .map(|(id, _data)| id)
271 }
261} 272}
262 273
263mod diagnostics { 274mod diagnostics {