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
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 root(&self) -> &SyntaxNode
pub fn root(&self) -> &SyntaxNode
The root node of the file’s untyped syntax tree.
sourcepub fn get(&self, range: Range<usize>) -> Option<&str>
pub fn get(&self, range: Range<usize>) -> Option<&str>
Slice out the part of the source code enclosed by the range.
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, span: Span) -> Option<Range<usize>>
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.
sourcepub fn byte_to_utf16(&self, byte_idx: usize) -> Option<usize>
pub fn byte_to_utf16(&self, byte_idx: usize) -> Option<usize>
Return the index of the UTF-16 code unit at the byte index.
sourcepub fn byte_to_line(&self, byte_idx: usize) -> Option<usize>
pub fn byte_to_line(&self, byte_idx: usize) -> Option<usize>
Return the index of the line that contains the given byte index.
sourcepub fn byte_to_column(&self, byte_idx: usize) -> Option<usize>
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.
sourcepub fn utf16_to_byte(&self, utf16_idx: usize) -> Option<usize>
pub fn utf16_to_byte(&self, utf16_idx: usize) -> Option<usize>
Return the byte index at the UTF-16 code unit.
sourcepub fn line_to_byte(&self, line_idx: usize) -> Option<usize>
pub fn line_to_byte(&self, line_idx: usize) -> Option<usize>
Return the byte position at which the given line starts.
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 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§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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