pub struct Span(/* private fields */);Expand description
Defines a range of text in a Typst source file.
Spans are used throughout the compiler to track which source section an
element stems from or an error/warning applies to. Errors and warnings use
the DiagSpan type which can contain either a normal span or a range
targeting a location in a non-Typst file, such as a JSON parsing error.
- The
.id()method can be used to get theFileIdfor the span and, by extension, its file system path. - The
WorldExt::rangefunction can be used to map the span to aRange<usize>.
This type is stored compactly in 8 bytes, and is copyable and null-optimized
(i.e. Option<Span> also takes 8 bytes), but can be expanded for easier
usage into the SpanKind enum via Self::get().
Spans internally distinguish between four kinds of values, these are
accessible as the SpanKind or DiagSpanKind enums via the
Span::get or DiagSpan::get methods.
- They can be detached, originating from nowhere or from the compiler itself.
- They can be numbered values, corresponding to a node in a Typst source file’s concrete syntax tree. These are the most common span type and are explained more below.
- They can be raw range spans, containing a range of two indices that came
from parsing a text as Typst syntax. The file itself is not necessarily a
Typst source file. The maximum value for the start/end of these ranges is
2^23-1, larger values will be saturated. - They can be an external start index, used for diagnostics on externally
loaded text files. These are only accessible as part of a
DiagSpanwhich also contains the end index. The maximum value for the start/end of these ranges is2^46-1, larger values will be saturated.
§Numbered spans
Typst source files use numbered spans. Rather than using byte ranges, which shift a lot as you type, each syntax tree node gets a unique number.
During editing, the span numbers stay mostly stable, even for nodes behind an insertion. This is not true for simple ranges as they would shift. This allows spans to be used as inputs to memoized functions without hurting cache performance when text is inserted somewhere in the document other than the end.
Span numbers are ordered in the syntax tree to enable quickly finding the node of a known span:
- The span number of a parent node is always smaller than the number of any of its children
- The span numbers of sibling nodes always increase from left to right
Combining those guarantees, we have that for siblings in order [A, B, C], the span numbers for node A and all of A’s children are less than node B’s span number, and the numbers for node C and all of C’s children are greater than B’s span number.
Implementations§
Source§impl Span
impl Span
Sourcepub const fn from_raw(v: NonZeroU64) -> Self
pub const fn from_raw(v: NonZeroU64) -> Self
Construct from a raw number.
Should only be used with numbers retrieved via
into_raw. Misuse may results in panics, but no
unsafety.
Sourcepub const fn is_detached(self) -> bool
pub const fn is_detached(self) -> bool
Whether the span is detached.
Sourcepub const fn id(self) -> Option<FileId>
pub const fn id(self) -> Option<FileId>
The id of the file the span points into.
Returns None if the span is detached.
Sourcepub const fn get(self) -> SpanKind
pub const fn get(self) -> SpanKind
Unpack the span into the variants of a SpanKind for easier use.
To access a range, you may want to use WorldExt::range instead.
Sourcepub const fn into_raw(self) -> NonZeroU64
pub const fn into_raw(self) -> NonZeroU64
Extract the raw underlying number.
Sourcepub fn find(iter: impl IntoIterator<Item = Self>) -> Self
pub fn find(iter: impl IntoIterator<Item = Self>) -> Self
Find the first non-detached span in the iterator.
Trait Implementations§
impl Copy for Span
impl Eq for Span
impl StructuralPartialEq for Span
Auto Trait Implementations§
impl Freeze for Span
impl RefUnwindSafe for Span
impl Send for Span
impl Sync for Span
impl Unpin for Span
impl UnsafeUnpin for Span
impl UnwindSafe for Span
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more