@@ -84,6 +84,8 @@ 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.
8789 newlines : Box < [ TextSize ] > ,
8890 /// List of non-ASCII characters on each line.
8991 line_wide_chars : IntMap < u32 , Box < [ WideChar ] > > ,
@@ -125,6 +127,8 @@ impl LineIndex {
125127 cur_col += c_len;
126128 }
127129
130+ newlines. push ( TextSize :: of ( text) ) ;
131+
128132 // Save any wide characters seen in the last line
129133 if !wide_chars. is_empty ( ) {
130134 line_wide_chars. insert ( line, wide_chars. into_boxed_slice ( ) ) ;
@@ -143,8 +147,12 @@ impl LineIndex {
143147 }
144148
145149 /// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid,
146- /// e.g. if it points to the middle of a multi-byte character.
150+ /// e.g. if it extends past the end of the text or points to the middle of a multi-byte
151+ /// character.
147152 pub fn try_line_col ( & self , offset : TextSize ) -> Option < LineCol > {
153+ if offset > * self . newlines . last ( ) . unwrap ( ) {
154+ return None ;
155+ }
148156 let line = self . newlines . partition_point ( |& it| it <= offset) ;
149157 let start = self . start_offset ( line) ?;
150158 let col = offset - start;
0 commit comments