Struct EditHelper

Source
pub struct EditHelper<'a, W: Widget<A::Ui>, A: Area, 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:

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);
         
        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 */
        }
    }

Implementations§

Source§

impl<'a, W: Widget<A::Ui>, A: Area> EditHelper<'a, W, A, ()>

Source

pub fn new(widget: &'a mut W, area: &'a A) -> Self

Returns a new instance of EditHelper

Source§

impl<W: Widget<A::Ui>, A: Area, S> EditHelper<'_, W, A, S>

Source

pub fn edit_nth(&mut self, n: usize) -> Editor<'_, W, A, S>

Edits the nth Cursor in the Text

Once dropped, the Cursor in this Editor will be added back to the list of Cursors, unless it is destroyed

If you want to edit on the main cursor, see edit_main, if you want to edit on many Cursors, see edit_iter.

Just like all other edit methods, this one will populate the Cursors, so if there are no Cursors, it will create one at Point::default.

Source

pub fn edit_main(&mut self) -> Editor<'_, W, A, S>

Edits the main Cursor in the Text

Once dropped, the Cursor in this Editor will be added back to the list of Cursors, unless it is destroyed

If you want to edit on the nth cursor, see edit_nth, same for edit_last, if you want to edit on many Cursors, see edit_iter.

Just like all other edit methods, this one will populate the Cursors, so if there are no Cursors, it will create one at Point::default.

Source

pub fn edit_last(&mut self) -> Editor<'_, W, A, S>

Edits the last Cursor in the Text

Once dropped, the Cursor in this Editor will be added back to the list of Cursors, unless it is destroyed

If you want to edit on the nth cursor, see edit_nth, same for edit_main, if you want to edit on many Cursors, see edit_iter.

Just like all other edit methods, this one will populate the Cursors, so if there are no Cursors, it will create one at Point::default.

Source

pub fn edit_iter<'b>(&'b mut self) -> EditIter<'b, W, A, S>

A Lender over all Editors of the Text

This lets you easily iterate over all Cursors, without having to worry about insertion affecting the order at which they are edited (like what repeated calls to edit_nth would do)

Note however that you can’t use a Lender (also known as a lending iterator) in a for loop, but you should be able to just while let Some(e) = editors.next() {} or helper.edit_iter().for_each(|_| {}) instead.

Just like all other edit methods, this one will populate the Cursors, so if there are no Cursors, it will create one at Point::default.

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, W: 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.