mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 14:51:48 +00:00
Check for inside multibyte
This commit is contained in:
parent
902b3438c9
commit
0ad2450396
1 changed files with 10 additions and 2 deletions
|
@ -144,12 +144,20 @@ impl LineIndex {
|
|||
self.try_line_col(offset).expect("invalid offset")
|
||||
}
|
||||
|
||||
/// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid.
|
||||
/// Transforms the `TextSize` into a `LineCol`, or returns `None` if the `offset` was invalid,
|
||||
/// e.g. if it points to the middle of a multi-byte character.
|
||||
pub fn try_line_col(&self, offset: TextSize) -> Option<LineCol> {
|
||||
let line = self.newlines.partition_point(|&it| it <= offset).checked_sub(1)?;
|
||||
let line_start_offset = self.newlines.get(line)?;
|
||||
let col = offset - line_start_offset;
|
||||
Some(LineCol { line: line as u32, col: col.into() })
|
||||
let ret = LineCol { line: line as u32, col: col.into() };
|
||||
self.line_wide_chars
|
||||
.get(&ret.line)
|
||||
.into_iter()
|
||||
.flat_map(|it| it.iter())
|
||||
.find(|it| it.start < col && col < it.end)
|
||||
.is_none()
|
||||
.then_some(ret)
|
||||
}
|
||||
|
||||
/// Transforms the `LineCol` into a `TextSize`.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue