mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 15:14:32 +00:00
Return None for too-large offset
This commit is contained in:
parent
e8a93306f0
commit
343976fe56
1 changed files with 9 additions and 1 deletions
|
@ -84,6 +84,8 @@ impl WideChar {
|
|||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LineIndex {
|
||||
/// Offset the beginning of each line (except the first, which always has offset 0).
|
||||
///
|
||||
/// Invariant: Always non-empty and the last element holds the length of the original text.
|
||||
newlines: Box<[TextSize]>,
|
||||
/// List of non-ASCII characters on each line.
|
||||
line_wide_chars: IntMap<u32, Box<[WideChar]>>,
|
||||
|
@ -125,6 +127,8 @@ impl LineIndex {
|
|||
cur_col += c_len;
|
||||
}
|
||||
|
||||
newlines.push(TextSize::of(text));
|
||||
|
||||
// Save any wide characters seen in the last line
|
||||
if !wide_chars.is_empty() {
|
||||
line_wide_chars.insert(line, wide_chars.into_boxed_slice());
|
||||
|
@ -143,8 +147,12 @@ impl LineIndex {
|
|||
}
|
||||
|
||||
/// 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.
|
||||
/// e.g. if it extends past the end of the text or points to the middle of a multi-byte
|
||||
/// character.
|
||||
pub fn try_line_col(&self, offset: TextSize) -> Option<LineCol> {
|
||||
if offset > *self.newlines.last().unwrap() {
|
||||
return None;
|
||||
}
|
||||
let line = self.newlines.partition_point(|&it| it <= offset);
|
||||
let start = self.start_offset(line)?;
|
||||
let col = offset - start;
|
||||
|
|
Loading…
Reference in a new issue