pub struct LineIndex { /* private fields */ }Expand description
Maps byte offsets in a source string to 1-indexed (line, column)
pairs. Columns count Unicode codepoints, matching what grep -n
and most editors display.
Implementations§
Source§impl LineIndex
impl LineIndex
Sourcepub fn new(source: &str) -> Self
pub fn new(source: &str) -> Self
Build from source bytes. The index does not hold the
reference; it captures only the newline offsets.
Sourcepub fn locate(
&self,
source: &str,
byte: usize,
) -> Result<(usize, usize), LineIndexError>
pub fn locate( &self, source: &str, byte: usize, ) -> Result<(usize, usize), LineIndexError>
1-indexed (line, column) for the codepoint starting at byte.
source must be the same buffer the index was built from
(otherwise codepoint counting will use the wrong bytes).
§Errors
Returns Err if byte lies past the end of source or not
on a UTF-8 boundary. Callers should pass offsets produced by
pulldown-cmark, which always satisfy both conditions.
Sourcepub fn byte_of_position_0based(
&self,
source: &str,
line: usize,
column: usize,
) -> Option<usize>
pub fn byte_of_position_0based( &self, source: &str, line: usize, column: usize, ) -> Option<usize>
Byte offset of the codepoint at LSP-convention (line, column)
(both 0-indexed; column counts Unicode codepoints).
Returns None if line is past the end of the source, if
column lands past the end of its line, or if the position
isn’t on a UTF-8 boundary.
The 0-based input convention matches the Language Server
Protocol (Position { line, character }); the existing
Self::locate reverses the mapping and returns 1-based
coordinates suitable for file:line:col diagnostic display.
Don’t conflate the two.
Sourcepub fn line_bounds(&self, source: &str, byte: usize) -> Option<Range<usize>>
pub fn line_bounds(&self, source: &str, byte: usize) -> Option<Range<usize>>
Byte range of the line containing byte, with the trailing
\n trimmed. Returns None if byte is past the source end.
The slice source[range] is exactly the line text the
rustc-style pretty renderer and the JSON Lines snippet field
quote back to the user.