1
- //! Spanmaps allow turning absolute ranges into relative ranges for incrementality purposes as well
2
- //! as associating spans with text ranges in a particular file.
3
-
4
- // FIXME: Consider moving this into the span crate
5
-
6
- use base_db:: FileId ;
7
- use span:: { ErasedFileAstId , Span , SpanAnchor , SyntaxContextId , ROOT_ERASED_FILE_AST_ID } ;
8
- use syntax:: { ast:: HasModuleItem , AstNode , TextRange , TextSize } ;
1
+ //! Span maps for real files and macro expansions.
2
+ use span:: Span ;
3
+ use syntax:: TextRange ;
9
4
use triomphe:: Arc ;
10
5
11
- use crate :: db :: ExpandDatabase ;
6
+ pub use span :: RealSpanMap ;
12
7
13
8
pub type ExpansionSpanMap = span:: SpanMap < Span > ;
14
9
@@ -39,11 +34,6 @@ impl mbe::SpanMapper<Span> for SpanMapRef<'_> {
39
34
self . span_for_range ( range)
40
35
}
41
36
}
42
- impl mbe:: SpanMapper < Span > for RealSpanMap {
43
- fn span_for ( & self , range : TextRange ) -> Span {
44
- self . span_for_range ( range)
45
- }
46
- }
47
37
48
38
impl SpanMap {
49
39
pub fn span_for_range ( & self , range : TextRange ) -> Span {
@@ -69,57 +59,3 @@ impl SpanMapRef<'_> {
69
59
}
70
60
}
71
61
}
72
-
73
- #[ derive( PartialEq , Eq , Hash , Debug ) ]
74
- pub struct RealSpanMap {
75
- file_id : FileId ,
76
- /// Invariant: Sorted vec over TextSize
77
- // FIXME: SortedVec<(TextSize, ErasedFileAstId)>?
78
- pairs : Box < [ ( TextSize , ErasedFileAstId ) ] > ,
79
- end : TextSize ,
80
- }
81
-
82
- impl RealSpanMap {
83
- /// Creates a real file span map that returns absolute ranges (relative ranges to the root ast id).
84
- pub fn absolute ( file_id : FileId ) -> Self {
85
- RealSpanMap {
86
- file_id,
87
- pairs : Box :: from ( [ ( TextSize :: new ( 0 ) , ROOT_ERASED_FILE_AST_ID ) ] ) ,
88
- end : TextSize :: new ( !0 ) ,
89
- }
90
- }
91
-
92
- pub fn from_file ( db : & dyn ExpandDatabase , file_id : FileId ) -> Self {
93
- let mut pairs = vec ! [ ( TextSize :: new( 0 ) , ROOT_ERASED_FILE_AST_ID ) ] ;
94
- let ast_id_map = db. ast_id_map ( file_id. into ( ) ) ;
95
- let tree = db. parse ( file_id) . tree ( ) ;
96
- pairs
97
- . extend ( tree. items ( ) . map ( |item| {
98
- ( item. syntax ( ) . text_range ( ) . start ( ) , ast_id_map. ast_id ( & item) . erase ( ) )
99
- } ) ) ;
100
- RealSpanMap {
101
- file_id,
102
- pairs : pairs. into_boxed_slice ( ) ,
103
- end : tree. syntax ( ) . text_range ( ) . end ( ) ,
104
- }
105
- }
106
-
107
- pub fn span_for_range ( & self , range : TextRange ) -> Span {
108
- assert ! (
109
- range. end( ) <= self . end,
110
- "range {range:?} goes beyond the end of the file {:?}" ,
111
- self . end
112
- ) ;
113
- let start = range. start ( ) ;
114
- let idx = self
115
- . pairs
116
- . binary_search_by ( |& ( it, _) | it. cmp ( & start) . then ( std:: cmp:: Ordering :: Less ) )
117
- . unwrap_err ( ) ;
118
- let ( offset, ast_id) = self . pairs [ idx - 1 ] ;
119
- Span {
120
- range : range - offset,
121
- anchor : SpanAnchor { file_id : self . file_id , ast_id } ,
122
- ctx : SyntaxContextId :: ROOT ,
123
- }
124
- }
125
- }
0 commit comments