pub struct SourceFile { /* private fields */ }Expand description
A file’s contents along with its display name
This is used for reporting rustc-style diagnostics where we show where in the file we found a problem. It contains an Arc so that it’s ~free for everything to pass/copy these around and produce better diagnostics.
Implementations§
Source§impl SourceFile
impl SourceFile
Sourcepub fn new_empty(origin_path: &str) -> Self
pub fn new_empty(origin_path: &str) -> Self
Create an empty SourceFile with the given name.
See SourceFile::new for details.
Sourcepub fn new(origin_path: &str, contents: String) -> Self
pub fn new(origin_path: &str, contents: String) -> Self
Create a new source file with the given name and contents.
This is intended for situations where you have the contents already and just want a SourceFile to manage it. This is appropriate for strings that were constructed dynamically or for tests.
The origin_path will be used as the filename as well.
Sourcepub fn load_local(origin_path: impl AsRef<Utf8Path>) -> Result<SourceFile>
pub fn load_local(origin_path: impl AsRef<Utf8Path>) -> Result<SourceFile>
SourceFile equivalent of LocalAsset::load_asset
Sourcepub fn origin_path(&self) -> &str
pub fn origin_path(&self) -> &str
Get the origin_path of a SourceFile
Sourcepub fn span_for_line_col(&self, line: usize, col: usize) -> Option<SourceSpan>
pub fn span_for_line_col(&self, line: usize, col: usize) -> Option<SourceSpan>
Gets a proper SourceSpan from a line-and-column representation
Both values are 1’s based, so (1, 1) is the start of the file.
If anything underflows/overflows or goes out of bounds then we’ll
just return None. unwrap_or_default() will give you the empty span from that.
This is a pretty heavy-weight process, we have to basically linearly scan the source for this position!
Sourcepub fn span_for_substr(&self, substr: &str) -> Option<SourceSpan>
pub fn span_for_substr(&self, substr: &str) -> Option<SourceSpan>
Creates a span for an item using a substring of contents
Note that substr must be a literal substring, as in it must be a pointer into the same string! If it’s not we’ll return None.
Trait Implementations§
Source§impl Clone for SourceFile
impl Clone for SourceFile
Source§fn clone(&self) -> SourceFile
fn clone(&self) -> SourceFile
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SourceFile
impl Debug for SourceFile
Source§impl PartialEq for SourceFile
impl PartialEq for SourceFile
Source§impl SourceCode for SourceFile
impl SourceCode for SourceFile
Source§fn read_span<'a>(
&'a self,
span: &SourceSpan,
context_lines_before: usize,
context_lines_after: usize,
) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError>
fn read_span<'a>( &'a self, span: &SourceSpan, context_lines_before: usize, context_lines_after: usize, ) -> Result<Box<dyn SpanContents<'a> + 'a>, MietteError>
SourceCode, keeping a
certain number of lines before and after the span as context.