@@ -84,11 +84,11 @@ impl WideChar {
8484#[ derive( Debug , Clone , PartialEq , Eq ) ]
8585pub struct LineIndex {
8686 /// Offset the beginning of each line (except the first, which always has offset 0).
87- ///
88- /// Invariant: Always non-empty and the last element holds the length of the original text.
8987 newlines : Box < [ TextSize ] > ,
9088 /// List of non-ASCII characters on each line.
9189 line_wide_chars : IntMap < u32 , Box < [ WideChar ] > > ,
90+ /// The size of the entire text.
91+ size : TextSize ,
9292}
9393
9494impl LineIndex {
@@ -127,14 +127,16 @@ impl LineIndex {
127127 cur_col += c_len;
128128 }
129129
130- newlines. push ( TextSize :: of ( text) ) ;
131-
132130 // Save any wide characters seen in the last line
133131 if !wide_chars. is_empty ( ) {
134132 line_wide_chars. insert ( line, wide_chars. into_boxed_slice ( ) ) ;
135133 }
136134
137- LineIndex { newlines : newlines. into_boxed_slice ( ) , line_wide_chars }
135+ LineIndex {
136+ newlines : newlines. into_boxed_slice ( ) ,
137+ line_wide_chars,
138+ size : TextSize :: of ( text) ,
139+ }
138140 }
139141
140142 /// Transforms the `TextSize` into a `LineCol`.
@@ -150,7 +152,7 @@ impl LineIndex {
150152 /// e.g. if it extends past the end of the text or points to the middle of a multi-byte
151153 /// character.
152154 pub fn try_line_col ( & self , offset : TextSize ) -> Option < LineCol > {
153- if offset > * self . newlines . last ( ) . unwrap ( ) {
155+ if offset > self . size {
154156 return None ;
155157 }
156158 let line = self . newlines . partition_point ( |& it| it <= offset) ;
0 commit comments