pub struct SourceMap { /* private fields */ }Expand description
Pre-computed index of line-start byte offsets for a source string.
Construction is O(n) in the source length. Each lookup is O(log n) in the number of lines (binary search), which is effectively O(1) for typical files.
Implementations§
Source§impl SourceMap
impl SourceMap
Sourcepub fn empty() -> Self
pub fn empty() -> Self
A no-op map for callers that never query line/column positions.
Returned by parse_arena_raw.
offset_to_line_col on an empty map returns LineCol { line: 0, col: offset }.
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Total number of lines in the source.
Sourcepub fn line_start(&self, line: u32) -> Option<u32>
pub fn line_start(&self, line: u32) -> Option<u32>
Byte offset where the given 0-based line starts.
Returns None if the line is out of range.
Sourcepub fn offset_to_line_col(&self, offset: u32) -> LineCol
pub fn offset_to_line_col(&self, offset: u32) -> LineCol
Convert a byte offset to a 0-based line/column.
If offset is past the end of the source, the position is clamped to
the last line.
Sourcepub fn span_to_line_col(&self, span: Span) -> LineColSpan
pub fn span_to_line_col(&self, span: Span) -> LineColSpan
Convert a Span to a start/end LineColSpan.
Sourcepub fn line_col_to_offset(&self, lc: LineCol) -> Option<u32>
pub fn line_col_to_offset(&self, lc: LineCol) -> Option<u32>
Convert a 0-based line/column back to a byte offset.
Returns None if the line is out of range.