22use std:: iter;
33
44use either:: Either ;
5- use span:: { FileId , FileRange , HirFileId , HirFileIdRepr , MacroFileId , SyntaxContextId } ;
6- use syntax:: { AstNode , SyntaxNode , SyntaxToken , TextRange , TextSize } ;
5+ use span:: {
6+ AstIdNode , ErasedFileAstId , FileAstId , FileId , FileRange , HirFileId , HirFileIdRepr ,
7+ MacroFileId , SyntaxContextId ,
8+ } ;
9+ use syntax:: { AstNode , AstPtr , SyntaxNode , SyntaxNodePtr , SyntaxToken , TextRange , TextSize } ;
710
8- use crate :: { db, map_node_range_up, span_for_offset, MacroFileIdExt } ;
11+ use crate :: {
12+ db:: { self , ExpandDatabase } ,
13+ map_node_range_up, span_for_offset, MacroFileIdExt ,
14+ } ;
915
1016/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
1117///
@@ -23,6 +29,31 @@ pub type InFile<T> = InFileWrapper<HirFileId, T>;
2329pub type InMacroFile < T > = InFileWrapper < MacroFileId , T > ;
2430pub type InRealFile < T > = InFileWrapper < FileId , T > ;
2531
32+ /// `AstId` points to an AST node in any file.
33+ ///
34+ /// It is stable across reparses, and can be used as salsa key/value.
35+ pub type AstId < N > = crate :: InFile < FileAstId < N > > ;
36+
37+ impl < N : AstIdNode > AstId < N > {
38+ pub fn to_node ( & self , db : & dyn ExpandDatabase ) -> N {
39+ self . to_ptr ( db) . to_node ( & db. parse_or_expand ( self . file_id ) )
40+ }
41+ pub fn to_in_file_node ( & self , db : & dyn ExpandDatabase ) -> crate :: InFile < N > {
42+ crate :: InFile :: new ( self . file_id , self . to_ptr ( db) . to_node ( & db. parse_or_expand ( self . file_id ) ) )
43+ }
44+ pub fn to_ptr ( & self , db : & dyn ExpandDatabase ) -> AstPtr < N > {
45+ db. ast_id_map ( self . file_id ) . get ( self . value )
46+ }
47+ }
48+
49+ pub type ErasedAstId = crate :: InFile < ErasedFileAstId > ;
50+
51+ impl ErasedAstId {
52+ pub fn to_ptr ( & self , db : & dyn ExpandDatabase ) -> SyntaxNodePtr {
53+ db. ast_id_map ( self . file_id ) . get_erased ( self . value )
54+ }
55+ }
56+
2657impl < FileKind , T > InFileWrapper < FileKind , T > {
2758 pub fn new ( file_id : FileKind , value : T ) -> Self {
2859 Self { file_id, value }
0 commit comments