pub struct WordCursor<'a> { /* private fields */ }
Expand description
A cursor providing utility function to navigate the rope by word boundaries. Boundaries can be the start of a word, its end, punctuation etc.
Implementations§
Source§impl<'a> WordCursor<'a>
impl<'a> WordCursor<'a>
pub fn new(text: &'a Rope, pos: usize) -> WordCursor<'a>
Sourcepub fn prev_boundary(&mut self, mode: Mode) -> Option<usize>
pub fn prev_boundary(&mut self, mode: Mode) -> Option<usize>
Get the previous start boundary of a word, and set the cursor position to the boundary found. The behaviour diffs a bit on new line character with modal and non modal, while on modal, it will ignore the new line character and on non-modal, it will stop at the new line character Example:
let rope = Rope::from("Hello world");
let mut cursor = WordCursor::new(&rope, 4);
let boundary = cursor.prev_boundary(Mode::Insert);
assert_eq!(boundary, Some(0));
Sourcepub fn prev_deletion_boundary(&mut self) -> Option<usize>
pub fn prev_deletion_boundary(&mut self) -> Option<usize>
Computes where the cursor position should be after backward deletion.
Example:
let text = "violet are blue";
let rope = Rope::from(text);
let mut cursor = WordCursor::new(&rope, 9);
let position = cursor.prev_deletion_boundary();
let position = position;
assert_eq!(position, Some(7));
assert_eq!(&text[..position.unwrap()], "violet ");
Sourcepub fn next_non_blank_char(&mut self) -> usize
pub fn next_non_blank_char(&mut self) -> usize
Get the position of the next non blank character in the rope
Example:
let rope = Rope::from(" world");
let mut cursor = WordCursor::new(&rope, 0);
let char_position = cursor.next_non_blank_char();
assert_eq!(char_position, 4);
Sourcepub fn next_boundary(&mut self) -> Option<usize>
pub fn next_boundary(&mut self) -> Option<usize>
Get the next start boundary of a word, and set the cursor position to the boundary found. Example:
let rope = Rope::from("Hello world");
let mut cursor = WordCursor::new(&rope, 0);
let boundary = cursor.next_boundary();
assert_eq!(boundary, Some(6));
Sourcepub fn end_boundary(&mut self) -> Option<usize>
pub fn end_boundary(&mut self) -> Option<usize>
Get the next end boundary, and set the cursor position to the boundary found. Example:
let rope = Rope::from("Hello world");
let mut cursor = WordCursor::new(&rope, 3);
let end_boundary = cursor.end_boundary();
assert_eq!(end_boundary, Some(5));
Sourcepub fn prev_code_boundary(&mut self) -> usize
pub fn prev_code_boundary(&mut self) -> usize
Get the first matching CharClassification::Other
backward and set the cursor position to this location .
Example:
let text = "violet, are\n blue";
let rope = Rope::from(text);
let mut cursor = WordCursor::new(&rope, 11);
let position = cursor.prev_code_boundary();
assert_eq!(&text[position..], "are\n blue");
Sourcepub fn next_code_boundary(&mut self) -> usize
pub fn next_code_boundary(&mut self) -> usize
Get the first matching CharClassification::Other
forward and set the cursor position to this location .
Example:
let text = "violet, are\n blue";
let rope = Rope::from(text);
let mut cursor = WordCursor::new(&rope, 11);
let position = cursor.next_code_boundary();
assert_eq!(&text[position..], "\n blue");
Sourcepub fn match_pairs(&mut self) -> Option<usize>
pub fn match_pairs(&mut self) -> Option<usize>
Looks for a matching pair character, either forward for opening chars (ex: (
) or
backward for closing char (ex: }
), and return the matched character position if found.
Will return None
if the character under cursor is not matchable (see crate::util::matching_char
).
Example:
let text = "{ }";
let rope = Rope::from(text);
let mut cursor = WordCursor::new(&rope, 2);
let position = cursor.match_pairs();
assert_eq!(position, Some(0));
Sourcepub fn next_unmatched(&mut self, c: char) -> Option<usize>
pub fn next_unmatched(&mut self, c: char) -> Option<usize>
Take a matchable character and look cforward for the first unmatched one ignoring the encountered matched pairs.
Example:
let rope = Rope::from("outer {inner}} world");
let mut cursor = WordCursor::new(&rope, 0);
let position = cursor.next_unmatched('}');
assert_eq!(position, Some(14));
Sourcepub fn previous_unmatched(&mut self, c: char) -> Option<usize>
pub fn previous_unmatched(&mut self, c: char) -> Option<usize>
Take a matchable character and look backward for the first unmatched one ignoring the encountered matched pairs.
Example:
let rope = Rope::from("outer {{inner} world");
let mut cursor = WordCursor::new(&rope, 15);
let position = cursor.previous_unmatched('{');
assert_eq!(position, Some(6));
Sourcepub fn select_word(&mut self) -> (usize, usize)
pub fn select_word(&mut self) -> (usize, usize)
Return the previous and end boundaries of the word under cursor.
Example:
let text = "violet are blue";
let rope = Rope::from(text);
let mut cursor = WordCursor::new(&rope, 9);
let (start, end) = cursor.select_word();
assert_eq!(&text[start..end], "are");
Sourcepub fn find_enclosing_pair(&mut self) -> Option<(usize, usize)>
pub fn find_enclosing_pair(&mut self) -> Option<(usize, usize)>
Return the enclosing brackets of the current position
Example:
let text = "outer {{inner} world";
let rope = Rope::from(text);
let mut cursor = WordCursor::new(&rope, 10);
let (start, end) = cursor.find_enclosing_pair().unwrap();
assert_eq!(start, 7);
assert_eq!(end, 13)
Auto Trait Implementations§
impl<'a> Freeze for WordCursor<'a>
impl<'a> RefUnwindSafe for WordCursor<'a>
impl<'a> Send for WordCursor<'a>
impl<'a> Sync for WordCursor<'a>
impl<'a> Unpin for WordCursor<'a>
impl<'a> UnwindSafe for WordCursor<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more