@@ -75,27 +75,40 @@ pub struct RealSpanMap {
75
75
/// Invariant: Sorted vec over TextSize
76
76
// FIXME: SortedVec<(TextSize, ErasedFileAstId)>?
77
77
pairs : Box < [ ( TextSize , ErasedFileAstId ) ] > ,
78
+ end : TextSize ,
78
79
}
79
80
80
81
impl RealSpanMap {
81
82
/// Creates a real file span map that returns absolute ranges (relative ranges to the root ast id).
82
83
pub fn absolute ( file_id : FileId ) -> Self {
83
- RealSpanMap { file_id, pairs : Box :: from ( [ ( TextSize :: new ( 0 ) , ROOT_ERASED_FILE_AST_ID ) ] ) }
84
+ RealSpanMap {
85
+ file_id,
86
+ pairs : Box :: from ( [ ( TextSize :: new ( 0 ) , ROOT_ERASED_FILE_AST_ID ) ] ) ,
87
+ end : TextSize :: new ( !0 ) ,
88
+ }
84
89
}
85
90
86
91
pub fn from_file ( db : & dyn ExpandDatabase , file_id : FileId ) -> Self {
87
92
let mut pairs = vec ! [ ( TextSize :: new( 0 ) , ROOT_ERASED_FILE_AST_ID ) ] ;
88
93
let ast_id_map = db. ast_id_map ( file_id. into ( ) ) ;
89
- pairs. extend (
90
- db. parse ( file_id)
91
- . tree ( )
92
- . items ( )
93
- . map ( |item| ( item. syntax ( ) . text_range ( ) . start ( ) , ast_id_map. ast_id ( & item) . erase ( ) ) ) ,
94
- ) ;
95
- RealSpanMap { file_id, pairs : pairs. into_boxed_slice ( ) }
94
+ let tree = db. parse ( file_id) . tree ( ) ;
95
+ pairs
96
+ . extend ( tree. items ( ) . map ( |item| {
97
+ ( item. syntax ( ) . text_range ( ) . start ( ) , ast_id_map. ast_id ( & item) . erase ( ) )
98
+ } ) ) ;
99
+ RealSpanMap {
100
+ file_id,
101
+ pairs : pairs. into_boxed_slice ( ) ,
102
+ end : tree. syntax ( ) . text_range ( ) . end ( ) ,
103
+ }
96
104
}
97
105
98
106
pub fn span_for_range ( & self , range : TextRange ) -> SpanData {
107
+ assert ! (
108
+ range. end( ) <= self . end,
109
+ "range {range:?} goes beyond the end of the file {:?}" ,
110
+ self . end
111
+ ) ;
99
112
let start = range. start ( ) ;
100
113
let idx = self
101
114
. pairs
0 commit comments