Struct Editor

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

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.

Source

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

Inserts new text directly behind the caret

The selection remains unaltered, if the anchor is ahead of the caret, it will move forwards by edit.chars().count().

If you wish to replace the selected text, see replace

Source

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

If there is a selection, acts like replace, otherwise acts like insert

This only makes a difference if your selections are inclusive, since a replace when the anchor is None would still include one character.

Source

pub fn iter(&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 iter_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((start, end)) = nth {
            m.move_to(start);
            m.set_anchor();
            m.move_to(end);
        }
    })
}
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((start, end)) = nth {
            m.move_to(start);
            m.set_anchor();
            m.move_to(end);
        }
    })
}
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)

Sets the caret of the Cursor on the start of the selection

Source

pub fn set_caret_on_end(&mut self)

Sets the caret of the Cursor on the end of the selection

Source

pub fn selection(&self) -> IntoIter<&str, 2>

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 indent_on(&mut self, point: Point) -> usize

Returns the needed level of indentation in the line of the Point

If the tree-sitter can figure out the indentation level, it will return that. Otherwise, it will copy the level of indentation of the last non empty line.

Source

pub fn caret(&self) -> Point

Returns the caret

Source

pub fn caret_col(&self) -> usize

How many characterss the caret is from the start of the line

Source

pub fn caret_vcol(&self) -> usize

The visual distance between the caret and the start of the Area

Source

pub fn desired_caret_vcol(&self) -> usize

The desired visual distance between the caret and the start of the Area

Source

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

Returns the anchor

Source

pub fn anchor_col(&self) -> Option<usize>

How many characterss the anchor is from the start of the line

Source

pub fn anchor_vcol(&self) -> Option<usize>

The visual distance between the anchor and the start of the Area

Source

pub fn desired_anchor_vcol(&self) -> Option<usize>

The desired visual distance between the anchor and the start of the Area

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 is_incl(&self) -> bool

Whether or not this cursor’s selections are inclusive

Source

pub fn text(&self) -> &Text

Source

pub fn cfg(&self) -> PrintCfg

The PrintCfg in use

Auto Trait Implementations§

§

impl<'a, 'b, A, W> Freeze for Editor<'a, 'b, A, W>

§

impl<'a, 'b, A, W> RefUnwindSafe for Editor<'a, 'b, A, W>

§

impl<'a, 'b, A, W> Send for Editor<'a, 'b, A, W>

§

impl<'a, 'b, A, W> Sync for Editor<'a, 'b, A, W>

§

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

§

impl<'a, 'b, A, W> !UnwindSafe for Editor<'a, 'b, A, W>

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.