2
2
use std:: iter;
3
3
4
4
use 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 } ;
7
10
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
+ } ;
9
15
10
16
/// `InFile<T>` stores a value of `T` inside a particular file/syntax tree.
11
17
///
@@ -23,6 +29,31 @@ pub type InFile<T> = InFileWrapper<HirFileId, T>;
23
29
pub type InMacroFile < T > = InFileWrapper < MacroFileId , T > ;
24
30
pub type InRealFile < T > = InFileWrapper < FileId , T > ;
25
31
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
+
26
57
impl < FileKind , T > InFileWrapper < FileKind , T > {
27
58
pub fn new ( file_id : FileKind , value : T ) -> Self {
28
59
Self { file_id, value }
0 commit comments