File tree Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change @@ -141,10 +141,15 @@ impl LineIndex {
141
141
///
142
142
/// If the offset is invalid.
143
143
pub fn line_col ( & self , offset : TextSize ) -> LineCol {
144
- let line = self . newlines . partition_point ( |& it| it <= offset) - 1 ;
145
- let line_start_offset = self . newlines [ line] ;
144
+ self . try_line_col ( offset) . expect ( "invalid offset" )
145
+ }
146
+
147
+ /// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid.
148
+ pub fn try_line_col ( & self , offset : TextSize ) -> Option < LineCol > {
149
+ let line = self . newlines . partition_point ( |& it| it <= offset) . checked_sub ( 1 ) ?;
150
+ let line_start_offset = self . newlines . get ( line) ?;
146
151
let col = offset - line_start_offset;
147
- LineCol { line : line as u32 , col : col. into ( ) }
152
+ Some ( LineCol { line : line as u32 , col : col. into ( ) } )
148
153
}
149
154
150
155
/// Transforms the `LineCol` into a `TextSize`.
You can’t perform that action at this time.
0 commit comments