pub struct SourceFile { /* private fields */ }Expand description
One source file, with the line index a caret needs.
Line starts are computed once at construction. Every later question about a position is a binary search over that vector rather than a rescan, which matters because a diagnostic-heavy run asks the question many times per file.
Implementations§
Source§impl SourceFile
impl SourceFile
Sourcepub const fn name(&self) -> &SourceName
pub const fn name(&self) -> &SourceName
Returns where this file came from.
Sourcepub fn len(&self) -> u32
pub fn len(&self) -> u32
Returns the source length in bytes.
§Panics
Never for a file a SourceMap holds: SourceMap::add refuses text
whose length does not fit in a u32 offset.
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Returns how many lines the file has.
A file always has at least one line, and a trailing newline does not open an extra one.
Sourcepub fn line_col(&self, offset: u32) -> LineCol
pub fn line_col(&self, offset: u32) -> LineCol
Returns the one-based line and column of offset.
§Panics
Panics when offset lies past the end of the file, or inside a
multi-byte character. Both are defects in whatever produced the offset:
spans come from a lexer whose token extents partition the file.
Sourcepub fn offset(&self, at: LineCol) -> Option<u32>
pub fn offset(&self, at: LineCol) -> Option<u32>
Returns the offset at names, the inverse of SourceFile::line_col:
None when the file has no such line, or the line no such column. A
line’s columns end at its line break, or one past the file’s last
character on its last line.
Sourcepub fn line_index(&self, offset: u32) -> usize
pub fn line_index(&self, offset: u32) -> usize
Returns the zero-based index of the line containing offset.