Skip to content

Commit 0ad2450

Browse files
committed
Check for inside multibyte
1 parent 902b343 commit 0ad2450

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

lib/line-index/src/lib.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,20 @@ impl LineIndex {
144144
self.try_line_col(offset).expect("invalid offset")
145145
}
146146

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.
148149
pub fn try_line_col(&self, offset: TextSize) -> Option<LineCol> {
149150
let line = self.newlines.partition_point(|&it| it <= offset).checked_sub(1)?;
150151
let line_start_offset = self.newlines.get(line)?;
151152
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)
153161
}
154162

155163
/// Transforms the `LineCol` into a `TextSize`.

0 commit comments

Comments
 (0)