Struct Editor

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

Source

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.

Source

pub fn insert(&mut self, edit: impl ToString)

Inserts new text directly behind the caret

If the anchor is ahead of the caret, it will move forwards by the number of chars in the new text.

If you wish to replace the selected text, see replace, if you want to append after the caret instead, see append

Source

pub fn append(&mut self, edit: impl ToString)

Appends new text directly after the caret

If the anchor is ahead of the caret, it will move forwards by the number of chars in the new text.

If you wish to replace the selected text, see replace, if you want to insert before the caret instead, see insert

Source

pub fn move_hor(&mut self, count: i32) -> i32

Moves the cursor horizontally. May cause vertical movement

Returns the distance moved in chars.

Source

pub fn move_ver(&mut self, count: i32) -> i32

Moves the cursor vertically. May cause horizontal movement

Returns the distance moved in lines.

Source

pub fn move_ver_wrapped(&mut self, count: i32)

Moves the cursor vertically. May cause horizontal movement

Returns the distance moved in wrapped lines.

Source

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

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

pub fn unset_anchor(&mut self) -> Option<Point>

Returns and takes the anchor of the Cursor.

Source

pub fn set_anchor(&mut self)

Sets the anchor to the current caret

Source

pub fn swap_ends(&mut self)

Swaps the position of the caret and anchor

Source

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

Source

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

Source

pub fn reset(&mut self)

Resets the Cursor to how it was before being modified

Source

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.

Source

pub fn destroy(self)

Destroys the current Cursor

Will not destroy it if it is the last Cursor left

If this was the main cursor, the main cursor will now be the cursor immediately behind it.

Source

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.

Source

pub fn chars_fwd(&self) -> impl Iterator<Item = (Point, char)> + '_

Iterates over the chars

This iteration will begin on the caret. It will also include the Point of each char

Source

pub fn chars_rev(&self) -> impl Iterator<Item = (Point, char)> + '_

Iterates over the chars, in reverse

This iteration will begin on the caret. It will also include the Point of each char

Source

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);
        }
    })
}
Source

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);
        }
    })
}
Source

pub fn matches<R: RegexPattern>(&mut self, pat: R) -> bool

Wether the current selection matches a regex pattern

Source

pub fn char(&self) -> char

Returns the char in the caret

Source

pub fn char_at(&self, p: Point) -> Option<char>

Returns the char at a given Point

Source

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.

Source

pub fn contiguous_in(&mut self, range: impl TextRange) -> &str

Source

pub fn len(&self) -> Point

Returns the length of the Text, in Point

Source

pub fn last_point(&self) -> Option<Point>

Returns the position of the last char if there is one

Source

pub fn lines_on( &mut self, range: impl TextRange, ) -> impl DoubleEndedIterator<Item = (usize, &str)> + '_

An Iterator over the lines in a given range

Source

pub fn indent(&self) -> usize

Gets the current level of indentation

Source

pub fn indent_on(&self, p: Point) -> usize

Gets the indentation level on the given Point

Source

pub fn get_reader<R: Reader>(&mut self) -> Option<R::PublicReader<'_>>

Gets a Reader’s public facing API, if it exists

Source

pub fn caret(&self) -> Point

Returns the caret

Source

pub fn anchor(&self) -> Option<Point>

Returns the anchor

Source

pub fn range(&self) -> [Point; 2]

Source

pub fn v_caret(&self) -> VPoint

Source

pub fn v_anchor(&self) -> Option<VPoint>

Source

pub fn anchor_is_start(&self) -> bool

Returns true if the anchor exists before the caret

Source

pub fn is_main(&self) -> bool

Whether or not this is the main Cursor

Source

pub fn text(&self) -> &Text

Source

pub fn cfg(&self) -> PrintCfg

The PrintCfg in use

Source§

impl<W: Widget<A::Ui>, A: Area> Editor<'_, W, A, Searcher>

Incremental search functions, only available on IncSearchers

Source

pub fn search_inc_fwd( &mut self, end: Option<Point>, ) -> impl Iterator<Item = [Point; 2]> + '_

Search incrementally from an IncSearch request

This will match the Regex pattern from the current position of the caret. if end is Some, the search will end at the requested Point.

Source

pub fn search_inc_rev( &mut self, start: Option<Point>, ) -> impl Iterator<Item = [Point; 2]> + '_

Search incrementally from an IncSearch request in reverse

This will match the Regex pattern from the current position of the caret in reverse. if start is Some, the search will end at the requested Point.

Source

pub fn matches_inc(&mut self) -> bool

Whether the Cursor’s selection matches the IncSearch request

Trait Implementations§

Source§

impl<'a, W: Widget<A::Ui> + 'a, A: Area + 'a, S: 'a> Drop for Editor<'a, W, A, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, W, A, S> !Freeze for Editor<'a, W, A, S>

§

impl<'a, W, A, S> !RefUnwindSafe for Editor<'a, W, A, S>

§

impl<'a, W, A, S> !Send for Editor<'a, W, A, S>

§

impl<'a, W, A, S> !Sync for Editor<'a, W, A, S>

§

impl<'a, W, A, S> Unpin for Editor<'a, W, A, S>

§

impl<'a, W, A, S> !UnwindSafe for Editor<'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.