@@ -75,27 +75,40 @@ pub struct RealSpanMap {
7575 /// Invariant: Sorted vec over TextSize
7676 // FIXME: SortedVec<(TextSize, ErasedFileAstId)>?
7777 pairs : Box < [ ( TextSize , ErasedFileAstId ) ] > ,
78+ end : TextSize ,
7879}
7980
8081impl RealSpanMap {
8182 /// Creates a real file span map that returns absolute ranges (relative ranges to the root ast id).
8283 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+ }
8489 }
8590
8691 pub fn from_file ( db : & dyn ExpandDatabase , file_id : FileId ) -> Self {
8792 let mut pairs = vec ! [ ( TextSize :: new( 0 ) , ROOT_ERASED_FILE_AST_ID ) ] ;
8893 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+ }
96104 }
97105
98106 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+ ) ;
99112 let start = range. start ( ) ;
100113 let idx = self
101114 . pairs
0 commit comments