pub struct LineIndex { /* private fields */ }Expand description
Precomputed index that maps between byte offsets and (line, col) pairs.
It stores the byte offset of the start of every line so that both directions of the mapping are O(log n) via binary search.
Implementations§
Source§impl LineIndex
impl LineIndex
Sourcepub fn build_from_str(text: &str) -> Self
pub fn build_from_str(text: &str) -> Self
Build from a plain &str (convenience for testing).
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Number of lines in the indexed text.
Sourcepub fn line_col_to_byte(&self, lc: LineCol) -> Option<usize>
pub fn line_col_to_byte(&self, lc: LineCol) -> Option<usize>
Convert a LineCol to an absolute byte offset.
Returns None if the position is out of range.
Sourcepub fn byte_to_line_col(&self, byte_offset: usize) -> Option<LineCol>
pub fn byte_to_line_col(&self, byte_offset: usize) -> Option<LineCol>
Convert an absolute byte offset to a LineCol.
Returns None if byte_offset > source.len().
Sourcepub fn line_start_byte(&self, line: usize) -> Option<usize>
pub fn line_start_byte(&self, line: usize) -> Option<usize>
Byte offset of the first byte of line (0-based).
Returns None if line >= line_count().
Sourcepub fn line_end_byte(&self, line: usize) -> Option<usize>
pub fn line_end_byte(&self, line: usize) -> Option<usize>
Byte offset one past the last byte of line (exclusive end).
For the last line this equals source.len(). Returns None if
line >= line_count().
Sourcepub fn utf16_col_to_byte(&self, line: usize, utf16_col: usize) -> Option<usize>
pub fn utf16_col_to_byte(&self, line: usize, utf16_col: usize) -> Option<usize>
Convert a UTF-16 code-unit column (as used by LSP) to a byte offset within the document.
line is 0-based. utf16_col is the number of UTF-16 code units
from the start of the line.
Returns None if the position cannot be mapped (line out of range or
column past end of line).
Sourcepub fn byte_to_utf16_col(&self, byte_offset: usize) -> Option<(usize, usize)>
pub fn byte_to_utf16_col(&self, byte_offset: usize) -> Option<(usize, usize)>
Convert a byte offset to a (line, utf16_col) pair — the inverse of
utf16_col_to_byte.