pub struct SourceDocument { /* private fields */ }Expand description
Immutable source content retained by a compilation.
Implementations§
Source§impl SourceDocument
impl SourceDocument
Sourcepub fn origin(&self) -> &SourceOrigin
pub fn origin(&self) -> &SourceOrigin
Return this document’s stable physical or logical origin.
Sourcepub fn len(&self) -> ByteOffset
pub fn len(&self) -> ByteOffset
Return the source length as a validated compiler byte offset.
Sourcepub fn line_count(&self) -> usize
pub fn line_count(&self) -> usize
Return the number of logical lines.
Every document has at least one line. A trailing LF, CRLF, or lone CR creates a final empty line.
Sourcepub fn offset(&self, offset: usize) -> Result<ByteOffset, SourceRangeError>
pub fn offset(&self, offset: usize) -> Result<ByteOffset, SourceRangeError>
Validate and convert a byte index into a compiler byte offset.
The exclusive end-of-document offset is valid.
Sourcepub fn range(
&self,
range: Range<usize>,
) -> Result<SourceRange, SourceRangeError>
pub fn range( &self, range: Range<usize>, ) -> Result<SourceRange, SourceRangeError>
Create a checked half-open byte range in this document.
Sourcepub fn empty_range(
&self,
offset: usize,
) -> Result<SourceRange, SourceRangeError>
pub fn empty_range( &self, offset: usize, ) -> Result<SourceRange, SourceRangeError>
Create a checked empty range at a byte offset in this document.
Sourcepub fn slice(&self, range: SourceRange) -> Result<&[u8], SourceRangeError>
pub fn slice(&self, range: SourceRange) -> Result<&[u8], SourceRangeError>
Return the bytes covered by a checked source range.
Sourcepub fn line_column(
&self,
offset: ByteOffset,
) -> Result<(usize, usize), SourceRangeError>
pub fn line_column( &self, offset: ByteOffset, ) -> Result<(usize, usize), SourceRangeError>
Convert a checked byte offset to a one-based line and byte column.
The exclusive end-of-document offset is valid. Columns count bytes; callers needing editor character encodings must convert explicitly.
Sourcepub fn byte_position(
&self,
offset: ByteOffset,
) -> Result<BytePosition, PositionError>
pub fn byte_position( &self, offset: ByteOffset, ) -> Result<BytePosition, PositionError>
Convert a byte offset to a zero-based byte position.
Every offset from zero through EOF is representable, including offsets on either byte of CRLF and offsets inside invalid UTF-8.
Sourcepub fn byte_offset(
&self,
position: BytePosition,
) -> Result<ByteOffset, PositionError>
pub fn byte_offset( &self, position: BytePosition, ) -> Result<ByteOffset, PositionError>
Convert a zero-based byte position to its byte offset.
On non-final lines, positions identify every terminator byte. The offset immediately after an LF, CRLF, or lone CR is column zero of the next line. On the final line, its end position identifies EOF.
Sourcepub fn position(
&self,
offset: ByteOffset,
encoding: PositionEncoding,
) -> Result<Position, PositionError>
pub fn position( &self, offset: ByteOffset, encoding: PositionEncoding, ) -> Result<Position, PositionError>
Convert a byte offset to an editor position in an explicit encoding.
The source must be valid UTF-8, and the offset must lie on a Unicode scalar boundary. Line terminators are excluded from editor columns. The start of LF, CRLF, or lone CR maps to the line’s end position; the offset between CR and LF has no editor-position representation.
Sourcepub fn position_offset(
&self,
position: Position,
encoding: PositionEncoding,
) -> Result<ByteOffset, PositionError>
pub fn position_offset( &self, position: Position, encoding: PositionEncoding, ) -> Result<ByteOffset, PositionError>
Convert an editor position in an explicit encoding to a byte offset.
Lines and characters are zero-based. End-of-line positions map to the first byte of LF, CRLF, or lone CR; a trailing terminator creates a final empty line whose zero position maps to EOF.