Verified Commit 74286067 authored by Phil Booth's avatar Phil Booth

fix(cursor): include punctuation in word movement

parent 80274931
Pipeline #32704759 passed with stage
in 1 minute and 56 seconds
......@@ -225,12 +225,12 @@ impl Cursor {
break;
}
let (is_newline, is_word) =
arena().read(word_id, &|word| (word.is_newline_word(), word.is_word()));
let (is_newline, is_space) =
arena().read(word_id, &|word| (word.is_newline_word(), word.is_space()));
if is_newline {
line_count += 1;
} else if is_word {
} else if !is_space {
count += 1;
}
......
......@@ -251,7 +251,7 @@ fn move_to_line_end() {
#[test]
fn move_word() {
let line_ids = parse::lines("0 11 222\n3333").unwrap();
let line_ids = parse::lines("0 11, 222\n3333").unwrap();
let word_id = arena().read(line_ids[0], &|line| line.first_child().unwrap());
let grapheme_id = arena().read(word_id, &|word| word.first_child());
let mut cursor = Cursor::new(grapheme_id, 0, 0);
......@@ -265,7 +265,11 @@ fn move_word() {
assert_eq!(cursor.line_index(), 0);
cursor.move_word(1);
assert_eq!(cursor.grapheme_index(), 5);
assert_eq!(cursor.grapheme_index(), 4);
assert_eq!(cursor.line_index(), 0);
cursor.move_word(1);
assert_eq!(cursor.grapheme_index(), 6);
assert_eq!(cursor.line_index(), 0);
cursor.move_word(1);
......@@ -277,7 +281,11 @@ fn move_word() {
assert_eq!(cursor.line_index(), 1);
cursor.move_word(-1);
assert_eq!(cursor.grapheme_index(), 5);
assert_eq!(cursor.grapheme_index(), 6);
assert_eq!(cursor.line_index(), 0);
cursor.move_word(-1);
assert_eq!(cursor.grapheme_index(), 4);
assert_eq!(cursor.line_index(), 0);
cursor.move_word(-1);
......@@ -292,38 +300,38 @@ fn move_word() {
assert_eq!(cursor.grapheme_index(), 0);
assert_eq!(cursor.line_index(), 0);
cursor.move_word(2);
assert_eq!(cursor.grapheme_index(), 5);
cursor.move_word(3);
assert_eq!(cursor.grapheme_index(), 6);
assert_eq!(cursor.line_index(), 0);
cursor.move_word(2);
cursor.move_word(3);
assert_eq!(cursor.grapheme_index(), 0);
assert_eq!(cursor.line_index(), 1);
cursor.move_word(2);
cursor.move_word(3);
assert_eq!(cursor.grapheme_index(), 0);
assert_eq!(cursor.line_index(), 1);
cursor.move_word(-1);
assert_eq!(cursor.grapheme_index(), 5);
assert_eq!(cursor.grapheme_index(), 6);
assert_eq!(cursor.line_index(), 0);
cursor.move_grapheme(1);
assert_eq!(cursor.grapheme_index(), 6);
assert_eq!(cursor.grapheme_index(), 7);
assert_eq!(cursor.line_index(), 0);
cursor.move_word(-1);
assert_eq!(cursor.grapheme_index(), 5);
assert_eq!(cursor.grapheme_index(), 6);
assert_eq!(cursor.line_index(), 0);
cursor.move_word(-1);
assert_eq!(cursor.grapheme_index(), 2);
assert_eq!(cursor.grapheme_index(), 4);
assert_eq!(cursor.line_index(), 0);
}
#[test]
fn move_to_word_end() {
let line_ids = parse::lines("00 111 2222\n33333").unwrap();
let line_ids = parse::lines("00 111, 2222\n33333").unwrap();
let word_id = arena().read(line_ids[0], &|line| line.first_child().unwrap());
let grapheme_id = arena().read(word_id, &|word| word.first_child());
let mut cursor = Cursor::new(grapheme_id, 0, 0);
......@@ -345,7 +353,11 @@ fn move_to_word_end() {
assert_eq!(cursor.line_index(), 0);
cursor.move_to_word_end(1);
assert_eq!(cursor.grapheme_index(), 10);
assert_eq!(cursor.grapheme_index(), 6);
assert_eq!(cursor.line_index(), 0);
cursor.move_to_word_end(1);
assert_eq!(cursor.grapheme_index(), 11);
assert_eq!(cursor.line_index(), 0);
cursor.move_to_word_end(1);
......@@ -357,7 +369,11 @@ fn move_to_word_end() {
assert_eq!(cursor.line_index(), 1);
cursor.move_to_word_end(-1);
assert_eq!(cursor.grapheme_index(), 10);
assert_eq!(cursor.grapheme_index(), 11);
assert_eq!(cursor.line_index(), 0);
cursor.move_to_word_end(-1);
assert_eq!(cursor.grapheme_index(), 6);
assert_eq!(cursor.line_index(), 0);
cursor.move_to_word_end(-1);
......
......@@ -44,6 +44,7 @@ use self::{
lazy_static! {
static ref TOKENS: RwLock<TokenArena> = RwLock::new(TokenArena::new());
static ref WORD_PATTERN: Regex = Regex::new(r"^\w+$").unwrap();
static ref SPACE_PATTERN: Regex = Regex::new(r"^\s+$").unwrap();
}
pub fn arena() -> RwLockReadGuard<'static, TokenArena> {
......@@ -186,6 +187,23 @@ impl Token {
}
}
pub fn is_space_grapheme(&self) -> bool {
self.value_or(false, &|value| SPACE_PATTERN.is_match(value.as_ref()))
}
pub fn is_space(&self) -> bool {
match *self {
Token::Word { ref children, .. } => {
if let Some(first_id) = children.first() {
arena().read(first_id, &|first| first.is_space_grapheme())
} else {
false
}
}
_ => false,
}
}
pub fn is_first_child(&self) -> bool {
if let Some(parent_id) = self.parent() {
let this_id = self.siblings(&|siblings| siblings.this());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment