2021-07-03 03:07:49 +02:00
|
|
|
use ropey::RopeSlice;
|
|
|
|
|
2021-07-09 01:45:19 +02:00
|
|
|
use crate::chars::{categorize_char, CharCategory};
|
|
|
|
use crate::graphemes::{next_grapheme_boundary, prev_grapheme_boundary};
|
|
|
|
use crate::movement::Direction;
|
2021-07-03 03:07:49 +02:00
|
|
|
use crate::surround;
|
|
|
|
use crate::Range;
|
|
|
|
|
2021-07-09 01:45:19 +02:00
|
|
|
fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction) -> usize {
|
|
|
|
use CharCategory::{Eol, Whitespace};
|
2021-07-03 03:07:49 +02:00
|
|
|
|
|
|
|
let iter = match direction {
|
2021-07-09 01:45:19 +02:00
|
|
|
Direction::Forward => slice.chars_at(pos),
|
2021-07-03 03:07:49 +02:00
|
|
|
Direction::Backward => {
|
|
|
|
let mut iter = slice.chars_at(pos);
|
|
|
|
iter.reverse();
|
|
|
|
iter
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-09 01:45:19 +02:00
|
|
|
let mut prev_category = match direction {
|
|
|
|
Direction::Forward if pos == 0 => Whitespace,
|
|
|
|
Direction::Forward => categorize_char(slice.char(pos - 1)),
|
|
|
|
Direction::Backward if pos == slice.len_chars() => Whitespace,
|
|
|
|
Direction::Backward => categorize_char(slice.char(pos)),
|
|
|
|
};
|
|
|
|
|
|
|
|
for ch in iter {
|
|
|
|
match categorize_char(ch) {
|
|
|
|
Eol | Whitespace => return pos,
|
|
|
|
category => {
|
|
|
|
if category != prev_category && pos != 0 && pos != slice.len_chars() {
|
2021-07-03 03:07:49 +02:00
|
|
|
return pos;
|
2021-07-09 01:45:19 +02:00
|
|
|
} else {
|
|
|
|
if direction == Direction::Forward {
|
|
|
|
pos += 1;
|
|
|
|
} else {
|
|
|
|
pos = pos.saturating_sub(1);
|
|
|
|
}
|
|
|
|
prev_category = category;
|
2021-07-03 03:07:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 01:45:19 +02:00
|
|
|
|
|
|
|
pos
|
2021-07-03 03:07:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum TextObject {
|
|
|
|
Around,
|
|
|
|
Inside,
|
|
|
|
}
|
|
|
|
|
|
|
|
// count doesn't do anything yet
|
|
|
|
pub fn textobject_word(
|
|
|
|
slice: RopeSlice,
|
|
|
|
range: Range,
|
|
|
|
textobject: TextObject,
|
2021-07-09 01:45:19 +02:00
|
|
|
_count: usize,
|
2021-07-03 03:07:49 +02:00
|
|
|
) -> Range {
|
2021-07-09 01:45:19 +02:00
|
|
|
// For 1-width cursor semantics.
|
|
|
|
let head = if range.head > range.anchor {
|
|
|
|
prev_grapheme_boundary(slice, range.head)
|
|
|
|
} else {
|
|
|
|
range.head
|
|
|
|
};
|
|
|
|
|
|
|
|
let word_start = find_word_boundary(slice, head, Direction::Backward);
|
|
|
|
let word_end = match slice.get_char(head).map(categorize_char) {
|
|
|
|
None | Some(CharCategory::Whitespace | CharCategory::Eol) => head,
|
|
|
|
_ => find_word_boundary(slice, head + 1, Direction::Forward),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Special case.
|
|
|
|
if word_start == word_end {
|
|
|
|
return Range::new(word_start, word_end);
|
|
|
|
}
|
2021-07-03 03:07:49 +02:00
|
|
|
|
|
|
|
match textobject {
|
2021-07-09 01:45:19 +02:00
|
|
|
TextObject::Inside => Range::new(word_start, word_end),
|
|
|
|
TextObject::Around => Range::new(
|
|
|
|
match slice
|
|
|
|
.get_char(word_start.saturating_sub(1))
|
|
|
|
.map(categorize_char)
|
2021-07-03 03:07:49 +02:00
|
|
|
{
|
2021-07-09 01:45:19 +02:00
|
|
|
None | Some(CharCategory::Eol) => word_start,
|
|
|
|
_ => prev_grapheme_boundary(slice, word_start),
|
|
|
|
},
|
|
|
|
match slice.get_char(word_end).map(categorize_char) {
|
|
|
|
None | Some(CharCategory::Eol) => word_end,
|
|
|
|
_ => next_grapheme_boundary(slice, word_end),
|
|
|
|
},
|
|
|
|
),
|
|
|
|
}
|
2021-07-03 03:07:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn textobject_surround(
|
|
|
|
slice: RopeSlice,
|
|
|
|
range: Range,
|
|
|
|
textobject: TextObject,
|
|
|
|
ch: char,
|
|
|
|
count: usize,
|
|
|
|
) -> Range {
|
|
|
|
surround::find_nth_pairs_pos(slice, ch, range.head, count)
|
|
|
|
.map(|(anchor, head)| match textobject {
|
2021-07-09 01:45:19 +02:00
|
|
|
TextObject::Inside => Range::new(
|
|
|
|
next_grapheme_boundary(slice, anchor),
|
|
|
|
prev_grapheme_boundary(slice, head),
|
|
|
|
),
|
2021-07-03 03:07:49 +02:00
|
|
|
TextObject::Around => Range::new(anchor, head),
|
|
|
|
})
|
|
|
|
.unwrap_or(range)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::TextObject::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::Range;
|
|
|
|
use ropey::Rope;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_textobject_word() {
|
|
|
|
// (text, [(cursor position, textobject, final range), ...])
|
|
|
|
let tests = &[
|
|
|
|
(
|
|
|
|
"cursor at beginning of doc",
|
2021-07-09 01:45:19 +02:00
|
|
|
vec![(0, Inside, (0, 6)), (0, Around, (0, 7))],
|
2021-07-03 03:07:49 +02:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor at middle of word",
|
|
|
|
vec![
|
2021-07-09 01:45:19 +02:00
|
|
|
(13, Inside, (10, 16)),
|
|
|
|
(10, Inside, (10, 16)),
|
|
|
|
(15, Inside, (10, 16)),
|
|
|
|
(13, Around, (9, 17)),
|
|
|
|
(10, Around, (9, 17)),
|
|
|
|
(15, Around, (9, 17)),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor between word whitespace",
|
2021-07-09 01:45:19 +02:00
|
|
|
vec![(6, Inside, (6, 6)), (6, Around, (6, 6))],
|
2021-07-03 03:07:49 +02:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on word before newline\n",
|
|
|
|
vec![
|
2021-07-09 01:45:19 +02:00
|
|
|
(22, Inside, (22, 29)),
|
|
|
|
(28, Inside, (22, 29)),
|
|
|
|
(25, Inside, (22, 29)),
|
|
|
|
(22, Around, (21, 29)),
|
|
|
|
(28, Around, (21, 29)),
|
|
|
|
(25, Around, (21, 29)),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on newline\nnext line",
|
2021-07-09 01:45:19 +02:00
|
|
|
vec![(17, Inside, (17, 17)), (17, Around, (17, 17))],
|
2021-07-03 03:07:49 +02:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on word after newline\nnext line",
|
|
|
|
vec![
|
2021-07-09 01:45:19 +02:00
|
|
|
(29, Inside, (29, 33)),
|
|
|
|
(30, Inside, (29, 33)),
|
|
|
|
(32, Inside, (29, 33)),
|
|
|
|
(29, Around, (29, 34)),
|
|
|
|
(30, Around, (29, 34)),
|
|
|
|
(32, Around, (29, 34)),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on #$%:;* punctuation",
|
|
|
|
vec![
|
2021-07-09 01:45:19 +02:00
|
|
|
(13, Inside, (10, 16)),
|
|
|
|
(10, Inside, (10, 16)),
|
|
|
|
(15, Inside, (10, 16)),
|
|
|
|
(13, Around, (9, 17)),
|
|
|
|
(10, Around, (9, 17)),
|
|
|
|
(15, Around, (9, 17)),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on punc%^#$:;.tuation",
|
|
|
|
vec![
|
2021-07-09 01:45:19 +02:00
|
|
|
(14, Inside, (14, 21)),
|
|
|
|
(20, Inside, (14, 21)),
|
|
|
|
(17, Inside, (14, 21)),
|
|
|
|
(14, Around, (13, 22)),
|
2021-07-03 03:07:49 +02:00
|
|
|
// FIXME: edge case
|
|
|
|
// (20, Around, (14, 20)),
|
2021-07-09 01:45:19 +02:00
|
|
|
(17, Around, (13, 22)),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor in extra whitespace",
|
|
|
|
vec![
|
|
|
|
(9, Inside, (9, 9)),
|
|
|
|
(10, Inside, (10, 10)),
|
|
|
|
(11, Inside, (11, 11)),
|
2021-07-09 01:45:19 +02:00
|
|
|
(9, Around, (9, 9)),
|
|
|
|
(10, Around, (10, 10)),
|
|
|
|
(11, Around, (11, 11)),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor at end of doc",
|
2021-07-09 01:45:19 +02:00
|
|
|
vec![(19, Inside, (17, 20)), (19, Around, (16, 20))],
|
2021-07-03 03:07:49 +02:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (sample, scenario) in tests {
|
|
|
|
let doc = Rope::from(*sample);
|
|
|
|
let slice = doc.slice(..);
|
|
|
|
for &case in scenario {
|
|
|
|
let (pos, objtype, expected_range) = case;
|
|
|
|
let result = textobject_word(slice, Range::point(pos), objtype, 1);
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
expected_range.into(),
|
|
|
|
"\nCase failed: {:?} - {:?}",
|
|
|
|
sample,
|
|
|
|
case
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_textobject_surround() {
|
|
|
|
// (text, [(cursor position, textobject, final range, count), ...])
|
|
|
|
let tests = &[
|
|
|
|
(
|
|
|
|
"simple (single) surround pairs",
|
|
|
|
vec![
|
|
|
|
(3, Inside, (3, 3), '(', 1),
|
2021-07-09 01:45:19 +02:00
|
|
|
(7, Inside, (8, 14), ')', 1),
|
|
|
|
(10, Inside, (8, 14), '(', 1),
|
|
|
|
(14, Inside, (8, 14), ')', 1),
|
2021-07-03 03:07:49 +02:00
|
|
|
(3, Around, (3, 3), '(', 1),
|
2021-07-09 01:45:19 +02:00
|
|
|
(7, Around, (7, 15), ')', 1),
|
|
|
|
(10, Around, (7, 15), '(', 1),
|
|
|
|
(14, Around, (7, 15), ')', 1),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"samexx 'single' surround pairs",
|
|
|
|
vec![
|
|
|
|
(3, Inside, (3, 3), '\'', 1),
|
2021-07-09 01:45:19 +02:00
|
|
|
(7, Inside, (7, 7), '\'', 1),
|
|
|
|
(10, Inside, (8, 14), '\'', 1),
|
|
|
|
(14, Inside, (14, 14), '\'', 1),
|
2021-07-03 03:07:49 +02:00
|
|
|
(3, Around, (3, 3), '\'', 1),
|
2021-07-09 01:45:19 +02:00
|
|
|
(7, Around, (7, 7), '\'', 1),
|
|
|
|
(10, Around, (7, 15), '\'', 1),
|
|
|
|
(14, Around, (14, 14), '\'', 1),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"(nested (surround (pairs)) 3 levels)",
|
|
|
|
vec![
|
2021-07-09 01:45:19 +02:00
|
|
|
(0, Inside, (1, 35), '(', 1),
|
|
|
|
(6, Inside, (1, 35), ')', 1),
|
|
|
|
(8, Inside, (9, 25), '(', 1),
|
|
|
|
(8, Inside, (9, 35), ')', 2),
|
|
|
|
(20, Inside, (9, 25), '(', 2),
|
|
|
|
(20, Inside, (1, 35), ')', 3),
|
|
|
|
(0, Around, (0, 36), '(', 1),
|
|
|
|
(6, Around, (0, 36), ')', 1),
|
|
|
|
(8, Around, (8, 26), '(', 1),
|
|
|
|
(8, Around, (8, 36), ')', 2),
|
|
|
|
(20, Around, (8, 26), '(', 2),
|
|
|
|
(20, Around, (0, 36), ')', 3),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"(mixed {surround [pair] same} line)",
|
|
|
|
vec![
|
2021-07-09 01:45:19 +02:00
|
|
|
(2, Inside, (1, 34), '(', 1),
|
|
|
|
(9, Inside, (8, 28), '{', 1),
|
|
|
|
(18, Inside, (18, 22), '[', 1),
|
|
|
|
(2, Around, (0, 35), '(', 1),
|
|
|
|
(9, Around, (7, 29), '{', 1),
|
|
|
|
(18, Around, (17, 23), '[', 1),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"(stepped (surround) pairs (should) skip)",
|
2021-07-09 01:45:19 +02:00
|
|
|
vec![(22, Inside, (1, 39), '(', 1), (22, Around, (0, 40), '(', 1)],
|
2021-07-03 03:07:49 +02:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"[surround pairs{\non different]\nlines}",
|
|
|
|
vec![
|
2021-07-09 01:45:19 +02:00
|
|
|
(7, Inside, (1, 29), '[', 1),
|
|
|
|
(15, Inside, (16, 36), '{', 1),
|
|
|
|
(7, Around, (0, 30), '[', 1),
|
|
|
|
(15, Around, (15, 37), '{', 1),
|
2021-07-03 03:07:49 +02:00
|
|
|
],
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (sample, scenario) in tests {
|
|
|
|
let doc = Rope::from(*sample);
|
|
|
|
let slice = doc.slice(..);
|
|
|
|
for &case in scenario {
|
|
|
|
let (pos, objtype, expected_range, ch, count) = case;
|
|
|
|
let result = textobject_surround(slice, Range::point(pos), objtype, ch, count);
|
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
expected_range.into(),
|
|
|
|
"\nCase failed: {:?} - {:?}",
|
|
|
|
sample,
|
|
|
|
case
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|