Struct EditHelper

Source
pub struct EditHelper<'a, W, A, S>
where W: Widget<A::Ui> + 'static, A: Area,
{ /* 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:

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, ()>
where W: Widget<A::Ui> + 'static, A: Area,

Source

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>
where W: Widget<A::Ui> + 'static, A: Area,

Source

pub fn edit_nth( &mut self, n: usize, edit: impl FnOnce(&mut Editor<'_, '_, A, W>), )

Edits on the nth Cursor’s selection

Since the editing function takes Editor as an argument, you cannot change the selection of the Cursor.

If you want to move the nth cursor, see move_nth, if you want to edit on the main cursor, see edit_main, if you want to edit each cursor, see edit_many.

Source

pub fn edit_main(&mut self, edit: impl FnOnce(&mut Editor<'_, '_, A, W>))

Edits on the main Cursor’s selection

Since the editing function takes Editor as an argument, you cannot change the selection of the Cursor.

If you want to move the main cursor, see move_main, if you want to edit on the nth cursor, see edit_nth, if you want to edit each cursor, see edit_many.

Source

pub fn edit_many( &mut self, range: impl RangeBounds<usize> + Clone, f: impl FnMut(&mut Editor<'_, '_, A, W>), )

Edits on a range of Cursors

Since the editing function takes Editor as an argument, you cannot change the selection of the Cursor.

If you want to move many cursors, see move_many, if you want to edit on a specific cursor, see edit_nth or edit_main.

Source

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.

Source

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.

Source

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.

Source

pub fn widget(&self) -> &W

A shared reference to the Widget

Source

pub fn widget_mut(&mut self) -> &mut W

A mutable reference to the Widget

Source

pub fn text(&self) -> &Text

A shared reference to the Widget

Source

pub fn text_mut(&mut self) -> &mut Text

A mutable reference to the Widget

Source

pub fn cursors(&self) -> &Cursors

A shared reference to the Widget

Source

pub fn cursors_mut(&mut self) -> &mut Cursors

A mutable reference to the Widget

Source

pub fn undo(&mut self)

Undoes the last moment in the history, if there is 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 cfg(&self) -> PrintCfg

The PrintCfg in use

Source§

impl<'a, A> EditHelper<'a, File, A, Searcher>
where A: Area,

Source

pub fn new_inc(widget: &'a mut File, area: &'a A, searcher: Searcher) -> Self

Returns a new instance of EditHelper

Auto Trait Implementations§

§

impl<'a, W, A, S> Freeze for EditHelper<'a, W, A, S>
where S: Freeze,

§

impl<'a, W, A, S> RefUnwindSafe for EditHelper<'a, W, A, S>

§

impl<'a, W, A, S> Send for EditHelper<'a, W, A, S>
where S: Send,

§

impl<'a, W, A, S> Sync for EditHelper<'a, W, A, S>
where S: Sync,

§

impl<'a, W, A, S> Unpin for EditHelper<'a, W, A, S>
where S: Unpin,

§

impl<'a, W, A, S> !UnwindSafe for EditHelper<'a, W, A, S>

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<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.