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>
impl<'t> TextMut<'t>
Sourcepub fn replace_range(&mut self, range: impl TextRange, edit: impl ToString)
pub fn replace_range(&mut self, range: impl TextRange, edit: impl ToString)
Sourcepub fn insert_tag<Idx>(&mut self, tagger: Tagger, idx: Idx, tag: impl Tag<Idx>)
pub fn insert_tag<Idx>(&mut self, tagger: Tagger, idx: Idx, tag: impl Tag<Idx>)
Inserts a Tag at the given position.
Sourcepub fn insert_tag_after<Idx>(
&mut self,
tagger: Tagger,
idx: Idx,
tag: impl Tag<Idx>,
)
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.
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...
});
}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.
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.
Sourcepub fn new_moment(&mut self)
pub fn new_moment(&mut self)
Finishes the current moment and adds a new one to the history.
Sourcepub fn selections_mut(self) -> &'t mut Selections
pub fn selections_mut(self) -> &'t mut Selections
A mut reference to this Text’s Selections if they
exist.
Methods from Deref<Target = Text>§
Sourcepub fn is_empty(&self) -> bool
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.
Sourcepub fn is_empty_empty(&self) -> bool
pub fn is_empty_empty(&self) -> bool
Sourcepub fn ghost_max_points_at(&self, b: usize) -> TwoPoints
pub fn ghost_max_points_at(&self, b: usize) -> TwoPoints
Sourcepub fn len_points(&self) -> TwoPoints
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.
Sourcepub fn points_after(&self, tp: TwoPoints) -> Option<TwoPoints>
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.
Sourcepub fn visual_line_start(&self, points: TwoPoints, skip: usize) -> TwoPoints
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.
Sourcepub fn has_unsaved_changes(&self) -> bool
pub fn has_unsaved_changes(&self) -> bool
Sourcepub fn iter_fwd(&self, at: TwoPoints) -> FwdIter<'_> ⓘ
pub fn iter_fwd(&self, at: TwoPoints) -> FwdIter<'_> ⓘ
A forward iterator of the chars and tags of the Text.
Sourcepub fn iter_rev(&self, at: TwoPoints) -> RevIter<'_> ⓘ
pub fn iter_rev(&self, at: TwoPoints) -> RevIter<'_> ⓘ
A reverse iterator of the chars and tags of the Text
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.
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.
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
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
Sourcepub fn selections(&self) -> &Selections
pub fn selections(&self) -> &Selections
The Selections printed to this Text, if they exist.
Sourcepub fn get_main_sel(&self) -> Option<&Selection>
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.
Sourcepub fn main_sel(&self) -> &Selection
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.
Sourcepub fn get_spawned_ids(&self) -> impl Iterator<Item = SpawnId>
pub fn get_spawned_ids(&self) -> impl Iterator<Item = SpawnId>
A list of all SpawnIds that belong to this Text
Sourcepub fn to_string_no_last_nl(&self) -> String
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.
Sourcepub fn version(&self) -> TextVersion
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>§
Sourcepub fn start_point(&self) -> Point
pub fn start_point(&self) -> Point
The Point at the end of the Strs.
This is the equivalent of strs.range().start.
Sourcepub fn end_point(&self) -> Point
pub fn end_point(&self) -> Point
The Point at the end of the Strs.
This is the equivalent of strs.range().end.
Sourcepub fn point_at_byte(&self, byte: usize) -> Point
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.
Sourcepub fn point_at_char(&self, char: usize) -> Point
pub fn point_at_char(&self, char: usize) -> Point
Sourcepub fn point_at_coords(&self, line: usize, column: usize) -> Point
pub fn point_at_coords(&self, line: usize, column: usize) -> Point
Sourcepub fn full(&self) -> &Strs
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.
Sourcepub fn last_point(&self) -> Point
pub fn last_point(&self) -> Point
Sourcepub fn get(&self, range: impl TextRange) -> Option<&Strs>
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.
Sourcepub fn slices(&self, range: impl TextRange) -> [&[u8]; 2]
pub fn slices(&self, range: impl TextRange) -> [&[u8]; 2]
Returns a struct of two &[u8] representing a [TextRange]
from the slice.
Sourcepub fn lines(&self) -> Lines<'_> ⓘ
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.
Sourcepub fn bytes(&self) -> impl DoubleEndedIterator<Item = u8>
pub fn bytes(&self) -> impl DoubleEndedIterator<Item = u8>
Sourcepub fn chars(&self) -> impl DoubleEndedIterator<Item = char>
pub fn chars(&self) -> impl DoubleEndedIterator<Item = char>
Sourcepub fn char_indices(&self) -> impl DoubleEndedIterator<Item = (usize, char)>
pub fn char_indices(&self) -> impl DoubleEndedIterator<Item = (usize, char)>
Sourcepub fn byte_range(&self) -> Range<usize>
pub fn byte_range(&self) -> Range<usize>
A Range<usize> of the bytes on this Strs.
Sourcepub fn range(&self) -> Range<Point>
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.
Sourcepub fn indent(&self, opts: PrintOpts) -> usize
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.
Sourcepub fn is_empty_line(&self) -> bool
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())
}