Lines

Struct Lines 

Source
pub struct Lines<S>(/* private fields */);
Expand description

A text buffer and metadata about lines.

This is internally reference-counted and thus cheap to clone.

Implementations§

Source§

impl<T: AsRef<str>> Lines<T>

Source

pub fn new(text: T) -> Self

Create from the text buffer and compute the line metadata.

Source

pub fn text(&self) -> &str

The text as a string slice.

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 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 byte_to_line_column(&self, byte_idx: usize) -> Option<(usize, usize)>

Return the index of the line and column at 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.

Source§

impl Lines<String>

Source

pub fn replace(&mut self, new: &str) -> bool

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 whether any changes were made.

Source

pub fn replacement_range(&self, new: &str) -> Option<(usize, usize)>

Returns the common prefix and suffix lengths. Returns None if the old and new strings are equal.

Source

pub fn edit(&mut self, replace: Range<usize>, with: &str)

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.

Trait Implementations§

Source§

impl<S: AsRef<str>> AsRef<str> for Lines<S>

Source§

fn as_ref(&self) -> &str

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

impl<S: Clone> Clone for Lines<S>

Source§

fn clone(&self) -> Lines<S>

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<S: Hash> Hash for Lines<S>

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<S> Freeze for Lines<S>

§

impl<S> RefUnwindSafe for Lines<S>
where S: RefUnwindSafe,

§

impl<S> Send for Lines<S>
where S: Sync + Send,

§

impl<S> Sync for Lines<S>
where S: Sync + Send,

§

impl<S> Unpin for Lines<S>

§

impl<S> UnwindSafe for Lines<S>
where S: RefUnwindSafe,

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.