use super::TextLine;
#[derive(Copy, Clone, PartialEq, Eq)]
enum CharKind {
Whitespace,
Alphanumeric,
Other,
}
impl CharKind {
fn of(c: char) -> Self {
use CharKind::*;
if c.is_whitespace() {
Whitespace
} else if c.is_alphanumeric() {
Alphanumeric
} else {
Other
}
}
}
#[derive(Copy, Clone, PartialEq, Eq)]
pub enum Direction {
Forward,
Backward,
}
impl TextLine {
pub fn cursor_movement(movement: Direction, skip: bool) -> fn(&Self, &mut usize) -> bool {
use Direction::*;
match (movement, skip) {
(Backward, false) => Self::backward,
(Backward, true) => Self::skip_backward,
(Forward, false) => Self::forward,
(Forward, true) => Self::skip_forward,
}
}
pub fn forward(&self, text_cursor: &mut usize) -> bool {
if *text_cursor < self.len() {
*text_cursor += 1;
true
} else {
false
}
}
pub fn backward(&self, text_cursor: &mut usize) -> bool {
if *text_cursor > 0 {
*text_cursor -= 1;
true
} else {
false
}
}
pub fn skip_forward(&self, text_cursor: &mut usize) -> bool {
let len = self.len();
let start_kind = loop {
if *text_cursor == len {
return false;
}
match CharKind::of(self.char_at(*text_cursor)) {
CharKind::Whitespace => (),
kind => break kind,
}
*text_cursor += 1;
};
loop {
*text_cursor += 1;
if *text_cursor == len {
return true;
}
if CharKind::of(self.char_at(*text_cursor)) != start_kind {
return true;
}
}
}
pub fn skip_backward(&self, text_cursor: &mut usize) -> bool {
if *text_cursor == 0 {
return false;
}
let start_kind = loop {
*text_cursor -= 1;
if *text_cursor == 0 {
return false;
}
match CharKind::of(self.char_at(*text_cursor)) {
CharKind::Whitespace => (),
kind => break kind,
}
};
loop {
*text_cursor -= 1;
if CharKind::of(self.char_at(*text_cursor)) != start_kind {
*text_cursor += 1;
return true;
}
if *text_cursor == 0 {
return true;
}
}
}
}