pub struct SourceContent { /* private fields */ }
Expand description
Represents key information about a source file and its content:
- The path to the file (or its name, in the case of virtual files)
- The content of the file
- The byte offsets of every line in the file, for use in looking up line/column information
Implementations§
Source§impl SourceContent
impl SourceContent
Sourcepub fn new(path: Arc<str>, content: Box<str>) -> Self
pub fn new(path: Arc<str>, content: Box<str>) -> Self
Create a new SourceContent from the (possibly virtual) file path, and its content as a UTF-8 string.
When created, the line starts for this file will be computed, which requires scanning the file content once.
Sourcepub fn path(&self) -> &Path
pub fn path(&self) -> &Path
Get the name of this source file as a std::path::Path
Sourcepub fn source_range(&self) -> Range<ByteIndex>
pub fn source_range(&self) -> Range<ByteIndex>
Returns the range of valid byte indices for this file
Sourcepub fn source_slice(&self, span: impl Into<Range<usize>>) -> Option<&str>
pub fn source_slice(&self, span: impl Into<Range<usize>>) -> Option<&str>
Returns a subset of the underlying content as a string slice.
The bounds of the given span are character indices, not byte indices.
Returns None
if the given span is out of bounds
Sourcepub fn line_start(&self, line_index: LineIndex) -> Option<ByteIndex>
pub fn line_start(&self, line_index: LineIndex) -> Option<ByteIndex>
Returns the byte index at which the line corresponding to line_index
starts
Returns None
if the given index is out of bounds
Sourcepub fn last_line_index(&self) -> LineIndex
pub fn last_line_index(&self) -> LineIndex
Returns the index of the last line in this file
Sourcepub fn line_range(&self, line_index: LineIndex) -> Option<Range<ByteIndex>>
pub fn line_range(&self, line_index: LineIndex) -> Option<Range<ByteIndex>>
Get the range of byte indices covered by the given line
Sourcepub fn line_index(&self, byte_index: ByteIndex) -> LineIndex
pub fn line_index(&self, byte_index: ByteIndex) -> LineIndex
Get the index of the line to which byte_index
belongs
Sourcepub fn line_column_to_offset(
&self,
line_index: LineIndex,
column_index: ColumnIndex,
) -> Option<ByteIndex>
pub fn line_column_to_offset( &self, line_index: LineIndex, column_index: ColumnIndex, ) -> Option<ByteIndex>
Get the ByteIndex corresponding to the given line and column indices.
Returns None
if the line or column indices are out of bounds.
Sourcepub fn location(&self, byte_index: ByteIndex) -> Option<FileLineCol>
pub fn location(&self, byte_index: ByteIndex) -> Option<FileLineCol>
Get a FileLineCol corresponding to the line/column in this file at which byte_index
occurs
Trait Implementations§
Source§impl Clone for SourceContent
impl Clone for SourceContent
Source§fn clone(&self) -> SourceContent
fn clone(&self) -> SourceContent
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more