pub struct Cursor<'w, W: Widget + ?Sized = Buffer> { /* private fields */ }Expand description
A selection that can edit Text, but can’t alter selections.
This struct will be used only inside functions passed to the
edit_* family of methods from the Handle.
To make edits, you can use three different functions. You can,
those being replace, insert, and append. replace
will completely replace the Selection’s selection. insert
will place text behind the caret, and append will place it
after the caret.
You can also move the Selection’s selection in many different
ways, which are described below, in the impl section for this
struct.
let sel = handle.edit_main(&mut pa, |mut c| {
c.set_anchor();
c.set_caret_on_end();
c.replace("my replacement");
c.append(" and my edit");
c.swap_ends();
c.insert("This is ");
c.swap_ends();
c.move_hor(" and my edit".chars().count() as i32);
c.set_anchor();
c.move_hor(-("This is my replacement and my edit".chars().count() as i32));
c.selection().to_string()
});
assert_eq!(&sel, "This is my replacement and my edit");Implementations§
Source§impl<'w, W: Widget + ?Sized> Cursor<'w, W>
impl<'w, W: Widget + ?Sized> Cursor<'w, W>
Sourcepub fn replace(&mut self, edit: impl ToString)
pub fn replace(&mut self, edit: impl ToString)
Replaces the entire selection with new text.
If there is a selection, then it is treated as inclusive,
therefore, a selection where caret == anchor will remove the
character where the caret is. If there is no selection, then
this has the same effect as insert. If you wish to
append to the caret instead, see append.
After replacing the sele tion, if the caret is behind the
anchor (or in the same spot), it will be placed on the start
of the selection, while the anchor will be placed on the
new end. If it is ahead, it will be placed ahead.
Sourcepub fn move_hor(&mut self, by: i32) -> i32
pub fn move_hor(&mut self, by: i32) -> i32
Moves the selection horizontally. May cause vertical movement.
Returns the distance traveled, in character indices
Sourcepub fn move_ver(&mut self, by: i32) -> bool
pub fn move_ver(&mut self, by: i32) -> bool
Moves the selection vertically. May cause horizontal movement.
Returns true if the caret actually moved at all.
Sourcepub fn move_ver_wrapped(&mut self, count: i32) -> bool
pub fn move_ver_wrapped(&mut self, count: i32) -> bool
Moves the selection vertically a number of wrapped lines. May cause horizontal movement.
Returns true if the caret actually moved at all.
Sourcepub fn move_to(&mut self, point_or_points: impl CaretOrRange)
pub fn move_to(&mut self, point_or_points: impl CaretOrRange)
Moves the selection to a Point or a range of Points.
If you give it just a Point, it will move the caret,
without affecting the anchor. If you give it a range of
Points, the anchor will be placed at the start, while the
caret will be placed at the end of said range. You can flip
those positions with a function like swap_ends.
If a Point is not valid, it will be corrected and clamped
to the lenght of the Text.
Sourcepub fn move_to_start(&mut self)
pub fn move_to_start(&mut self)
Moves the selection to Point::default, i.c., the start of
the Text.
Sourcepub fn move_to_coords(&mut self, line: usize, column: usize)
pub fn move_to_coords(&mut self, line: usize, column: usize)
Moves the selection to a line and a column.
- If the coords isn’t valid, it will move to the “maximum” position allowed.
Sourcepub fn move_to_col(&mut self, column: usize)
pub fn move_to_col(&mut self, column: usize)
Moves to a column on the current line.
Sourcepub fn unset_anchor(&mut self) -> Option<Point>
pub fn unset_anchor(&mut self) -> Option<Point>
Returns and takes the anchor of the Selection, if there
was one.
Sourcepub fn set_anchor(&mut self)
pub fn set_anchor(&mut self)
Sets the anchor to the current caret.
Sourcepub fn set_anchor_if_needed(&mut self) -> bool
pub fn set_anchor_if_needed(&mut self) -> bool
Sets the anchor if it was not already set.
Returns true if the anchor was set by this command.
Sourcepub fn set_caret_on_start(&mut self) -> bool
pub fn set_caret_on_start(&mut self) -> bool
Sets the caret of the Selection on the start of the
selection.
Returns true if a swap occurred
Sourcepub fn set_caret_on_end(&mut self) -> bool
pub fn set_caret_on_end(&mut self) -> bool
Sets the caret of the Selection on the end of the
selection.
Returns true if a swap occurred
Sourcepub fn copy(&mut self) -> Cursor<'_, W>
pub fn copy(&mut self) -> Cursor<'_, W>
Copies the current Selection in place.
This will leave an additional Selection with the current
selection. Do note that normal intersection rules apply, so if
at the end of the movement, this selection intersects with any
other, they will be merged into one.
When this Cursor is dropped, like with normal Cursors,
its Selection will be added to the Selections, unless
you destroy it.
Sourcepub fn set_desired_vcol(&mut self, x: usize)
pub fn set_desired_vcol(&mut self, x: usize)
Sets the “desired visual column”.
The desired visual column determines at what point in a line the caret will be placed when moving up and down through lines of varying lengths.
Will also set the “desired wrapped visual column”, which is the same thing but used when moving vertically in a wrapped fashion.
Sourcepub fn matches_pat<R: RegexPattern>(&self, pat: R) -> bool
pub fn matches_pat<R: RegexPattern>(&self, pat: R) -> bool
Wether the current selection matches a regex pattern.
Sourcepub fn search<R: RegexPattern>(&self, pat: R) -> CursorMatches<'_, R> ⓘ
pub fn search<R: RegexPattern>(&self, pat: R) -> CursorMatches<'_, R> ⓘ
Returns an Iterator over the matches of a
RegexPattern.
This Iterator normally covers the entire range of the
Text, however, there are methods that you can use to
narrow it down to ranges relative to the Cursor’s caret.
For example, CursorMatches::from_caret will narrow the
searched range from the beginning of the caret’s char all
the way until the end of the Text.
This Iterator also implements DoubleEndedIterator, which
means you can search in reverse as well.
Sourcepub fn selection(&self) -> &Strs
pub fn selection(&self) -> &Strs
Returns the Selection’s selection.
The reason why this return value is IntoIter<&str, 2> is
because the Text utilizes an underlying GapBuffer
to store the characters. This means that the text is
always separated into two distinct chunks.
If this Selection’s selection happens to be entirely
within one of these chunks, the other &str will just be
empty.
Sourcepub fn strs(&self, range: impl TextRange) -> &Strs
pub fn strs(&self, range: impl TextRange) -> &Strs
Returns the Strs for the given [TextRange].
§Panics
Panics if the range doesn’t start and end in valid utf8
boundaries. If you’d like to handle that scenario, check out
Cursor::try_strs.
Sourcepub fn try_strs(&self, range: impl TextRange) -> Option<&Strs>
pub fn try_strs(&self, range: impl TextRange) -> Option<&Strs>
Returns the Strs for the given [TextRange].
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 Cursor::strs isntead.
Sourcepub fn last_point(&self) -> Point
pub fn last_point(&self) -> Point
Returns the position of the last char if there is one.
Sourcepub fn indent_on(&self, line: usize) -> usize
pub fn indent_on(&self, line: usize) -> usize
Gets the indentation level on a given line.
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 range(&self) -> Range<Point>
pub fn range(&self) -> Range<Point>
The Point range of the Selection.
This is an inclusive range (not Rust’s RangeInclusive
however), this means that, even if there is no anchor, the
lenght of this range will always be at least 1.
If you want an exclusive range, see Cursor::range_excl.
Sourcepub fn range_excl(&self) -> Range<Point>
pub fn range_excl(&self) -> Range<Point>
An exclusive Point range of the Selection.
If you wish for an inclusive range, whose length is always
greater than or equal to 1, see RangeInclusive.
Sourcepub fn anchor_is_start(&self) -> bool
pub fn anchor_is_start(&self) -> bool
Returns true if the anchor exists before the caret.