File tree Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Expand file tree Collapse file tree 1 file changed +10
-2
lines changed Original file line number Diff line number Diff line change @@ -144,12 +144,20 @@ impl LineIndex {
144
144
self . try_line_col ( offset) . expect ( "invalid offset" )
145
145
}
146
146
147
- /// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid.
147
+ /// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid,
148
+ /// e.g. if it points to the middle of a multi-byte character.
148
149
pub fn try_line_col ( & self , offset : TextSize ) -> Option < LineCol > {
149
150
let line = self . newlines . partition_point ( |& it| it <= offset) . checked_sub ( 1 ) ?;
150
151
let line_start_offset = self . newlines . get ( line) ?;
151
152
let col = offset - line_start_offset;
152
- Some ( LineCol { line : line as u32 , col : col. into ( ) } )
153
+ let ret = LineCol { line : line as u32 , col : col. into ( ) } ;
154
+ self . line_wide_chars
155
+ . get ( & ret. line )
156
+ . into_iter ( )
157
+ . flat_map ( |it| it. iter ( ) )
158
+ . find ( |it| it. start < col && col < it. end )
159
+ . is_none ( )
160
+ . then_some ( ret)
153
161
}
154
162
155
163
/// Transforms the `LineCol` into a `TextSize`.
You can’t perform that action at this time.
0 commit comments