@@ -84,11 +84,11 @@ 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.
89
87
newlines : Box < [ TextSize ] > ,
90
88
/// List of non-ASCII characters on each line.
91
89
line_wide_chars : IntMap < u32 , Box < [ WideChar ] > > ,
90
+ /// The size of the entire text.
91
+ size : TextSize ,
92
92
}
93
93
94
94
impl LineIndex {
@@ -127,14 +127,16 @@ impl LineIndex {
127
127
cur_col += c_len;
128
128
}
129
129
130
- newlines. push ( TextSize :: of ( text) ) ;
131
-
132
130
// Save any wide characters seen in the last line
133
131
if !wide_chars. is_empty ( ) {
134
132
line_wide_chars. insert ( line, wide_chars. into_boxed_slice ( ) ) ;
135
133
}
136
134
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
+ }
138
140
}
139
141
140
142
/// Transforms the `TextSize` into a `LineCol`.
@@ -150,7 +152,7 @@ impl LineIndex {
150
152
/// e.g. if it extends past the end of the text or points to the middle of a multi-byte
151
153
/// character.
152
154
pub fn try_line_col ( & self , offset : TextSize ) -> Option < LineCol > {
153
- if offset > * self . newlines . last ( ) . unwrap ( ) {
155
+ if offset > self . size {
154
156
return None ;
155
157
}
156
158
let line = self . newlines . partition_point ( |& it| it <= offset) ;
0 commit comments