Skip to main content

Span

Struct Span 

Source
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 the FileId for the span and, by extension, its file system path.
  • The WorldExt::range function can be used to map the span to a Range<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.

  1. They can be detached, originating from nowhere or from the compiler itself.
  2. 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.
  3. 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.
  4. They can be an external start index, used for diagnostics on externally loaded text files. These are only accessible as part of a DiagSpan which also contains the end index. The maximum value for the start/end of these ranges is 2^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

Source

pub const fn detached() -> Self

The detached span that does not point into any file.

Source

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.

Source

pub const fn is_detached(self) -> bool

Whether the span is detached.

Source

pub const fn id(self) -> Option<FileId>

The id of the file the span points into.

Returns None if the span is detached.

Source

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.

Source

pub const fn into_raw(self) -> NonZeroU64

Extract the raw underlying number.

Source

pub fn or(self, other: Self) -> Self

Return other if self is detached and self otherwise.

Source

pub fn find(iter: impl IntoIterator<Item = Self>) -> Self

Find the first non-detached span in the iterator.

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Span

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Span

Source§

impl Debug for Span

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Span

Source§

impl From<Span> for DiagSpan

Source§

fn from(span: Span) -> Self

Converts to this type from the input type.
Source§

impl Hash for Span

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Span

Source§

fn eq(&self, other: &Span) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.