pub struct Span { /* private fields */ }Expand description
A shared source snapshot and optional byte range within it.
Implementations§
Source§impl Span
impl Span
Sourcepub fn new(source: Arc<Source>, range: Option<Range<Offset>>) -> Self
pub fn new(source: Arc<Source>, range: Option<Range<Offset>>) -> Self
Builds a span in source with an optional byte range.
Sourcepub fn ranged(source: Arc<Source>, range: Range<Offset>) -> Self
pub fn ranged(source: Arc<Source>, range: Range<Offset>) -> Self
Builds a span covering range in source.
Sourcepub fn unlocated(source: Arc<Source>) -> Self
pub fn unlocated(source: Arc<Source>) -> Self
Builds a span whose source is known but whose byte range is not.
Sourcepub fn for_text(text: impl Into<Arc<str>>) -> Self
pub fn for_text(text: impl Into<Arc<str>>) -> Self
Creates a span covering one complete synthetic source buffer.
Examples found in repository?
13 fn parse(input: &mut ParseStream) -> Result<Self, TokenParseError> {
14 let Some(token) = input.next_token() else {
15 return Err(TokenParseError::UnexpectedEnd {
16 expected: "identifier",
17 span: Span::for_text(""),
18 });
19 };
20
21 let span = token.span();
22 match token {
23 TokenTree::Ident(identifier) => Ok(Self(identifier)),
24 token => Err(TokenParseError::UnexpectedToken {
25 expected: "identifier",
26 found: token.spelling().unwrap_or("group or trivia").to_owned(),
27 span,
28 }),
29 }
30 }Sourcepub fn with_source(self, source: Arc<Source>) -> Self
pub fn with_source(self, source: Arc<Source>) -> Self
Changes the source snapshot while preserving this span’s range.
Sourcepub fn with_text(self, text: impl Into<Arc<str>>) -> Self
pub fn with_text(self, text: impl Into<Arc<str>>) -> Self
Ensures that this span resolves to exactly text.
Parsed syntax retains its original source allocation. Programmatically constructed syntax receives a compact synthetic source allocation rather than storing a second copy of its spelling in every token.
Sourcepub fn at(&self, offset: Offset) -> Self
pub fn at(&self, offset: Offset) -> Self
Creates a zero-width span at offset with this span’s source identity.
Sourcepub fn subspan(&self, start: Offset, end: Offset) -> Self
pub fn subspan(&self, start: Offset, end: Offset) -> Self
Returns a subspan at byte offsets relative to this span’s opening.
The returned span shares the same source identity. Invalid or unavailable ranges become unlocated spans in the same file.
Sourcepub fn start(&self) -> Option<Offset>
pub fn start(&self) -> Option<Offset>
The boundary this span opens at, or None when it is unlocated.
Sourcepub fn line_column_range(&self) -> Option<LineColumnRange>
pub fn line_column_range(&self) -> Option<LineColumnRange>
Returns this span as one-based line and character-column positions.
Returns None for an unlocated span, a range outside its source
snapshot, or a boundary inside a multi-byte UTF-8 character.
Sourcepub fn len(&self) -> Option<usize>
pub fn len(&self) -> Option<usize>
How many bytes this span covers, or None when it is unlocated.
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Reports whether this span covers no bytes, as the virtual marker tokens do. Unlocated syntax covers no bytes either.
Reports whether both spans identify the same source site.
Sourcepub fn contains(&self, offset: Offset) -> bool
pub fn contains(&self, offset: Offset) -> bool
Returns whether offset lies inside this span, excluding its closing
boundary.
Boundaries sit between bytes, so the closing one belongs to whatever
follows: in a+b the boundary after a is contained by +, not by a.
This is the predicate to use when exactly one span must own a position. A
zero-width span contains nothing, having no interior.
Sourcepub fn touches(&self, offset: Offset) -> bool
pub fn touches(&self, offset: Offset) -> bool
Returns whether offset lies inside this span or on either boundary.
Adjacent spans both touch the boundary between them, so this answers “which syntax is the cursor at” rather than “which syntax owns this position” — a cursor just past an identifier still touches it, and a zero-width marker is touched at its own position.
Sourcepub fn shifted(&self, edit: &TextEdit) -> Self
pub fn shifted(&self, edit: &TextEdit) -> Self
Returns this span’s position in the buffer produced by edit.
Both boundaries move under the same rule, so nothing here decides which side of an insertion a boundary belongs to. Because that rule is monotone the pair can never invert, and a zero-width span needs no special case: its two equal boundaries stay equal.
Text inserted at a span’s closing boundary therefore extends the span, which is what an unclosed group wants — it keeps reaching whatever now follows it. A span wholly removed by a deletion collapses to a zero-width span at the deletion point rather than losing its location, because it still records where the syntax used to be.