1
1
//! FIXME: write short doc here
2
2
3
- use hir_def:: { StructId , StructOrUnionId , UnionId } ;
3
+ use hir_def:: { ModuleId , StructId , StructOrUnionId , UnionId } ;
4
4
use hir_expand:: name:: AsName ;
5
5
use ra_syntax:: {
6
6
ast:: { self , AstNode , NameOwner } ,
@@ -10,9 +10,9 @@ use ra_syntax::{
10
10
use crate :: {
11
11
db:: { AstDatabase , DefDatabase , HirDatabase } ,
12
12
ids:: { AstItemDef , LocationCtx } ,
13
- AstId , Const , Crate , DefWithBody , Enum , EnumVariant , FieldSource , Function , HasBody , HasSource ,
14
- ImplBlock , Local , Module , ModuleSource , Source , Static , Struct , StructField , Trait , TypeAlias ,
15
- Union , VariantDef ,
13
+ Const , DefWithBody , Enum , EnumVariant , FieldSource , Function , HasBody , HasSource , ImplBlock ,
14
+ Local , Module , ModuleSource , Source , Static , Struct , StructField , Trait , TypeAlias , Union ,
15
+ VariantDef ,
16
16
} ;
17
17
18
18
pub trait FromSource : Sized {
@@ -152,44 +152,48 @@ impl Local {
152
152
}
153
153
154
154
impl Module {
155
- pub fn from_declaration ( db : & impl HirDatabase , src : Source < ast:: Module > ) -> Option < Self > {
156
- let src_parent = Source {
157
- file_id : src. file_id ,
158
- ast : ModuleSource :: new ( db, Some ( src. file_id . original_file ( db) ) , None ) ,
159
- } ;
160
- let parent_module = Module :: from_definition ( db, src_parent) ?;
155
+ pub fn from_declaration ( db : & impl DefDatabase , src : Source < ast:: Module > ) -> Option < Self > {
156
+ let parent_declaration = src. ast . syntax ( ) . ancestors ( ) . skip ( 1 ) . find_map ( ast:: Module :: cast) ;
157
+
158
+ let parent_module = match parent_declaration {
159
+ Some ( parent_declaration) => {
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
172
let child_name = src. ast . name ( ) ?;
162
173
parent_module. child ( db, & child_name. as_name ( ) )
163
174
}
164
175
165
- pub fn from_definition (
166
- db : & ( impl DefDatabase + AstDatabase ) ,
167
- src : Source < ModuleSource > ,
168
- ) -> Option < Self > {
169
- let decl_id = match src. ast {
176
+ pub fn from_definition ( db : & impl DefDatabase , src : Source < ModuleSource > ) -> Option < Self > {
177
+ match src. ast {
170
178
ModuleSource :: Module ( ref module) => {
171
179
assert ! ( !module. has_semi( ) ) ;
172
- let ast_id_map = db. ast_id_map ( src. file_id ) ;
173
- let item_id = AstId :: new ( src. file_id , ast_id_map. ast_id ( module) ) ;
174
- Some ( item_id)
180
+ return Module :: from_declaration (
181
+ db,
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| {
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
- } ) ?;
188
+ let original_file = src. file_id . original_file ( db) ;
190
189
191
- Some ( Module :: new ( Crate { crate_id } , module_id) )
192
- } )
190
+ let ( krate, module_id) =
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
0 commit comments