pub struct LineIndex { /* private fields */ }Expand description
Line index for byte <-> (line, col) mapping.
Implementations§
Source§impl LineIndex
impl LineIndex
Sourcepub fn byte_to_position(&self, byte: usize) -> (usize, usize)
pub fn byte_to_position(&self, byte: usize) -> (usize, usize)
Convert a byte offset to (line, column) using byte columns.
Sourcepub fn position_to_byte(&self, line: usize, column: usize) -> Option<usize>
pub fn position_to_byte(&self, line: usize, column: usize) -> Option<usize>
Convert (line, column) back to byte offset.
Returns None when the line is out of range or when the column extends
past the end of the line (including the newline character, but not the
start of the next line).
Sourcepub fn position_to_byte_utf16(
&self,
text: &str,
line: usize,
column: usize,
) -> Option<usize>
pub fn position_to_byte_utf16( &self, text: &str, line: usize, column: usize, ) -> Option<usize>
Convert (line, column) back to byte offset where column is a
UTF-16 code unit offset (the unit used by the LSP Position.character
field).
This is the LSP-safe counterpart to position_to_byte, which uses
raw byte columns. On lines that contain only ASCII, the two functions
return identical results. On lines with multibyte UTF-8 characters the
byte offset can be larger than the UTF-16 column number.
§Parameters
text— the original UTF-8 source text that was used to build this index. The caller must pass the same text that was given tonew.line— 0-based line number.column— 0-based UTF-16 code unit offset from the start of the line.
§Return value
Returns None when:
lineis out of range (same asposition_to_byte).columnis past the end of the line (UTF-16 length of the line text).columnpoints into the middle of a UTF-16 surrogate pair (the interior of a supplementary character, U+10000..=U+10FFFF).
The returned byte offset is always on a UTF-8 character boundary.
Sourcepub fn position_to_byte_checked(
&self,
line: usize,
column: usize,
) -> Option<usize>
pub fn position_to_byte_checked( &self, line: usize, column: usize, ) -> Option<usize>
Convert (line, column) back to byte offset, returning None when
the column crosses the line boundary.
The newline character at the end of a line is the last addressable
column on that line. The byte at next_line_start belongs to the
next line and is therefore out of range.