pub struct Span { /* private fields */ }Expand description
Where syntax originated and, when available, the source buffer it indexes.
Source-derived tokens retain only this value. Their spelling is recovered by slicing the shared source buffer; it is never copied into token or syntax node storage. Spans constructed only from offsets remain useful for diagnostics and synthetic trees, but naturally cannot yield source text.
Implementations§
Source§impl Span
impl Span
Sourcepub fn detached() -> Self
pub fn detached() -> Self
Creates a span with no source location.
Examples found in repository?
10 fn parse(input: &mut ParseStream) -> Result<Self, TokenParseError> {
11 let Some(token) = input.next_token() else {
12 return Err(TokenParseError::UnexpectedEnd {
13 expected: "identifier",
14 span: Span::detached(),
15 });
16 };
17
18 let span = token.span();
19 match token {
20 TokenTree::Ident(identifier) => Ok(Self(identifier)),
21 token => Err(TokenParseError::UnexpectedToken {
22 expected: "identifier",
23 found: token.spelling().unwrap_or("group or trivia").to_owned(),
24 span,
25 }),
26 }
27 }Sourcepub fn new(start: Offset, end: Offset) -> Self
pub fn new(start: Offset, end: Offset) -> Self
Builds a span covering start..end, where end is the offset past the
last byte.
An inverted pair collapses to Detached rather than fabricating a range.
Sourcepub fn from_offset(offset: Offset) -> Self
pub fn from_offset(offset: Offset) -> Self
A zero-width span at one boundary, for syntax that occupies no bytes.
Sourcepub fn with_text(self, text: impl Into<Arc<str>>) -> Self
pub fn with_text(self, text: impl Into<Arc<str>>) -> Self
Attaches the complete source fragment covered by this span.
Existing source backing is retained. For an offset-only span, text
must have the same byte width as the span; its first byte is associated
with the span’s opening offset. Detached syntax may also carry a source
fragment, which is useful for programmatically constructed syntax.
§Panics
Panics when an offset-only span and text have different byte widths.
Sourcepub fn replaced_text(&self, text: impl Into<Arc<str>>) -> Self
pub fn replaced_text(&self, text: impl Into<Arc<str>>) -> Self
Replaces this span’s source fragment while retaining its opening location and updating its width.
Sourcepub fn source(text: impl Into<Arc<str>>) -> Self
pub fn source(text: impl Into<Arc<str>>) -> Self
Creates a source-backed span covering an entire source buffer.
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 buffer. Invalid or detached ranges become detached spans.
Sourcepub fn text(&self) -> Option<&str>
pub fn text(&self) -> Option<&str>
Returns the exact source text covered by this span when source backing is available.
Reports whether both spans slice the same shared source buffer.
Sourcepub fn start(&self) -> Option<Offset>
pub fn start(&self) -> Option<Offset>
The boundary this span opens at, or None when it is detached.
Sourcepub fn len(&self) -> Option<usize>
pub fn len(&self) -> Option<usize>
How many bytes this span covers, or None when it is detached.
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. Detached syntax covers no bytes either.
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 becoming Self::Detached,
because it still records where the syntax used to be.
Sourcepub fn join(&self, other: Self) -> Self
pub fn join(&self, other: Self) -> Self
The smallest span covering both, ignoring detached operands.
Sourcepub fn meet(&self, other: Self) -> Self
pub fn meet(&self, other: Self) -> Self
The overlap of both, or Detached when they do not overlap.
Sourcepub fn join_all(spans: impl IntoIterator<Item = Span>) -> Self
pub fn join_all(spans: impl IntoIterator<Item = Span>) -> Self
Joins every span in an iterator, ignoring detached spans.
Trait Implementations§
impl Eq for Span
Source§impl Index<Span> for str
Indexes the buffer, panicking when the span does not name text in it.
impl Index<Span> for str
Indexes the buffer, panicking when the span does not name text in it.
This is legal despite Index and str both being foreign because Span
is local, and it cannot overlap str’s own
impl<I: SliceIndex<str>> Index<I> for str because Span does not implement
SliceIndex.
Source§impl Index<Span> for String
String needs its own implementation rather than reaching str’s through
deref: it already has an Index impl of its own, so operator resolution stops
there instead of falling through.
impl Index<Span> for String
String needs its own implementation rather than reaching str’s through
deref: it already has an Index impl of its own, so operator resolution stops
there instead of falling through.