11//! Provides set of implementation for hir's objects that allows get back location in file.
22
3+ use base_db:: FileId ;
34use either:: Either ;
45use hir_def:: {
56 nameres:: { ModuleOrigin , ModuleSource } ,
67 src:: { HasChildSource , HasSource as _} ,
78 Lookup , MacroId , VariantId ,
89} ;
9- use hir_expand:: InFile ;
10+ use hir_expand:: { HirFileId , InFile } ;
1011use syntax:: ast;
1112
1213use crate :: {
@@ -32,6 +33,11 @@ impl Module {
3233 def_map[ self . id . local_id ] . definition_source ( db. upcast ( ) )
3334 }
3435
36+ pub fn definition_source_file_id ( self , db : & dyn HirDatabase ) -> HirFileId {
37+ let def_map = self . id . def_map ( db. upcast ( ) ) ;
38+ def_map[ self . id . local_id ] . definition_source_file_id ( )
39+ }
40+
3541 pub fn is_mod_rs ( self , db : & dyn HirDatabase ) -> bool {
3642 let def_map = self . id . def_map ( db. upcast ( ) ) ;
3743 match def_map[ self . id . local_id ] . origin {
@@ -40,6 +46,16 @@ impl Module {
4046 }
4147 }
4248
49+ pub fn as_source_file_id ( self , db : & dyn HirDatabase ) -> Option < FileId > {
50+ let def_map = self . id . def_map ( db. upcast ( ) ) ;
51+ match def_map[ self . id . local_id ] . origin {
52+ ModuleOrigin :: File { definition, .. } | ModuleOrigin :: CrateRoot { definition, .. } => {
53+ Some ( definition)
54+ }
55+ _ => None ,
56+ }
57+ }
58+
4359 pub fn is_inline ( self , db : & dyn HirDatabase ) -> bool {
4460 let def_map = self . id . def_map ( db. upcast ( ) ) ;
4561 def_map[ self . id . local_id ] . origin . is_inline ( )
0 commit comments