Source

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 lines(&self) -> &Lines<String>

An acceleration structure for conversion of UTF-8, UTF-16 and line/column indices.

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 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.

Typically, it’s easier to use WorldExt::range instead.

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 duplicate 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, dest: *mut u8)

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