typst_syntax

Struct Source

source
pub struct Source(/* private fields */);
Expand description

A source file.

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

source

pub fn new(id: FileId, text: String) -> Self

Create a new source file.

source

pub fn detached(text: impl Into<String>) -> Self

Create a source file without a real id and path, usually for testing.

source

pub fn root(&self) -> &SyntaxNode

The root node of the file’s untyped syntax tree.

source

pub fn id(&self) -> FileId

The id of the source file.

source

pub fn text(&self) -> &str

The whole source as a string slice.

source

pub fn get(&self, range: Range<usize>) -> Option<&str>

Slice out the part of the source code enclosed by the range.

source

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.

source

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.

source

pub fn len_bytes(&self) -> usize

Get the length of the file in UTF-8 encoded bytes.

source

pub fn len_utf16(&self) -> usize

Get the length of the file in UTF-16 code units.

source

pub fn len_lines(&self) -> usize

Get the length of the file in lines.

source

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.

source

pub fn range(&self, span: Span) -> Option<Range<usize>>

Get the byte range for the given span in this file.

Returns None if the span does not point into this source file.

source

pub fn byte_to_utf16(&self, byte_idx: usize) -> Option<usize>

Return the index of the UTF-16 code unit at the byte index.

source

pub fn byte_to_line(&self, byte_idx: usize) -> Option<usize>

Return the index of the line that contains the given byte index.

source

pub fn byte_to_column(&self, byte_idx: usize) -> Option<usize>

Return the index of the column at the byte index.

The column is defined as the number of characters in the line before the byte index.

source

pub fn utf16_to_byte(&self, utf16_idx: usize) -> Option<usize>

Return the byte index at the UTF-16 code unit.

source

pub fn line_to_byte(&self, line_idx: usize) -> Option<usize>

Return the byte position at which the given line starts.

source

pub fn line_to_range(&self, line_idx: usize) -> Option<Range<usize>>

Return the range which encloses the given line.

source

pub fn line_column_to_byte( &self, line_idx: usize, column_idx: usize, ) -> Option<usize>

Return the byte index of the given (line, column) pair.

The column defines the number of characters to go beyond the start of the line.

Trait Implementations§

source§

impl AsRef<str> for Source

source§

fn as_ref(&self) -> &str

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for Source

source§

fn clone(&self) -> Source

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Source

source§

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

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

impl Hash for Source

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

Auto Trait Implementations§

§

impl Freeze for Source

§

impl RefUnwindSafe for Source

§

impl Send for Source

§

impl Sync for Source

§

impl Unpin for Source

§

impl UnwindSafe for Source

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, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
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.