@@ -84,6 +84,8 @@ impl WideChar {
84
84
#[ derive( Debug , Clone , PartialEq , Eq ) ]
85
85
pub struct LineIndex {
86
86
/// 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.
87
89
newlines : Box < [ TextSize ] > ,
88
90
/// List of non-ASCII characters on each line.
89
91
line_wide_chars : IntMap < u32 , Box < [ WideChar ] > > ,
@@ -125,6 +127,8 @@ impl LineIndex {
125
127
cur_col += c_len;
126
128
}
127
129
130
+ newlines. push ( TextSize :: of ( text) ) ;
131
+
128
132
// Save any wide characters seen in the last line
129
133
if !wide_chars. is_empty ( ) {
130
134
line_wide_chars. insert ( line, wide_chars. into_boxed_slice ( ) ) ;
@@ -143,8 +147,12 @@ impl LineIndex {
143
147
}
144
148
145
149
/// 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.
147
152
pub fn try_line_col ( & self , offset : TextSize ) -> Option < LineCol > {
153
+ if offset > * self . newlines . last ( ) . unwrap ( ) {
154
+ return None ;
155
+ }
148
156
let line = self . newlines . partition_point ( |& it| it <= offset) ;
149
157
let start = self . start_offset ( line) ?;
150
158
let col = offset - start;
0 commit comments