@@ -25,9 +25,9 @@ use crate::{
25
25
db:: HirDatabase ,
26
26
semantics:: source_to_def:: { ChildContainer , SourceToDefCache , SourceToDefCtx } ,
27
27
source_analyzer:: { resolve_hir_path, SourceAnalyzer } ,
28
- Access , AssocItem , Callable , ConstParam , Crate , Field , Function , HirFileId , Impl , InFile ,
29
- Label , LifetimeParam , Local , MacroDef , Module , ModuleDef , Name , Path , ScopeDef , Trait , Type ,
30
- TypeAlias , TypeParam , VariantDef ,
28
+ Access , AssocItem , Callable , ConstParam , Crate , Field , Function , HasSource , HirFileId , Impl ,
29
+ InFile , Label , LifetimeParam , Local , MacroDef , Module , ModuleDef , Name , Path , ScopeDef , Trait ,
30
+ Type , TypeAlias , TypeParam , VariantDef ,
31
31
} ;
32
32
33
33
#[ derive( Debug , Clone , PartialEq , Eq ) ]
@@ -190,6 +190,14 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
190
190
self . imp . descend_node_into_attributes ( node)
191
191
}
192
192
193
+ /// Search for a definition's source and cache its syntax tree
194
+ pub fn source < Def : HasSource > ( & self , def : Def ) -> Option < InFile < Def :: Ast > >
195
+ where
196
+ Def :: Ast : AstNode ,
197
+ {
198
+ self . imp . source ( def)
199
+ }
200
+
193
201
pub fn hir_file_for ( & self , syntax_node : & SyntaxNode ) -> HirFileId {
194
202
self . imp . find_file ( syntax_node. clone ( ) ) . file_id
195
203
}
@@ -845,6 +853,15 @@ impl<'db> SemanticsImpl<'db> {
845
853
SemanticsScope { db : self . db , file_id, resolver }
846
854
}
847
855
856
+ fn source < Def : HasSource > ( & self , def : Def ) -> Option < InFile < Def :: Ast > >
857
+ where
858
+ Def :: Ast : AstNode ,
859
+ {
860
+ let res = def. source ( self . db ) ?;
861
+ self . cache ( find_root ( res. value . syntax ( ) ) , res. file_id ) ;
862
+ Some ( res)
863
+ }
864
+
848
865
fn analyze ( & self , node : & SyntaxNode ) -> SourceAnalyzer {
849
866
self . analyze_impl ( node, None )
850
867
}
0 commit comments