Skip to main content

TextMut

Struct TextMut 

Source
pub struct TextMut<'t> { /* private fields */ }
Expand description

A struct that allows for Text modifications from the Widget::text_mut function.

It is pretty much identical to &mut Text, the difference is that you can’t reassign it to a new Text. This is done in order to prevent radical changes to the Text of the Buffer from the outside.

Implementations§

Source§

impl<'t> TextMut<'t>

Source

pub fn replace_range(&mut self, range: impl TextRange, edit: impl ToString)

Replaces a range in the Text.

§[TextRange] behavior:

If you give a single usize/Point, it will be interpreted as a range from.

Source

pub fn parts(self) -> TextParts<'t>

The parts that make up a Text.

This function is used when you want to insert/remove Tags (i.e., borrow the inner InnerTags mutably via Tags), while still being able to read from the Strs and Selections.

Source

pub fn insert_tag<Idx>(&mut self, tagger: Tagger, idx: Idx, tag: impl Tag<Idx>)

Inserts a Tag at the given position.

Source

pub fn insert_tag_after<Idx>( &mut self, tagger: Tagger, idx: Idx, tag: impl Tag<Idx>, )

Like insert_tag, but does it after other Tags with the same priority.

Source

pub fn remove_tags(&mut self, tagger: Tagger, range: impl TextRangeOrIndex)

Removes the Tags of a Tagger from a region.

The input can either be a byte index, a Point, or a range of byte indices/Points. If you are implementing a Buffer updating hook through BufferUpdated, it can be very useful to just “undo” all of the Tag additions done by previous updates, you can do that efficiently with this function:

use duat::prelude::*;
setup_duat!(setup);

fn setup() {
    let tagger = Tagger::new();

    hook::add::<BufferUpdated>(move |pa, handle| {
        let buf = handle.write(pa);
        // Removing on the whole Buffer
        buf.text_mut().remove_tags(tagger, ..);
        // Logic to add Tags with tagger...
    });
}
Source

pub fn remove_tags_excl(&mut self, tagger: Tagger, range: impl TextRangeOrIndex)

Just like TextMut::remove_tags but excludes ends on the start and starts on the end.

In the regular remove_tags function, if you remove from a range x..y, tag ranges that end in x or start in y - 1 (exclusive range) will also be removed.

If you don’t want that to happen, you can use this function instead.

Source

pub fn remove_tags_if( &mut self, tagger: Tagger, from: impl TextRangeOrIndex, filter: impl FnMut(usize, RawTag) -> bool, )

Like TextMut::remove_tags, but removes base on a predicate.

If the function returns true, then the tag is removed. Note that every RawTag in here is guaranteed to have the same Tagger as the one passed to the function, so you don’t need to chack for that.

Source

pub fn clear_tags(&mut self)

Removes all Tags.

Refrain from using this function on Buffers, as there may be other Tag providers, and you should avoid messing with their tags.

Source

pub fn undo(&mut self)

Undoes the last moment, if there was one.

Source

pub fn redo(&mut self)

Redoes the last moment in the history, if there is one.

Source

pub fn new_moment(&mut self)

Finishes the current moment and adds a new one to the history.

Source

pub fn selections_mut(self) -> &'t mut Selections

A mut reference to this Text’s Selections if they exist.

Methods from Deref<Target = Text>§

Source

pub fn is_empty(&self) -> bool

Whether or not there are any characters in the Text, besides the final b'\n'.

§Note

This does not check for tags, so with a Ghost, there could actually be a “string” of characters on the Text, it just wouldn’t be considered real “text”. If you want to check for the InnerTags’b possible emptyness as well, see Text::is_empty_empty.

Source

pub fn is_empty_empty(&self) -> bool

Whether the Strs and InnerTags are empty.

This ignores the last '\n' in the Text, since it is always there no matter what.

If you only want to check for the Strs, ignoring possible Ghosts, see is_empty.

Source

pub fn ghost_max_points_at(&self, b: usize) -> TwoPoints

The maximum points in the atth byte.

This point is essentially the point at that byte, plus the last possible Point of any Ghosts in that position.

Source

pub fn len_points(&self) -> TwoPoints

The points at the end of the text.

This will essentially return the last point of the text, alongside the last possible Point of any Ghost at the end of the text.

Source

pub fn points_after(&self, tp: TwoPoints) -> Option<TwoPoints>

Points visually after the TwoPoints.

If the TwoPoints in question is concealed, treats the next visible character as the first character, and returns the points of the next visible character.

This method is useful if you want to iterator reversibly right after a certain point, thus including the character of said point.

Source

pub fn visual_line_start(&self, points: TwoPoints, skip: usize) -> TwoPoints

The visual start of the line.

This point is defined not by where the line actually begins, but by where the last ‘\n’ was located. For example, if Tags create ghost text or omit text from multiple different lines, this point may differ from where in the Text the real line actually begins.

The skip value is how many \n should be skipped before returning.

Source

pub fn get_ghost(&self, id: GhostId) -> Option<&Text>

Gets the Ghost of a given GhostId

Source

pub fn has_unsaved_changes(&self) -> bool

Wether or not the content has changed since the last save.

Returns true only if the actual buf of the Text have been changed, ignoring Tags and all the other things, since those are not written to the filesystem.

Source

pub fn iter_fwd(&self, at: TwoPoints) -> FwdIter<'_>

A forward iterator of the chars and tags of the Text.

Source

pub fn iter_rev(&self, at: TwoPoints) -> RevIter<'_>

A reverse iterator of the chars and tags of the Text

Source

pub fn tags_fwd( &self, b: usize, lookaround: Option<usize>, ) -> Peekable<Chain<FwdBoundsBefore<'_>, FwdTagsMapper<'_>>>

A forward iterator over the Tags of the Text.

This iterator will consider some Tags before b, since their ranges may overlap with b.

The amount of tags to look for behind depeds on the internal min_len factor. You can override by providing a lookaround, which will tell Duat how many Tags to look behind. If you set it to Some(0), lookaround will be disabled.

§Note

Duat works fine with Tags in the middle of a codepoint, but external utilizers may not, so keep that in mind.

Source

pub fn tags_rev( &self, b: usize, lookaround: Option<usize>, ) -> Peekable<Chain<RevBoundsAfter<'_>, RevTagsMapper<'_>>>

An reverse iterator over the Tags of the Text.

This iterator will consider some Tags ahead of b, since their ranges may overlap with b.

The amount of tags to look for ahead depeds on the internal min_len factor. You can override by providing a lookaround, which will tell Duat how many Tags to look ahead. If you set it to Some(0), lookaround will be disabled.

§Note

Duat works fine with Tags in the middle of a codepoint, but external utilizers may not, so keep that in mind.

Source

pub fn raw_tags_fwd(&self, b: usize) -> impl Iterator<Item = (usize, RawTag)>

A forward Iterator over the RawTags.

This Iterator does not take into account Tag ranges that intersect with the starting point, unlike Text::tags_fwd

Source

pub fn raw_tags_rev(&self, b: usize) -> impl Iterator<Item = (usize, RawTag)>

A reverse Iterator over the RawTags.

This Iterator does not take into account Tag ranges that intersect with the starting point, unlike Text::tags_rev

Source

pub fn selections(&self) -> &Selections

The Selections printed to this Text, if they exist.

Source

pub fn get_main_sel(&self) -> Option<&Selection>

Gets the main Selection, if there is one.

If you want a method that doesn’t return an Option (for convenience), see Text::main_sel.

Source

pub fn main_sel(&self) -> &Selection

Gets the main Selection.

§Panics

This method will panic if there are no Selections. If you want a non-panicking method, see Text::get_main_sel.

Source

pub fn get_spawned_ids(&self) -> impl Iterator<Item = SpawnId>

A list of all SpawnIds that belong to this Text

Source

pub fn to_string_no_last_nl(&self) -> String

Returns a String, but without the \n at the end of the Text.

Normally, when you call Text::to_string, (which is actually deref’d into Strs::to_string), it will also include the last \n character, which is always a part of the Text no matter what. This function doesn’t do that.

Source

pub fn version(&self) -> TextVersion

A struct representing how many changes took place since the creation of this Text

This struct tracks all Changes and Tag additions/removals, giving you information about wether this Text has changed, when comparing this to previous TextVersions of the same Text.

This does also include things like undoing and redoing. This is done to keep track of all changes that took place, even to previously extant states of the text.

Methods from Deref<Target = Strs>§

Source

pub fn start_point(&self) -> Point

The Point at the end of the Strs.

This is the equivalent of strs.range().start.

Source

pub fn end_point(&self) -> Point

The Point at the end of the Strs.

This is the equivalent of strs.range().end.

Source

pub fn char_at(&self, p: impl TextIndex) -> Option<char>

The char at a given position.

This position can either be a Point or a byte index. Will return None if the position is greater than or equal to self.len() or if it is not located in a utf8 character boundary.

Source

pub fn point_at_byte(&self, byte: usize) -> Point

The Point corresponding to the byte position, 0 indexed.

If the byte position would fall in between two characters (because the first one comprises more than one byte), the first character is chosen as the Point where the byte is located.

§Panics

Will panic if b is greater than the length of the text.

Source

pub fn point_at_char(&self, char: usize) -> Point

The Point associated with the cth char.

§Panics

Will panic if c is greater than the number of chars in the text.

Source

pub fn point_at_coords(&self, line: usize, column: usize) -> Point

The Point where the lth line starts, 0 indexed.

If l == number_of_lines, returns the last point of the Strs.

§Panics

Will panic if the number l is greater than the number of lines on the text.

Source

pub fn line(&self, n: usize) -> &Strs

The Strs for the nth line.

§Panics

Panics if n >= self.len().line()

Source

pub fn full(&self) -> &Strs

Returns an Strs for the whole Text that this one belongs to.

You should use this function if you want to create an api that requires the whole Strs to be used as an argument. You can accept any Strs, then just transform it to the full one using this function.

If this Strs is from Strs::empty, then this will return itself.

Source

pub fn last_point(&self) -> Point

The last Point associated with a char

This function takes into account the whole Text, not just the parts contained in the Strs. And since a Text can’t be empty, it will always return a Point associated with the \n character.

Source

pub fn get(&self, range: impl TextRange) -> Option<&Strs>

Tries to get a subslice of the Strs

It will return None if the range does not start or end in valid utf8 boundaries. If you expect the value to alway be Some, consider using the index operator ([]) instead.

This method is conceptually similar to &str::get, but the reference is to an Strs struct. This struct points to a subslice of the Strss, which is actually two slices, given the internal gap buffer representation.

This type is supposed to act nearly identically with the str type, only differing in the fact that its maximum lenght is u32::MAX, not usize::MAX.

Source

pub fn slices(&self, range: impl TextRange) -> [&[u8]; 2]

Returns a struct of two &[u8] representing a [TextRange] from the slice.

Source

pub fn to_array(&self) -> [&str; 2]

Converts this Strs into an array of its two parts.

Source

pub fn lines(&self) -> Lines<'_>

Returns an iterator over the lines in a given range

The lines are inclusive, that is, it will iterate over the whole line, not just the parts within the range.

Source

pub fn bytes(&self) -> impl DoubleEndedIterator<Item = u8>

Returns and Iterator over the bytes of this Strs

Source

pub fn chars(&self) -> impl DoubleEndedIterator<Item = char>

Returns and Iterator over the chars of this Strs

Source

pub fn char_indices(&self) -> impl DoubleEndedIterator<Item = (usize, char)>

Returns an Iterator over the chars and their indices.

Source

pub fn byte_range(&self) -> Range<usize>

A Range<usize> of the bytes on this Strs.

Source

pub fn range(&self) -> Range<Point>

A Range<Point> of the bytes on this Strs.

If you just care about the byte indices (most likely), check out Strs::byte_range isntead.

Source

pub fn indent(&self, opts: PrintOpts) -> usize

Gets the indentation level of this Strs

This assumes that it is a line in the Strs, ending with \n or \r\n.

This is the total “amount of spaces”, that is, how many ' ' character equivalents are here. This depends on your PrintOpts because of the tabstop field.

Source

pub fn len(&self) -> usize

The lenght of this Strs, in bytes.

Source

pub fn is_empty(&self) -> bool

Wether the len of this Strs is equal to 0.

Source

pub fn is_empty_line(&self) -> bool

Wether this is an empty line or not

An empty line is either \n or \r\n. If you want to check wether a line is just whitespace, you can do this:

use duat::prelude::*;

fn is_whitespace(line: &Strs) -> bool {
    line.chars().all(|char| char.is_whitespace())
}

Trait Implementations§

Source§

impl<'t> Debug for TextMut<'t>

Source§

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

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

impl<'t> Deref for TextMut<'t>

Source§

type Target = Text

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl PartialEq<&str> for TextMut<'_>

Source§

fn eq(&self, other: &&str) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<String> for TextMut<'_>

Source§

fn eq(&self, other: &String) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<Text> for TextMut<'_>

Source§

fn eq(&self, other: &Text) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<TextMut<'_>> for &str

Source§

fn eq(&self, other: &TextMut<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<TextMut<'_>> for String

Source§

fn eq(&self, other: &TextMut<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<TextMut<'_>> for Text

Source§

fn eq(&self, other: &TextMut<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialEq<TextMut<'_>> for TextMut<'_>

Source§

fn eq(&self, other: &TextMut<'_>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for TextMut<'_>

Auto Trait Implementations§

§

impl<'t> Freeze for TextMut<'t>

§

impl<'t> !RefUnwindSafe for TextMut<'t>

§

impl<'t> Send for TextMut<'t>

§

impl<'t> Sync for TextMut<'t>

§

impl<'t> Unpin for TextMut<'t>

§

impl<'t> UnsafeUnpin for TextMut<'t>

§

impl<'t> !UnwindSafe for TextMut<'t>

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> 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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.