pub struct EditHelper<'a, W, A, S>{ /* private fields */ }Expand description
A struct used by Modes to edit Text
You will want to use this struct when editing Widgets
with Cursors. For example, let’s say you want to create an
mode for the File widget:
/// A very basic example Mode.
#[derive(Clone)]
struct PlacesCharactersAndMoves;
impl<U: Ui> Mode<U> for PlacesCharactersAndMoves {
type Widget = File;
/* ... */In order to modify the widget, you must implement the
Mode::send_key method. In it, you receive the following:
- The key.
- A
&mut Self::Widget. - An
Area, which you can resize and modify it in other ways. - The current
Cursorsof the widget, these should be modified by theEditHelper.
In a Mode without cursors, you’d probably want to run
Cursors::clear, in order to make sure there are no cursors.
impl<U: Ui> Mode<U> for PlacesCharactersAndMoves {
/* ... */
fn send_key(&mut self, key: KeyEvent, widget: &mut File, area: &U::Area) {
match key {
// actions based on the key pressed
key!(KeyCode::Char('c')) => {
/* Do something when the character 'c' is typed. */
}
/* Matching the rest of the keys */
}
}(You can use the key! macro in order to match KeyEvents).
With the EditHelper, you can modify Text in a simplified
way. This is done by two actions, editing and moving. You
can only do one of these on any number of cursors at the same
time.
impl<U: Ui> Mode<U> for PlacesCharactersAndMoves {
/* ... */
fn send_key(&mut self, key: KeyEvent, file: &mut File, area: &U::Area) {
let mut helper = EditHelper::new(file, area);
helper.cursors_mut().make_excl();
match key {
key!(KeyCode::Char(c)) => {
helper.edit_many(.., |e| e.insert('c'));
helper.move_many(.., |mut m| m.move_hor(1));
},
key!(KeyCode::Right, KeyMod::SHIFT) => {
helper.move_many(.., |mut m| {
if m.anchor().is_none() {
m.set_anchor()
}
m.move_hor(1)
})
}
key!(KeyCode::Right) => {
helper.move_many(.., |mut m| {
m.unset_anchor();
m.move_hor(1)
})
}
/* Predictable remaining implementations */
}
}Notice the Cursors::make_excl. In Duat, there are two types of
Cursors, inclusive and exclusive. The only difference between
them is that in inclusive cursors, the selection acts like a Rust
inclusive selection (..=), while in exclusive cursors, it acts
like an exclusive selection (..).
Implementations§
Source§impl<'a, W, A> EditHelper<'a, W, A, ()>
impl<'a, W, A> EditHelper<'a, W, A, ()>
Sourcepub fn new(widget: &'a mut W, area: &'a A) -> Self
pub fn new(widget: &'a mut W, area: &'a A) -> Self
Returns a new instance of EditHelper
Source§impl<W, A, S> EditHelper<'_, W, A, S>
impl<W, A, S> EditHelper<'_, W, A, S>
Sourcepub fn edit_many(
&mut self,
range: impl RangeBounds<usize> + Clone,
f: impl FnMut(&mut Editor<'_, '_, A, W>),
)
pub fn edit_many( &mut self, range: impl RangeBounds<usize> + Clone, f: impl FnMut(&mut Editor<'_, '_, A, W>), )
Sourcepub fn move_nth<_T>(
&mut self,
n: usize,
mov: impl FnOnce(Mover<'_, A, S>) -> _T,
)
pub fn move_nth<_T>( &mut self, n: usize, mov: impl FnOnce(Mover<'_, A, S>) -> _T, )
Moves the nth Cursor’s selection
Since the moving function takes Mover as an argument, this
method cannot be used to change the Text in any way.
At the end of the movement, if the cursor intersects any other, they will be merged into one.
If you want to edit on the nth cursor, see edit_nth,
if you want to move the main cursor, see move_main, if you
want to move each cursor, see move_many.
Sourcepub fn move_main<_T>(&mut self, mov: impl FnOnce(Mover<'_, A, S>) -> _T)
pub fn move_main<_T>(&mut self, mov: impl FnOnce(Mover<'_, A, S>) -> _T)
Moves the main Cursor’s selection
Since the moving function takes Mover as an argument, this
method cannot be used to change the Text in any way.
At the end of the movement, if the cursor intersects any other, they will be merged into one.
If you want to move the main cursor, see edit_main,
if you want to move the main cursor, see move_main, if you
want to move each cursor, see move_many.
Sourcepub fn move_many<_T>(
&mut self,
range: impl RangeBounds<usize> + Clone,
mov: impl FnMut(Mover<'_, A, S>) -> _T,
)
pub fn move_many<_T>( &mut self, range: impl RangeBounds<usize> + Clone, mov: impl FnMut(Mover<'_, A, S>) -> _T, )
Moves a range of Cursor’s selections
Since the moving function takes Mover as an argument, this
method cannot be used to change the Text in any way.
At the end of the movement, if any of the cursors intersect with each other, they will be merged into one.
If you want to edit on many cursors, see edit_many,
if you want to move a specific cursor, see move_nth
or move_main.
Sourcepub fn widget_mut(&mut self) -> &mut W
pub fn widget_mut(&mut self) -> &mut W
A mutable reference to the Widget
Sourcepub fn cursors_mut(&mut self) -> &mut Cursors
pub fn cursors_mut(&mut self) -> &mut Cursors
A mutable reference to the Widget
Sourcepub fn new_moment(&mut self)
pub fn new_moment(&mut self)
Finishes the current moment and adds a new one to the history