pub struct Editor<'a, 'b, A, W>{ /* 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>
impl<'a, 'b, A, W> Editor<'a, 'b, A, W>
Sourcepub fn replace(&mut self, edit: impl ToString)
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.
Sourcepub fn insert(&mut self, edit: impl ToString)
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
Sourcepub fn insert_or_replace(&mut self, edit: impl ToString)
pub fn insert_or_replace(&mut self, edit: impl ToString)
Sourcepub fn search_fwd<R: RegexPattern>(
&mut self,
pat: R,
end: Option<Point>,
) -> impl Iterator<Item = R::Match> + '_
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);
}
})
}Sourcepub fn search_rev<R: RegexPattern>(
&mut self,
pat: R,
start: Option<Point>,
) -> impl Iterator<Item = R::Match> + '_
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);
}
})
}Sourcepub fn unset_anchor(&mut self) -> Option<Point>
pub fn unset_anchor(&mut self) -> Option<Point>
Returns and takes the anchor of the Cursor.
Sourcepub fn set_anchor(&mut self)
pub fn set_anchor(&mut self)
Sets the anchor to the current caret
Sourcepub fn set_caret_on_start(&mut self)
pub fn set_caret_on_start(&mut self)
Sets the caret of the Cursor on the start of the selection
Sourcepub fn set_caret_on_end(&mut self)
pub fn set_caret_on_end(&mut self)
Sets the caret of the Cursor on the end of the selection
Sourcepub fn selection(&self) -> IntoIter<&str, 2>
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.
Sourcepub fn indent_on(&mut self, point: Point) -> usize
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.
Sourcepub fn caret_vcol(&self) -> usize
pub fn caret_vcol(&self) -> usize
The visual distance between the caret and the start of the
Area
Sourcepub fn desired_caret_vcol(&self) -> usize
pub fn desired_caret_vcol(&self) -> usize
The desired visual distance between the caret and the start of
the Area
Sourcepub fn anchor_col(&self) -> Option<usize>
pub fn anchor_col(&self) -> Option<usize>
How many characterss the anchor is from the start of the line
Sourcepub fn anchor_vcol(&self) -> Option<usize>
pub fn anchor_vcol(&self) -> Option<usize>
The visual distance between the anchor and the start of the
Area
Sourcepub fn desired_anchor_vcol(&self) -> Option<usize>
pub fn desired_anchor_vcol(&self) -> Option<usize>
The desired visual distance between the anchor and the start
of the Area
Sourcepub fn anchor_is_start(&self) -> bool
pub fn anchor_is_start(&self) -> bool
Returns true if the anchor exists before the caret