2021-03-18 05:39:34 +01:00
|
|
|
|
use crate::graphemes::{nth_next_grapheme_boundary, nth_prev_grapheme_boundary, RopeGraphemes};
|
|
|
|
|
use crate::{coords_at_pos, pos_at_coords, ChangeSet, Position, Range, Rope, RopeSlice, Selection};
|
|
|
|
|
|
2021-06-07 16:34:19 +02:00
|
|
|
|
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
2021-03-18 07:07:02 +01:00
|
|
|
|
pub enum Direction {
|
|
|
|
|
Forward,
|
|
|
|
|
Backward,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn move_horizontally(
|
|
|
|
|
text: RopeSlice,
|
|
|
|
|
range: Range,
|
|
|
|
|
dir: Direction,
|
|
|
|
|
count: usize,
|
|
|
|
|
extend: bool,
|
|
|
|
|
) -> Range {
|
|
|
|
|
let pos = range.head;
|
|
|
|
|
let line = text.char_to_line(pos);
|
|
|
|
|
// TODO: we can optimize clamping by passing in RopeSlice limited to current line. that way
|
|
|
|
|
// we stop calculating past start/end of line.
|
|
|
|
|
let pos = match dir {
|
|
|
|
|
Direction::Backward => {
|
|
|
|
|
let start = text.line_to_char(line);
|
|
|
|
|
nth_prev_grapheme_boundary(text, pos, count).max(start)
|
|
|
|
|
}
|
|
|
|
|
Direction::Forward => {
|
|
|
|
|
// Line end is pos at the start of next line - 1
|
2021-05-31 10:08:19 +02:00
|
|
|
|
let end = text.line_to_char(line + 1).saturating_sub(1);
|
2021-03-18 07:07:02 +01:00
|
|
|
|
nth_next_grapheme_boundary(text, pos, count).min(end)
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
Range::new(if extend { range.anchor } else { pos }, pos)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn move_vertically(
|
|
|
|
|
text: RopeSlice,
|
|
|
|
|
range: Range,
|
|
|
|
|
dir: Direction,
|
|
|
|
|
count: usize,
|
|
|
|
|
extend: bool,
|
|
|
|
|
) -> Range {
|
|
|
|
|
let Position { row, col } = coords_at_pos(text, range.head);
|
|
|
|
|
|
|
|
|
|
let horiz = range.horiz.unwrap_or(col as u32);
|
|
|
|
|
|
|
|
|
|
let new_line = match dir {
|
|
|
|
|
Direction::Backward => row.saturating_sub(count),
|
2021-06-05 05:49:19 +02:00
|
|
|
|
Direction::Forward => std::cmp::min(
|
|
|
|
|
row.saturating_add(count),
|
|
|
|
|
text.len_lines().saturating_sub(2),
|
|
|
|
|
),
|
2021-03-18 07:07:02 +01:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// convert to 0-indexed, subtract another 1 because len_chars() counts \n
|
|
|
|
|
let new_line_len = text.line(new_line).len_chars().saturating_sub(2);
|
|
|
|
|
|
|
|
|
|
let new_col = std::cmp::min(horiz as usize, new_line_len);
|
|
|
|
|
|
|
|
|
|
let pos = pos_at_coords(text, Position::new(new_line, new_col));
|
|
|
|
|
|
|
|
|
|
let mut range = Range::new(if extend { range.anchor } else { pos }, pos);
|
|
|
|
|
range.horiz = Some(horiz);
|
|
|
|
|
range
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
pub fn move_next_word_start(slice: RopeSlice, mut begin: usize, count: usize) -> Option<Range> {
|
|
|
|
|
let mut end = begin;
|
|
|
|
|
|
2021-03-18 05:39:34 +01:00
|
|
|
|
for _ in 0..count {
|
2021-06-02 07:57:43 +02:00
|
|
|
|
if begin + 1 == slice.len_chars() {
|
2021-04-05 09:35:04 +02:00
|
|
|
|
return None;
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
let mut ch = slice.char(begin);
|
|
|
|
|
let next = slice.char(begin + 1);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
|
|
|
|
|
// if we're at the end of a word, or on whitespce right before new one
|
|
|
|
|
if categorize(ch) != categorize(next) {
|
2021-04-05 09:35:04 +02:00
|
|
|
|
begin += 1;
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-02 07:57:43 +02:00
|
|
|
|
if !skip_over_next(slice, &mut begin, |ch| ch == '\n') {
|
|
|
|
|
return None;
|
|
|
|
|
};
|
2021-04-05 09:35:04 +02:00
|
|
|
|
ch = slice.char(begin);
|
|
|
|
|
|
|
|
|
|
end = begin + 1;
|
|
|
|
|
|
2021-03-18 05:39:34 +01:00
|
|
|
|
if is_word(ch) {
|
2021-04-05 09:35:04 +02:00
|
|
|
|
skip_over_next(slice, &mut end, is_word);
|
2021-06-08 06:20:15 +02:00
|
|
|
|
} else if is_punctuation(ch) {
|
|
|
|
|
skip_over_next(slice, &mut end, is_punctuation);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 06:20:15 +02:00
|
|
|
|
skip_over_next(slice, &mut end, char::is_whitespace);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
Some(Range::new(begin, end - 1))
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
pub fn move_prev_word_start(slice: RopeSlice, mut begin: usize, count: usize) -> Option<Range> {
|
|
|
|
|
let mut with_end = false;
|
|
|
|
|
let mut end = begin;
|
|
|
|
|
|
2021-03-18 05:39:34 +01:00
|
|
|
|
for _ in 0..count {
|
2021-04-05 09:35:04 +02:00
|
|
|
|
if begin == 0 {
|
|
|
|
|
return None;
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
let ch = slice.char(begin);
|
|
|
|
|
let prev = slice.char(begin - 1);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
|
|
|
|
|
if categorize(ch) != categorize(prev) {
|
2021-04-05 09:35:04 +02:00
|
|
|
|
begin -= 1;
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
// return if not skip while?
|
|
|
|
|
skip_over_prev(slice, &mut begin, |ch| ch == '\n');
|
2021-03-18 05:39:34 +01:00
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
end = begin;
|
2021-03-18 05:39:34 +01:00
|
|
|
|
|
2021-06-08 06:20:15 +02:00
|
|
|
|
with_end = skip_over_prev(slice, &mut end, char::is_whitespace);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
|
|
|
|
|
// refetch
|
2021-04-05 09:35:04 +02:00
|
|
|
|
let ch = slice.char(end);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
|
|
|
|
|
if is_word(ch) {
|
2021-04-05 09:35:04 +02:00
|
|
|
|
with_end = skip_over_prev(slice, &mut end, is_word);
|
2021-06-08 06:20:15 +02:00
|
|
|
|
} else if is_punctuation(ch) {
|
|
|
|
|
with_end = skip_over_prev(slice, &mut end, is_punctuation);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-07 09:57:58 +02:00
|
|
|
|
Some(Range::new(begin, if with_end { end } else { end + 1 }))
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
pub fn move_next_word_end(slice: RopeSlice, mut begin: usize, count: usize) -> Option<Range> {
|
|
|
|
|
let mut end = begin;
|
|
|
|
|
|
2021-03-18 05:39:34 +01:00
|
|
|
|
for _ in 0..count {
|
2021-06-02 07:57:43 +02:00
|
|
|
|
if begin + 2 >= slice.len_chars() {
|
2021-04-05 09:35:04 +02:00
|
|
|
|
return None;
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
let ch = slice.char(begin);
|
|
|
|
|
let next = slice.char(begin + 1);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
|
|
|
|
|
if categorize(ch) != categorize(next) {
|
2021-04-05 09:35:04 +02:00
|
|
|
|
begin += 1;
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-02 07:57:43 +02:00
|
|
|
|
if !skip_over_next(slice, &mut begin, |ch| ch == '\n') {
|
|
|
|
|
return None;
|
|
|
|
|
};
|
2021-04-05 09:35:04 +02:00
|
|
|
|
|
|
|
|
|
end = begin;
|
|
|
|
|
|
2021-06-08 06:20:15 +02:00
|
|
|
|
skip_over_next(slice, &mut end, char::is_whitespace);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
|
|
|
|
|
// refetch
|
2021-04-05 09:35:04 +02:00
|
|
|
|
let ch = slice.char(end);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
|
|
|
|
|
if is_word(ch) {
|
2021-04-05 09:35:04 +02:00
|
|
|
|
skip_over_next(slice, &mut end, is_word);
|
2021-06-08 06:20:15 +02:00
|
|
|
|
} else if is_punctuation(ch) {
|
|
|
|
|
skip_over_next(slice, &mut end, is_punctuation);
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 09:35:04 +02:00
|
|
|
|
Some(Range::new(begin, end - 1))
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ---- util ------------
|
|
|
|
|
|
|
|
|
|
// used for by-word movement
|
|
|
|
|
|
2021-06-08 06:20:15 +02:00
|
|
|
|
#[inline]
|
2021-06-05 12:15:50 +02:00
|
|
|
|
pub(crate) fn is_word(ch: char) -> bool {
|
2021-03-18 05:39:34 +01:00
|
|
|
|
ch.is_alphanumeric() || ch == '_'
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-08 06:20:15 +02:00
|
|
|
|
#[inline]
|
|
|
|
|
pub(crate) fn is_punctuation(ch: char) -> bool {
|
|
|
|
|
use unicode_general_category::{get_general_category, GeneralCategory};
|
|
|
|
|
|
|
|
|
|
matches!(
|
|
|
|
|
get_general_category(ch),
|
|
|
|
|
GeneralCategory::OtherPunctuation
|
|
|
|
|
| GeneralCategory::OpenPunctuation
|
|
|
|
|
| GeneralCategory::ClosePunctuation
|
|
|
|
|
| GeneralCategory::InitialPunctuation
|
|
|
|
|
| GeneralCategory::FinalPunctuation
|
|
|
|
|
| GeneralCategory::ConnectorPunctuation
|
|
|
|
|
| GeneralCategory::DashPunctuation
|
|
|
|
|
| GeneralCategory::MathSymbol
|
|
|
|
|
| GeneralCategory::CurrencySymbol
|
|
|
|
|
| GeneralCategory::ModifierSymbol
|
|
|
|
|
)
|
2021-04-05 09:35:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-18 05:39:34 +01:00
|
|
|
|
#[derive(Debug, Eq, PartialEq)]
|
2021-06-05 12:15:50 +02:00
|
|
|
|
pub(crate) enum Category {
|
2021-03-18 05:39:34 +01:00
|
|
|
|
Whitespace,
|
|
|
|
|
Eol,
|
|
|
|
|
Word,
|
|
|
|
|
Punctuation,
|
2021-06-05 18:01:20 +02:00
|
|
|
|
Unknown,
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
2021-06-05 18:01:20 +02:00
|
|
|
|
|
2021-06-08 06:20:15 +02:00
|
|
|
|
#[inline]
|
2021-06-05 12:15:50 +02:00
|
|
|
|
pub(crate) fn categorize(ch: char) -> Category {
|
2021-03-18 05:39:34 +01:00
|
|
|
|
if ch == '\n' {
|
|
|
|
|
Category::Eol
|
2021-06-08 06:20:15 +02:00
|
|
|
|
} else if ch.is_whitespace() {
|
2021-03-18 05:39:34 +01:00
|
|
|
|
Category::Whitespace
|
2021-05-31 14:09:17 +02:00
|
|
|
|
} else if is_word(ch) {
|
|
|
|
|
Category::Word
|
2021-06-08 06:20:15 +02:00
|
|
|
|
} else if is_punctuation(ch) {
|
2021-03-18 05:39:34 +01:00
|
|
|
|
Category::Punctuation
|
|
|
|
|
} else {
|
2021-06-05 18:01:20 +02:00
|
|
|
|
Category::Unknown
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2021-06-02 07:57:43 +02:00
|
|
|
|
/// Returns true if there are more characters left after the new position.
|
|
|
|
|
pub fn skip_over_next<F>(slice: RopeSlice, pos: &mut usize, fun: F) -> bool
|
2021-03-18 05:39:34 +01:00
|
|
|
|
where
|
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
|
{
|
|
|
|
|
let mut chars = slice.chars_at(*pos);
|
|
|
|
|
|
2021-06-08 06:20:15 +02:00
|
|
|
|
#[allow(clippy::while_let_on_iterator)]
|
2021-06-02 07:57:43 +02:00
|
|
|
|
while let Some(ch) = chars.next() {
|
2021-03-18 05:39:34 +01:00
|
|
|
|
if !fun(ch) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
*pos += 1;
|
|
|
|
|
}
|
2021-06-02 07:57:43 +02:00
|
|
|
|
chars.next().is_some()
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2021-04-05 09:35:04 +02:00
|
|
|
|
/// Returns true if the final pos matches the predicate.
|
|
|
|
|
pub fn skip_over_prev<F>(slice: RopeSlice, pos: &mut usize, fun: F) -> bool
|
2021-03-18 05:39:34 +01:00
|
|
|
|
where
|
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
|
{
|
|
|
|
|
// need to +1 so that prev() includes current char
|
|
|
|
|
let mut chars = slice.chars_at(*pos + 1);
|
|
|
|
|
|
2021-06-08 06:20:15 +02:00
|
|
|
|
#[allow(clippy::while_let_on_iterator)]
|
2021-03-18 05:39:34 +01:00
|
|
|
|
while let Some(ch) = chars.prev() {
|
|
|
|
|
if !fun(ch) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
*pos = pos.saturating_sub(1);
|
|
|
|
|
}
|
2021-04-06 13:00:35 +02:00
|
|
|
|
fun(slice.char(*pos))
|
2021-03-18 05:39:34 +01:00
|
|
|
|
}
|
2021-03-18 07:07:02 +01:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_vertical_move() {
|
|
|
|
|
let text = Rope::from("abcd\nefg\nwrs");
|
|
|
|
|
let slice = text.slice(..);
|
|
|
|
|
let pos = pos_at_coords(slice, (0, 4).into());
|
|
|
|
|
|
|
|
|
|
let range = Range::new(pos, pos);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
coords_at_pos(
|
|
|
|
|
slice,
|
|
|
|
|
move_vertically(slice, range, Direction::Forward, 1, false).head
|
|
|
|
|
),
|
|
|
|
|
(1, 2).into()
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-06-08 06:20:15 +02:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_categorize() {
|
|
|
|
|
const WORD_TEST_CASE: &'static str =
|
|
|
|
|
"_hello_world_あいうえおー12345678901234567890";
|
|
|
|
|
const PUNCTUATION_TEST_CASE: &'static str = "!\"#$%&\'()*+,-./:;<=>?@[\\]^`{|}~!”#$%&’()*+、。:;<=>?@「」^`{|}~";
|
|
|
|
|
const WHITESPACE_TEST_CASE: &'static str = " ";
|
|
|
|
|
|
|
|
|
|
assert_eq!(Category::Eol, categorize('\n'));
|
|
|
|
|
|
|
|
|
|
for ch in WHITESPACE_TEST_CASE.chars() {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
Category::Whitespace,
|
|
|
|
|
categorize(ch),
|
|
|
|
|
"Testing '{}', but got `{:?}` instead of `Category::Whitespace`",
|
|
|
|
|
ch,
|
|
|
|
|
categorize(ch)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ch in WORD_TEST_CASE.chars() {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
Category::Word,
|
|
|
|
|
categorize(ch),
|
|
|
|
|
"Testing '{}', but got `{:?}` instead of `Category::Word`",
|
|
|
|
|
ch,
|
|
|
|
|
categorize(ch)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for ch in PUNCTUATION_TEST_CASE.chars() {
|
|
|
|
|
assert_eq!(
|
|
|
|
|
Category::Punctuation,
|
|
|
|
|
categorize(ch),
|
|
|
|
|
"Testing '{}', but got `{:?}` instead of `Category::Punctuation`",
|
|
|
|
|
ch,
|
|
|
|
|
categorize(ch)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-18 07:07:02 +01:00
|
|
|
|
}
|