pub struct SourceMap {
pub sources: Vec<SourceInfo>,
pub next_id: u32,
}Expand description
The central registry that owns all source fragments.
SourceMap assigns each registered fragment a unique SourceId and
provides utilities for converting between byte offsets and line/column
positions.
Fields§
§sources: Vec<SourceInfo>All registered source fragments, indexed by their SourceId.
next_id: u32The next SourceId to assign.
Implementations§
Source§impl SourceMap
impl SourceMap
Sourcepub fn add_file(&mut self, name: &str, content: String) -> SourceId
pub fn add_file(&mut self, name: &str, content: String) -> SourceId
Register a source file and return its SourceId.
Sourcepub fn add_macro_expansion(
&mut self,
macro_name: &str,
site: Span,
content: String,
) -> SourceId
pub fn add_macro_expansion( &mut self, macro_name: &str, site: Span, content: String, ) -> SourceId
Register a macro expansion and return its SourceId.
Sourcepub fn get(&self, id: SourceId) -> Option<&SourceInfo>
pub fn get(&self, id: SourceId) -> Option<&SourceInfo>
Look up a source by its identifier.
Sourcepub fn span_to_position(&self, span: &Span) -> Option<LineColumn>
pub fn span_to_position(&self, span: &Span) -> Option<LineColumn>
Convert a byte-offset span to a LineColumn.
Returns the position of the start of the span. Returns None if
the source is unknown or the offset is out of range.
Sourcepub fn position_to_offset(&self, lc: &LineColumn) -> Option<usize>
pub fn position_to_offset(&self, lc: &LineColumn) -> Option<usize>
Convert a LineColumn back to a byte offset.
Returns None if the source is unknown or the position is out of range.
Sourcepub fn span_text(&self, span: &Span) -> Option<&str>
pub fn span_text(&self, span: &Span) -> Option<&str>
Return the text slice covered by span.
Returns None if the source is unknown or the span is out of range.
Sourcepub fn chain_origin(&self, span: &Span) -> Vec<Span>
pub fn chain_origin(&self, span: &Span) -> Vec<Span>
Walk back through macro expansion sites and collect origin spans.
If span belongs to a macro-expanded source, this function follows the
expansion chain until it reaches a non-expanded source. The returned
vector goes from span (innermost) to the original user-written span
(outermost).