Struct WordCursor

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

Source

pub fn new(text: &'a Rope, pos: usize) -> WordCursor<'a>

Source

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

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

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

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

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

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

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

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

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

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

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

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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