pub struct Editor<'a, W: Widget<A::Ui>, A: Area, S> { /* private fields */ }Expand description
A cursor 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 EditHelper.
To make edits, you can use two different functions. You can either
replace or you can insert. The former will completely
replace the Cursor’s selection, while the latter will only
place the edit before the position of the caret, which could be
either in the start or the end of the selection.
helper.edit_main(|e| {
e.replace("my replacement");
e.insert(" and my edit");
});
helper.move_main(|mut m| {
m.move_hor(" and my edit".chars().count() as i32);
m.set_anchor();
m.move_hor(-("my replacement and my edit".chars().count() as i32));
let sel: String = m.selection().into_iter().collect();
assert_eq!(sel, "my replacement and my edit".to_string());
});Implementations§
Source§impl<'a, W: Widget<A::Ui>, A: Area, S> Editor<'a, W, A, S>
impl<'a, W: Widget<A::Ui>, A: Area, S> Editor<'a, W, A, S>
Sourcepub fn replace(&mut self, edit: impl ToString)
pub fn replace(&mut self, edit: impl ToString)
Replaces the entire selection with new text
If the caret is behind the anchor (or in the same spot),
after replacing the selection, the caret 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.
If there is no selection, then this has the same effect as
insert. If you wish to append to the caret instead, see
append.
Sourcepub fn move_hor(&mut self, count: i32) -> i32
pub fn move_hor(&mut self, count: i32) -> i32
Moves the cursor horizontally. May cause vertical movement
Returns the distance moved in chars.
Sourcepub fn move_ver(&mut self, count: i32) -> i32
pub fn move_ver(&mut self, count: i32) -> i32
Moves the cursor vertically. May cause horizontal movement
Returns the distance moved in lines.
Sourcepub fn move_ver_wrapped(&mut self, count: i32)
pub fn move_ver_wrapped(&mut self, count: i32)
Moves the cursor vertically. May cause horizontal movement
Returns the distance moved in wrapped lines.
Sourcepub fn move_to(&mut self, point: Point)
pub fn move_to(&mut self, point: Point)
Moves the cursor to a Point
- If the position isn’t valid, it will move to the “maximum” position allowed.
Sourcepub fn move_to_coords(&mut self, line: usize, col: usize)
pub fn move_to_coords(&mut self, line: usize, col: usize)
Moves the cursor to a line and a column
- If the coords isn’t valid, it will move to the “maximum” position allowed.
Sourcepub fn unset_anchor(&mut self) -> Option<Point>
pub fn unset_anchor(&mut self) -> Option<Point>
Returns and takes the anchor of the Cursor.
Sourcepub fn set_anchor(&mut self)
pub fn set_anchor(&mut self)
Sets the anchor to the current caret
Sourcepub fn set_caret_on_start(&mut self) -> bool
pub fn set_caret_on_start(&mut self) -> bool
Sets the caret of the Cursor 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 Cursor on the end of the
selection
Returns true if a swap occurred
Sourcepub fn copy(&mut self) -> Editor<'_, W, A, S>
pub fn copy(&mut self) -> Editor<'_, W, A, S>
Copies the current Cursor in place
This will leave an additional Cursor with the current
selection. Do note that normal intersection rules apply, so if
at the end of the movement, this cursor intersects with any
other, they will be merged into one.
When this Editor is dropped, like with normal Editors,
its Cursor will be added to the Cursors, 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 search_fwd<R: RegexPattern>(
&mut self,
pat: R,
end: Option<Point>,
) -> impl Iterator<Item = R::Match> + '_
pub fn search_fwd<R: RegexPattern>( &mut self, pat: R, end: Option<Point>, ) -> impl Iterator<Item = R::Match> + '_
Searches the Text for a regex
The search will begin on the caret, and returns the bounding
Points, alongside the match. If an end is provided,
the search will stop at the given Point.
§Panics
If the regex is not valid, this method will panic.
fn search_nth_paren<S>(
helper: &mut EditHelper<File, impl Area, S>,
n: usize,
) {
helper.move_many(.., |mut m| {
let mut nth = m.search_fwd('(', None).nth(n);
if let Some([p0, p1]) = nth {
m.move_to(p0);
m.set_anchor();
m.move_to(p1);
}
})
}Sourcepub fn search_rev<R: RegexPattern>(
&mut self,
pat: R,
start: Option<Point>,
) -> impl Iterator<Item = R::Match> + '_
pub fn search_rev<R: RegexPattern>( &mut self, pat: R, start: Option<Point>, ) -> impl Iterator<Item = R::Match> + '_
Searches the Text for a regex, in reverse
The search will begin on the caret, and returns the bounding
Points, alongside the match. If a start is provided,
the search will stop at the given Point.
§Panics
If the regex is not valid, this method will panic.
fn search_nth_rev<S>(
helper: &mut EditHelper<File, impl Area, S>,
n: usize,
s: &str,
) {
helper.move_many(.., |mut m| {
let mut nth = m.search_rev(s, None).nth(n);
if let Some([p0, p1]) = nth {
m.move_to(p0);
m.set_anchor();
m.move_to(p1);
}
})
}Sourcepub fn matches<R: RegexPattern>(&mut self, pat: R) -> bool
pub fn matches<R: RegexPattern>(&mut self, pat: R) -> bool
Wether the current selection matches a regex pattern
Sourcepub fn selection(&self) -> Strs<'_> ⓘ
pub fn selection(&self) -> Strs<'_> ⓘ
Returns the Cursor’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 Cursor’s selection happens to be entirely
within one of these chunks, the other &str will just be
empty.
pub fn contiguous_in(&mut self, range: impl TextRange) -> &str
Sourcepub fn last_point(&self) -> Option<Point>
pub fn last_point(&self) -> Option<Point>
Returns the position of the last char if there is one
Sourcepub fn lines_on(
&mut self,
range: impl TextRange,
) -> impl DoubleEndedIterator<Item = (usize, &str)> + '_
pub fn lines_on( &mut self, range: impl TextRange, ) -> impl DoubleEndedIterator<Item = (usize, &str)> + '_
Sourcepub fn get_reader<R: Reader>(&mut self) -> Option<R::PublicReader<'_>>
pub fn get_reader<R: Reader>(&mut self) -> Option<R::PublicReader<'_>>
Gets a Reader’s public facing API, if it exists
pub fn range(&self) -> [Point; 2]
pub fn v_caret(&self) -> VPoint
pub fn v_anchor(&self) -> Option<VPoint>
Sourcepub fn anchor_is_start(&self) -> bool
pub fn anchor_is_start(&self) -> bool
Returns true if the anchor exists before the caret
pub fn text(&self) -> &Text
Source§impl<W: Widget<A::Ui>, A: Area> Editor<'_, W, A, Searcher>
Incremental search functions, only available on IncSearchers
impl<W: Widget<A::Ui>, A: Area> Editor<'_, W, A, Searcher>
Incremental search functions, only available on IncSearchers