pub struct Source(/* private fields */);Expand description
A Typst source file containing the full source text, a mapping from byte indices to lines/columns, and the parsed syntax tree.
All line and column indices start at zero, just like byte indices. Only for user-facing display, you should add 1 to them.
Values of this type are cheap to clone and hash.
Implementations§
Source§impl Source
impl Source
Sourcepub fn detached(text: impl Into<String>) -> Self
pub fn detached(text: impl Into<String>) -> Self
Create a source file without a real id and path, usually for testing.
Sourcepub fn with_root(id: FileId, text: String, root: SyntaxNode) -> Self
pub fn with_root(id: FileId, text: String, root: SyntaxNode) -> Self
Create a new source file with an already created syntax tree.
Sourcepub fn root(&self) -> &SyntaxNode
pub fn root(&self) -> &SyntaxNode
The root node of the file’s untyped syntax tree.
Sourcepub fn lines(&self) -> &Lines<String>
pub fn lines(&self) -> &Lines<String>
An acceleration structure for conversion of UTF-8, UTF-16 and line/column indices.
Sourcepub fn replace(&mut self, new: &str) -> Range<usize>
pub fn replace(&mut self, new: &str) -> Range<usize>
Fully replace the source text.
This performs a naive (suffix/prefix-based) diff of the old and new text
to produce the smallest single edit that transforms old into new and
then calls edit with it.
Returns the range in the new source that was ultimately reparsed.
Sourcepub fn edit(&mut self, replace: Range<usize>, with: &str) -> Range<usize>
pub fn edit(&mut self, replace: Range<usize>, with: &str) -> Range<usize>
Edit the source file by replacing the given range.
Returns the range in the new source that was ultimately reparsed.
The method panics if the replace range is out of bounds.
Sourcepub fn find(&self, span: Span) -> Option<LinkedNode<'_>>
pub fn find(&self, span: Span) -> Option<LinkedNode<'_>>
Find the node with the given span.
Returns None if the span does not point into this source file.
Sourcepub fn range(
&self,
num: SpanNumber,
sub_range: Option<SubRange>,
) -> Option<Range<usize>>
pub fn range( &self, num: SpanNumber, sub_range: Option<SubRange>, ) -> Option<Range<usize>>
Get the byte range for the given span number (and optional sub-range) in this file.
The main way to get a SpanNumber is by unpacking a span with
Span::get, but it’s likely easier to use WorldExt::range instead.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Source
impl RefUnwindSafe for Source
impl Send for Source
impl Sync for Source
impl Unpin for Source
impl UnsafeUnpin for Source
impl UnwindSafe for Source
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<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