diff options
Diffstat (limited to 'crates/ra_hir/src/from_source.rs')
-rw-r--r-- | crates/ra_hir/src/from_source.rs | 68 |
1 files changed, 36 insertions, 32 deletions
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 | ||
3 | use hir_def::{StructId, StructOrUnionId, UnionId}; | 3 | use hir_def::{ModuleId, StructId, StructOrUnionId, UnionId}; |
4 | use hir_expand::name::AsName; | 4 | use hir_expand::name::AsName; |
5 | use ra_syntax::{ | 5 | use ra_syntax::{ |
6 | ast::{self, AstNode, NameOwner}, | 6 | ast::{self, AstNode, NameOwner}, |
@@ -10,9 +10,9 @@ use ra_syntax::{ | |||
10 | use crate::{ | 10 | use 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 | ||
18 | pub trait FromSource: Sized { | 18 | pub trait FromSource: Sized { |
@@ -152,44 +152,48 @@ impl Local { | |||
152 | } | 152 | } |
153 | 153 | ||
154 | impl Module { | 154 | impl 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 | ||