2021-07-19 06:59:31 +02:00
|
|
|
use std::iter;
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
use ropey::iter::Chars;
|
2022-02-03 09:11:25 +01:00
|
|
|
use tree_sitter::{Node, QueryCursor};
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
use crate::{
|
2021-07-01 21:24:22 +02:00
|
|
|
chars::{categorize_char, char_is_line_ending, CharCategory},
|
2021-07-02 18:51:29 +02:00
|
|
|
graphemes::{
|
|
|
|
next_grapheme_boundary, nth_next_grapheme_boundary, nth_prev_grapheme_boundary,
|
2021-07-21 03:15:34 +02:00
|
|
|
prev_grapheme_boundary,
|
2021-07-02 18:51:29 +02:00
|
|
|
},
|
2022-02-21 12:07:16 +01:00
|
|
|
line_ending::rope_is_line_ending,
|
2022-06-21 18:36:36 +02:00
|
|
|
pos_at_visual_coords,
|
2022-02-03 09:11:25 +01:00
|
|
|
syntax::LanguageConfiguration,
|
|
|
|
textobject::TextObject,
|
2022-06-21 18:36:36 +02:00
|
|
|
visual_coords_at_pos, Position, Range, RopeSlice,
|
2021-06-11 14:57:07 +02:00
|
|
|
};
|
2021-03-18 05:39:34 +01:00
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
|
|
|
pub enum Movement {
|
|
|
|
Extend,
|
|
|
|
Move,
|
|
|
|
}
|
|
|
|
|
2021-03-18 07:07:02 +01:00
|
|
|
pub fn move_horizontally(
|
2021-06-11 14:57:07 +02:00
|
|
|
slice: RopeSlice,
|
2021-03-18 07:07:02 +01:00
|
|
|
range: Range,
|
|
|
|
dir: Direction,
|
|
|
|
count: usize,
|
2021-06-11 14:57:07 +02:00
|
|
|
behaviour: Movement,
|
2022-06-21 18:36:36 +02:00
|
|
|
_: usize,
|
2021-03-18 07:07:02 +01:00
|
|
|
) -> Range {
|
2021-07-26 17:40:30 +02:00
|
|
|
let pos = range.cursor(slice);
|
2021-07-08 02:24:39 +02:00
|
|
|
|
|
|
|
// Compute the new position.
|
2021-07-27 08:09:58 +02:00
|
|
|
let new_pos = match dir {
|
|
|
|
Direction::Forward => nth_next_grapheme_boundary(slice, pos, count),
|
|
|
|
Direction::Backward => nth_prev_grapheme_boundary(slice, pos, count),
|
2021-07-08 02:24:39 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Compute the final new range.
|
2021-07-26 17:40:30 +02:00
|
|
|
range.put_cursor(slice, new_pos, behaviour == Movement::Extend)
|
2021-03-18 07:07:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_vertically(
|
2021-06-11 14:57:07 +02:00
|
|
|
slice: RopeSlice,
|
2021-03-18 07:07:02 +01:00
|
|
|
range: Range,
|
|
|
|
dir: Direction,
|
|
|
|
count: usize,
|
2021-06-11 14:57:07 +02:00
|
|
|
behaviour: Movement,
|
2022-06-21 18:36:36 +02:00
|
|
|
tab_width: usize,
|
2021-03-18 07:07:02 +01:00
|
|
|
) -> Range {
|
2021-07-26 17:40:30 +02:00
|
|
|
let pos = range.cursor(slice);
|
2021-03-18 07:07:02 +01:00
|
|
|
|
2021-07-06 03:58:33 +02:00
|
|
|
// Compute the current position's 2d coordinates.
|
2022-06-21 18:36:36 +02:00
|
|
|
let Position { row, col } = visual_coords_at_pos(slice, pos, tab_width);
|
2021-03-18 07:07:02 +01:00
|
|
|
let horiz = range.horiz.unwrap_or(col as u32);
|
|
|
|
|
2021-07-06 03:58:33 +02:00
|
|
|
// Compute the new position.
|
2021-07-27 08:20:58 +02:00
|
|
|
let new_row = match dir {
|
|
|
|
Direction::Forward => (row + count).min(slice.len_lines().saturating_sub(1)),
|
|
|
|
Direction::Backward => row.saturating_sub(count),
|
2021-06-21 00:09:10 +02:00
|
|
|
};
|
2021-07-27 08:20:58 +02:00
|
|
|
let new_col = col.max(horiz as usize);
|
2022-06-21 18:36:36 +02:00
|
|
|
let new_pos = pos_at_visual_coords(slice, Position::new(new_row, new_col), tab_width);
|
2021-03-18 07:07:02 +01:00
|
|
|
|
2021-07-26 17:40:30 +02:00
|
|
|
// Special-case to avoid moving to the end of the last non-empty line.
|
|
|
|
if behaviour == Movement::Extend && slice.line(new_row).len_chars() == 0 {
|
|
|
|
return range;
|
2021-07-06 03:58:33 +02:00
|
|
|
}
|
2021-07-26 17:40:30 +02:00
|
|
|
|
|
|
|
let mut new_range = range.put_cursor(slice, new_pos, behaviour == Movement::Extend);
|
|
|
|
new_range.horiz = Some(horiz);
|
|
|
|
new_range
|
2021-03-18 07:07:02 +01:00
|
|
|
}
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
pub fn move_next_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
|
|
|
|
word_move(slice, range, count, WordMotionTarget::NextWordStart)
|
2021-03-18 05:39:34 +01:00
|
|
|
}
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
pub fn move_next_word_end(slice: RopeSlice, range: Range, count: usize) -> Range {
|
|
|
|
word_move(slice, range, count, WordMotionTarget::NextWordEnd)
|
2021-03-18 05:39:34 +01:00
|
|
|
}
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
pub fn move_prev_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
|
|
|
|
word_move(slice, range, count, WordMotionTarget::PrevWordStart)
|
|
|
|
}
|
2021-03-18 05:39:34 +01:00
|
|
|
|
2021-06-29 14:39:05 +02:00
|
|
|
pub fn move_next_long_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
|
|
|
|
word_move(slice, range, count, WordMotionTarget::NextLongWordStart)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_next_long_word_end(slice: RopeSlice, range: Range, count: usize) -> Range {
|
|
|
|
word_move(slice, range, count, WordMotionTarget::NextLongWordEnd)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn move_prev_long_word_start(slice: RopeSlice, range: Range, count: usize) -> Range {
|
|
|
|
word_move(slice, range, count, WordMotionTarget::PrevLongWordStart)
|
|
|
|
}
|
|
|
|
|
2021-07-03 03:07:49 +02:00
|
|
|
pub fn move_prev_word_end(slice: RopeSlice, range: Range, count: usize) -> Range {
|
|
|
|
word_move(slice, range, count, WordMotionTarget::PrevWordEnd)
|
|
|
|
}
|
|
|
|
|
2021-07-01 21:55:18 +02:00
|
|
|
fn word_move(slice: RopeSlice, range: Range, count: usize, target: WordMotionTarget) -> Range {
|
2021-07-19 06:59:31 +02:00
|
|
|
let is_prev = matches!(
|
|
|
|
target,
|
|
|
|
WordMotionTarget::PrevWordStart
|
|
|
|
| WordMotionTarget::PrevLongWordStart
|
|
|
|
| WordMotionTarget::PrevWordEnd
|
|
|
|
);
|
|
|
|
|
|
|
|
// Special-case early-out.
|
|
|
|
if (is_prev && range.head == 0) || (!is_prev && range.head == slice.len_chars()) {
|
|
|
|
return range;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prepare the range appropriately based on the target movement
|
|
|
|
// direction. This is addressing two things at once:
|
|
|
|
//
|
2021-07-26 17:40:30 +02:00
|
|
|
// 1. Block-cursor semantics.
|
2021-07-19 06:59:31 +02:00
|
|
|
// 2. The anchor position being irrelevant to the output result.
|
|
|
|
#[allow(clippy::collapsible_else_if)] // Makes the structure clearer in this case.
|
|
|
|
let start_range = if is_prev {
|
|
|
|
if range.anchor < range.head {
|
|
|
|
Range::new(range.head, prev_grapheme_boundary(slice, range.head))
|
|
|
|
} else {
|
|
|
|
Range::new(next_grapheme_boundary(slice, range.head), range.head)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if range.anchor < range.head {
|
|
|
|
Range::new(prev_grapheme_boundary(slice, range.head), range.head)
|
|
|
|
} else {
|
|
|
|
Range::new(range.head, next_grapheme_boundary(slice, range.head))
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Do the main work.
|
|
|
|
(0..count).fold(start_range, |r, _| {
|
|
|
|
slice.chars_at(r.head).range_to_target(target, r)
|
2021-06-11 14:57:07 +02:00
|
|
|
})
|
2021-03-18 05:39:34 +01:00
|
|
|
}
|
|
|
|
|
2022-03-07 14:25:26 +01:00
|
|
|
pub fn move_prev_paragraph(
|
|
|
|
slice: RopeSlice,
|
|
|
|
range: Range,
|
|
|
|
count: usize,
|
|
|
|
behavior: Movement,
|
|
|
|
) -> Range {
|
2022-02-07 16:37:09 +01:00
|
|
|
let mut line = range.cursor_line(slice);
|
|
|
|
let first_char = slice.line_to_char(line) == range.cursor(slice);
|
2022-02-21 15:58:54 +01:00
|
|
|
let prev_line_empty = rope_is_line_ending(slice.line(line.saturating_sub(1)));
|
2022-02-07 16:37:09 +01:00
|
|
|
let curr_line_empty = rope_is_line_ending(slice.line(line));
|
2022-02-21 15:58:54 +01:00
|
|
|
let prev_empty_to_line = prev_line_empty && !curr_line_empty;
|
2022-02-07 16:37:09 +01:00
|
|
|
|
2022-02-21 15:58:54 +01:00
|
|
|
// skip character before paragraph boundary
|
|
|
|
if prev_empty_to_line && !first_char {
|
2022-02-07 16:37:09 +01:00
|
|
|
line += 1;
|
|
|
|
}
|
|
|
|
let mut lines = slice.lines_at(line);
|
|
|
|
lines.reverse();
|
|
|
|
let mut lines = lines.map(rope_is_line_ending).peekable();
|
|
|
|
for _ in 0..count {
|
|
|
|
while lines.next_if(|&e| e).is_some() {
|
|
|
|
line -= 1;
|
|
|
|
}
|
|
|
|
while lines.next_if(|&e| !e).is_some() {
|
|
|
|
line -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let head = slice.line_to_char(line);
|
|
|
|
let anchor = if behavior == Movement::Move {
|
|
|
|
// exclude first character after paragraph boundary
|
2022-02-21 15:58:54 +01:00
|
|
|
if prev_empty_to_line && first_char {
|
2022-02-07 16:37:09 +01:00
|
|
|
range.cursor(slice)
|
|
|
|
} else {
|
|
|
|
range.head
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
range.put_cursor(slice, head, true).anchor
|
|
|
|
};
|
|
|
|
Range::new(anchor, head)
|
|
|
|
}
|
|
|
|
|
2022-03-07 14:25:26 +01:00
|
|
|
pub fn move_next_paragraph(
|
|
|
|
slice: RopeSlice,
|
|
|
|
range: Range,
|
|
|
|
count: usize,
|
|
|
|
behavior: Movement,
|
|
|
|
) -> Range {
|
2022-02-21 12:07:16 +01:00
|
|
|
let mut line = range.cursor_line(slice);
|
|
|
|
let last_char =
|
|
|
|
prev_grapheme_boundary(slice, slice.line_to_char(line + 1)) == range.cursor(slice);
|
|
|
|
let curr_line_empty = rope_is_line_ending(slice.line(line));
|
2022-04-05 02:43:14 +02:00
|
|
|
let next_line_empty =
|
|
|
|
rope_is_line_ending(slice.line(slice.len_lines().saturating_sub(1).min(line + 1)));
|
2022-02-21 15:58:54 +01:00
|
|
|
let curr_empty_to_line = curr_line_empty && !next_line_empty;
|
2022-02-21 12:07:16 +01:00
|
|
|
|
2022-02-21 15:58:54 +01:00
|
|
|
// skip character after paragraph boundary
|
|
|
|
if curr_empty_to_line && last_char {
|
2022-02-21 12:07:16 +01:00
|
|
|
line += 1;
|
|
|
|
}
|
|
|
|
let mut lines = slice.lines_at(line).map(rope_is_line_ending).peekable();
|
2022-02-07 16:37:09 +01:00
|
|
|
for _ in 0..count {
|
|
|
|
while lines.next_if(|&e| !e).is_some() {
|
|
|
|
line += 1;
|
|
|
|
}
|
|
|
|
while lines.next_if(|&e| e).is_some() {
|
|
|
|
line += 1;
|
|
|
|
}
|
|
|
|
}
|
2022-02-21 12:07:16 +01:00
|
|
|
let head = slice.line_to_char(line);
|
2022-02-07 16:37:09 +01:00
|
|
|
let anchor = if behavior == Movement::Move {
|
2022-02-21 15:58:54 +01:00
|
|
|
if curr_empty_to_line && last_char {
|
2022-02-21 12:07:16 +01:00
|
|
|
range.head
|
|
|
|
} else {
|
|
|
|
range.cursor(slice)
|
|
|
|
}
|
2022-02-07 16:37:09 +01:00
|
|
|
} else {
|
2022-02-21 12:07:16 +01:00
|
|
|
range.put_cursor(slice, head, true).anchor
|
2022-02-07 16:37:09 +01:00
|
|
|
};
|
2022-02-21 12:07:16 +01:00
|
|
|
Range::new(anchor, head)
|
2022-02-07 16:37:09 +01:00
|
|
|
}
|
|
|
|
|
2021-03-18 05:39:34 +01:00
|
|
|
// ---- util ------------
|
|
|
|
|
|
|
|
#[inline]
|
2021-06-11 14:57:07 +02:00
|
|
|
/// Returns first index that doesn't satisfy a given predicate when
|
|
|
|
/// advancing the character index.
|
|
|
|
///
|
|
|
|
/// Returns none if all characters satisfy the predicate.
|
|
|
|
pub fn skip_while<F>(slice: RopeSlice, pos: usize, fun: F) -> Option<usize>
|
2021-03-18 05:39:34 +01:00
|
|
|
where
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
{
|
2021-06-11 14:57:07 +02:00
|
|
|
let mut chars = slice.chars_at(pos).enumerate();
|
|
|
|
chars.find_map(|(i, c)| if !fun(c) { Some(pos + i) } else { None })
|
2021-03-18 05:39:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2021-06-11 14:57:07 +02:00
|
|
|
/// Returns first index that doesn't satisfy a given predicate when
|
|
|
|
/// retreating the character index, saturating if all elements satisfy
|
|
|
|
/// the condition.
|
|
|
|
pub fn backwards_skip_while<F>(slice: RopeSlice, pos: usize, fun: F) -> Option<usize>
|
2021-03-18 05:39:34 +01:00
|
|
|
where
|
|
|
|
F: Fn(char) -> bool,
|
|
|
|
{
|
2021-11-14 16:16:20 +01:00
|
|
|
let mut chars_starting_from_next = slice.chars_at(pos);
|
2021-06-11 14:57:07 +02:00
|
|
|
let mut backwards = iter::from_fn(|| chars_starting_from_next.prev()).enumerate();
|
|
|
|
backwards.find_map(|(i, c)| {
|
|
|
|
if !fun(c) {
|
|
|
|
Some(pos.saturating_sub(i))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-03-18 05:39:34 +01:00
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
/// Possible targets of a word motion
|
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum WordMotionTarget {
|
|
|
|
NextWordStart,
|
|
|
|
NextWordEnd,
|
|
|
|
PrevWordStart,
|
2021-07-03 03:07:49 +02:00
|
|
|
PrevWordEnd,
|
2021-06-29 14:39:05 +02:00
|
|
|
// A "Long word" (also known as a WORD in vim/kakoune) is strictly
|
|
|
|
// delimited by whitespace, and can consist of punctuation as well
|
|
|
|
// as alphanumerics.
|
|
|
|
NextLongWordStart,
|
|
|
|
NextLongWordEnd,
|
|
|
|
PrevLongWordStart,
|
2021-06-11 14:57:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait CharHelpers {
|
|
|
|
fn range_to_target(&mut self, target: WordMotionTarget, origin: Range) -> Range;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CharHelpers for Chars<'_> {
|
2021-07-19 06:59:31 +02:00
|
|
|
/// Note: this only changes the anchor of the range if the head is effectively
|
|
|
|
/// starting on a boundary (either directly or after skipping newline characters).
|
|
|
|
/// Any other changes to the anchor should be handled by the calling code.
|
2021-06-11 14:57:07 +02:00
|
|
|
fn range_to_target(&mut self, target: WordMotionTarget, origin: Range) -> Range {
|
2021-07-19 06:59:31 +02:00
|
|
|
let is_prev = matches!(
|
|
|
|
target,
|
2021-07-03 03:07:49 +02:00
|
|
|
WordMotionTarget::PrevWordStart
|
2021-07-19 06:59:31 +02:00
|
|
|
| WordMotionTarget::PrevLongWordStart
|
|
|
|
| WordMotionTarget::PrevWordEnd
|
|
|
|
);
|
|
|
|
|
|
|
|
// Reverse the iterator if needed for the motion direction.
|
|
|
|
if is_prev {
|
|
|
|
self.reverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function to advance index in the appropriate motion direction.
|
|
|
|
let advance: &dyn Fn(&mut usize) = if is_prev {
|
|
|
|
&|idx| *idx = idx.saturating_sub(1)
|
|
|
|
} else {
|
|
|
|
&|idx| *idx += 1
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialize state variables.
|
|
|
|
let mut anchor = origin.anchor;
|
|
|
|
let mut head = origin.head;
|
|
|
|
let mut prev_ch = {
|
|
|
|
let ch = self.prev();
|
|
|
|
if ch.is_some() {
|
2021-06-11 14:57:07 +02:00
|
|
|
self.next();
|
|
|
|
}
|
2021-07-19 06:59:31 +02:00
|
|
|
ch
|
2021-06-11 14:57:07 +02:00
|
|
|
};
|
|
|
|
|
2021-07-19 06:59:31 +02:00
|
|
|
// Skip any initial newline characters.
|
|
|
|
while let Some(ch) = self.next() {
|
|
|
|
if char_is_line_ending(ch) {
|
|
|
|
prev_ch = Some(ch);
|
|
|
|
advance(&mut head);
|
|
|
|
} else {
|
|
|
|
self.prev();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if prev_ch.map(char_is_line_ending).unwrap_or(false) {
|
|
|
|
anchor = head;
|
|
|
|
}
|
2021-06-11 14:57:07 +02:00
|
|
|
|
2021-07-19 06:59:31 +02:00
|
|
|
// Find our target position(s).
|
|
|
|
let head_start = head;
|
2021-07-29 21:41:24 +02:00
|
|
|
#[allow(clippy::while_let_on_iterator)] // Clippy's suggestion to fix doesn't work here.
|
2021-07-19 06:59:31 +02:00
|
|
|
while let Some(next_ch) = self.next() {
|
2021-07-19 21:30:08 +02:00
|
|
|
if prev_ch.is_none() || reached_target(target, prev_ch.unwrap(), next_ch) {
|
2021-07-19 06:59:31 +02:00
|
|
|
if head == head_start {
|
|
|
|
anchor = head;
|
|
|
|
} else {
|
|
|
|
break;
|
2021-06-11 14:57:07 +02:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 06:59:31 +02:00
|
|
|
prev_ch = Some(next_ch);
|
|
|
|
advance(&mut head);
|
2021-06-11 14:57:07 +02:00
|
|
|
}
|
2021-07-19 06:59:31 +02:00
|
|
|
|
|
|
|
// Un-reverse the iterator if needed.
|
|
|
|
if is_prev {
|
|
|
|
self.reverse();
|
|
|
|
}
|
|
|
|
|
|
|
|
Range::new(anchor, head)
|
2021-06-11 14:57:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:39:05 +02:00
|
|
|
fn is_word_boundary(a: char, b: char) -> bool {
|
|
|
|
categorize_char(a) != categorize_char(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn is_long_word_boundary(a: char, b: char) -> bool {
|
|
|
|
match (categorize_char(a), categorize_char(b)) {
|
|
|
|
(CharCategory::Word, CharCategory::Punctuation)
|
2021-06-29 14:54:16 +02:00
|
|
|
| (CharCategory::Punctuation, CharCategory::Word) => false,
|
2021-06-29 14:39:05 +02:00
|
|
|
(a, b) if a != b => true,
|
2021-06-29 14:54:16 +02:00
|
|
|
_ => false,
|
2021-06-29 14:39:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-19 06:59:31 +02:00
|
|
|
fn reached_target(target: WordMotionTarget, prev_ch: char, next_ch: char) -> bool {
|
2021-06-11 14:57:07 +02:00
|
|
|
match target {
|
2021-07-03 03:07:49 +02:00
|
|
|
WordMotionTarget::NextWordStart | WordMotionTarget::PrevWordEnd => {
|
2021-07-19 06:59:31 +02:00
|
|
|
is_word_boundary(prev_ch, next_ch)
|
|
|
|
&& (char_is_line_ending(next_ch) || !next_ch.is_whitespace())
|
2021-06-11 14:57:07 +02:00
|
|
|
}
|
|
|
|
WordMotionTarget::NextWordEnd | WordMotionTarget::PrevWordStart => {
|
2021-07-19 06:59:31 +02:00
|
|
|
is_word_boundary(prev_ch, next_ch)
|
|
|
|
&& (!prev_ch.is_whitespace() || char_is_line_ending(next_ch))
|
2021-06-29 14:39:05 +02:00
|
|
|
}
|
|
|
|
WordMotionTarget::NextLongWordStart => {
|
2021-07-19 06:59:31 +02:00
|
|
|
is_long_word_boundary(prev_ch, next_ch)
|
|
|
|
&& (char_is_line_ending(next_ch) || !next_ch.is_whitespace())
|
2021-06-29 14:39:05 +02:00
|
|
|
}
|
|
|
|
WordMotionTarget::NextLongWordEnd | WordMotionTarget::PrevLongWordStart => {
|
2021-07-19 06:59:31 +02:00
|
|
|
is_long_word_boundary(prev_ch, next_ch)
|
|
|
|
&& (!prev_ch.is_whitespace() || char_is_line_ending(next_ch))
|
2021-03-18 05:39:34 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-03-18 07:07:02 +01:00
|
|
|
|
2022-02-03 09:11:25 +01:00
|
|
|
pub fn goto_treesitter_object(
|
|
|
|
slice: RopeSlice,
|
|
|
|
range: Range,
|
|
|
|
object_name: &str,
|
|
|
|
dir: Direction,
|
|
|
|
slice_tree: Node,
|
|
|
|
lang_config: &LanguageConfiguration,
|
|
|
|
_count: usize,
|
|
|
|
) -> Range {
|
|
|
|
let get_range = move || -> Option<Range> {
|
|
|
|
let byte_pos = slice.char_to_byte(range.cursor(slice));
|
|
|
|
|
2022-02-11 04:31:52 +01:00
|
|
|
let cap_name = |t: TextObject| format!("{}.{}", object_name, t);
|
2022-02-03 09:11:25 +01:00
|
|
|
let mut cursor = QueryCursor::new();
|
2022-02-11 04:31:52 +01:00
|
|
|
let nodes = lang_config.textobject_query()?.capture_nodes_any(
|
|
|
|
&[
|
|
|
|
&cap_name(TextObject::Movement),
|
|
|
|
&cap_name(TextObject::Around),
|
|
|
|
&cap_name(TextObject::Inside),
|
|
|
|
],
|
2022-02-03 09:11:25 +01:00
|
|
|
slice_tree,
|
|
|
|
slice,
|
|
|
|
&mut cursor,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let node = match dir {
|
|
|
|
Direction::Forward => nodes
|
|
|
|
.filter(|n| n.start_byte() > byte_pos)
|
|
|
|
.min_by_key(|n| n.start_byte())?,
|
|
|
|
Direction::Backward => nodes
|
|
|
|
.filter(|n| n.start_byte() < byte_pos)
|
|
|
|
.max_by_key(|n| n.start_byte())?,
|
|
|
|
};
|
|
|
|
|
|
|
|
let len = slice.len_bytes();
|
|
|
|
let start_byte = node.start_byte();
|
|
|
|
let end_byte = node.end_byte();
|
|
|
|
if start_byte >= len || end_byte >= len {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let start_char = slice.byte_to_char(start_byte);
|
|
|
|
let end_char = slice.byte_to_char(end_byte);
|
|
|
|
|
|
|
|
// head of range should be at beginning
|
|
|
|
Some(Range::new(end_char, start_char))
|
|
|
|
};
|
|
|
|
get_range().unwrap_or(range)
|
|
|
|
}
|
|
|
|
|
2021-03-18 07:07:02 +01:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2021-06-11 14:57:07 +02:00
|
|
|
use ropey::Rope;
|
|
|
|
|
2022-06-21 18:36:36 +02:00
|
|
|
use crate::{coords_at_pos, pos_at_coords};
|
|
|
|
|
2021-03-18 07:07:02 +01:00
|
|
|
use super::*;
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
const SINGLE_LINE_SAMPLE: &str = "This is a simple alphabetic line";
|
|
|
|
const MULTILINE_SAMPLE: &str = "\
|
|
|
|
Multiline\n\
|
|
|
|
text sample\n\
|
|
|
|
which\n\
|
|
|
|
is merely alphabetic\n\
|
|
|
|
and whitespaced\n\
|
|
|
|
";
|
|
|
|
|
|
|
|
const MULTIBYTE_CHARACTER_SAMPLE: &str = "\
|
|
|
|
パーティーへ行かないか\n\
|
|
|
|
The text above is Japanese\n\
|
|
|
|
";
|
|
|
|
|
2021-03-18 07:07:02 +01:00
|
|
|
#[test]
|
|
|
|
fn test_vertical_move() {
|
|
|
|
let text = Rope::from("abcd\nefg\nwrs");
|
|
|
|
let slice = text.slice(..);
|
2021-07-21 03:15:34 +02:00
|
|
|
let pos = pos_at_coords(slice, (0, 4).into(), true);
|
2021-03-18 07:07:02 +01:00
|
|
|
|
|
|
|
let range = Range::new(pos, pos);
|
|
|
|
assert_eq!(
|
|
|
|
coords_at_pos(
|
|
|
|
slice,
|
2022-06-21 18:36:36 +02:00
|
|
|
move_vertically(slice, range, Direction::Forward, 1, Movement::Move, 4).head
|
2021-03-18 07:07:02 +01:00
|
|
|
),
|
2021-06-21 00:09:10 +02:00
|
|
|
(1, 3).into()
|
2021-03-18 07:07:02 +01:00
|
|
|
);
|
|
|
|
}
|
2021-06-08 06:20:15 +02:00
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
#[test]
|
2021-07-02 18:51:29 +02:00
|
|
|
fn horizontal_moves_through_single_line_text() {
|
2021-06-11 14:57:07 +02:00
|
|
|
let text = Rope::from(SINGLE_LINE_SAMPLE);
|
|
|
|
let slice = text.slice(..);
|
2021-07-21 03:15:34 +02:00
|
|
|
let position = pos_at_coords(slice, (0, 0).into(), true);
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
let mut range = Range::point(position);
|
|
|
|
|
|
|
|
let moves_and_expected_coordinates = [
|
2021-06-21 00:09:10 +02:00
|
|
|
((Direction::Forward, 1usize), (0, 1)), // T|his is a simple alphabetic line
|
|
|
|
((Direction::Forward, 2usize), (0, 3)), // Thi|s is a simple alphabetic line
|
|
|
|
((Direction::Forward, 0usize), (0, 3)), // Thi|s is a simple alphabetic line
|
|
|
|
((Direction::Forward, 999usize), (0, 32)), // This is a simple alphabetic line|
|
|
|
|
((Direction::Forward, 999usize), (0, 32)), // This is a simple alphabetic line|
|
|
|
|
((Direction::Backward, 999usize), (0, 0)), // |This is a simple alphabetic line
|
2021-06-11 14:57:07 +02:00
|
|
|
];
|
|
|
|
|
2021-12-18 06:57:49 +01:00
|
|
|
for ((direction, amount), coordinates) in moves_and_expected_coordinates {
|
2022-06-21 18:36:36 +02:00
|
|
|
range = move_horizontally(slice, range, direction, amount, Movement::Move, 0);
|
2021-06-11 14:57:07 +02:00
|
|
|
assert_eq!(coords_at_pos(slice, range.head), coordinates.into())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-02 18:51:29 +02:00
|
|
|
fn horizontal_moves_through_multiline_text() {
|
2021-06-11 14:57:07 +02:00
|
|
|
let text = Rope::from(MULTILINE_SAMPLE);
|
|
|
|
let slice = text.slice(..);
|
2021-07-21 03:15:34 +02:00
|
|
|
let position = pos_at_coords(slice, (0, 0).into(), true);
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
let mut range = Range::point(position);
|
|
|
|
|
2021-12-18 06:57:49 +01:00
|
|
|
let moves_and_expected_coordinates = [
|
2021-07-02 18:51:29 +02:00
|
|
|
((Direction::Forward, 11usize), (1, 1)), // Multiline\nt|ext sample\n...
|
|
|
|
((Direction::Backward, 1usize), (1, 0)), // Multiline\n|text sample\n...
|
|
|
|
((Direction::Backward, 5usize), (0, 5)), // Multi|line\ntext sample\n...
|
|
|
|
((Direction::Backward, 999usize), (0, 0)), // |Multiline\ntext sample\n...
|
|
|
|
((Direction::Forward, 3usize), (0, 3)), // Mul|tiline\ntext sample\n...
|
|
|
|
((Direction::Forward, 0usize), (0, 3)), // Mul|tiline\ntext sample\n...
|
|
|
|
((Direction::Backward, 0usize), (0, 3)), // Mul|tiline\ntext sample\n...
|
|
|
|
((Direction::Forward, 999usize), (5, 0)), // ...and whitespaced\n|
|
|
|
|
((Direction::Forward, 999usize), (5, 0)), // ...and whitespaced\n|
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
for ((direction, amount), coordinates) in moves_and_expected_coordinates {
|
2022-06-21 18:36:36 +02:00
|
|
|
range = move_horizontally(slice, range, direction, amount, Movement::Move, 0);
|
2021-06-11 14:57:07 +02:00
|
|
|
assert_eq!(coords_at_pos(slice, range.head), coordinates.into());
|
|
|
|
assert_eq!(range.head, range.anchor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn selection_extending_moves_in_single_line_text() {
|
|
|
|
let text = Rope::from(SINGLE_LINE_SAMPLE);
|
|
|
|
let slice = text.slice(..);
|
2021-07-21 03:15:34 +02:00
|
|
|
let position = pos_at_coords(slice, (0, 0).into(), true);
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
let mut range = Range::point(position);
|
|
|
|
let original_anchor = range.anchor;
|
|
|
|
|
2021-12-18 06:57:49 +01:00
|
|
|
let moves = [
|
2021-06-11 14:57:07 +02:00
|
|
|
(Direction::Forward, 1usize),
|
|
|
|
(Direction::Forward, 5usize),
|
|
|
|
(Direction::Backward, 3usize),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
for (direction, amount) in moves {
|
2022-06-21 18:36:36 +02:00
|
|
|
range = move_horizontally(slice, range, direction, amount, Movement::Extend, 0);
|
2021-06-11 14:57:07 +02:00
|
|
|
assert_eq!(range.anchor, original_anchor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vertical_moves_in_single_column() {
|
|
|
|
let text = Rope::from(MULTILINE_SAMPLE);
|
2021-07-09 01:45:19 +02:00
|
|
|
let slice = text.slice(..);
|
2021-07-21 03:15:34 +02:00
|
|
|
let position = pos_at_coords(slice, (0, 0).into(), true);
|
2021-06-11 14:57:07 +02:00
|
|
|
let mut range = Range::point(position);
|
2021-12-18 06:57:49 +01:00
|
|
|
let moves_and_expected_coordinates = [
|
2021-06-11 14:57:07 +02:00
|
|
|
((Direction::Forward, 1usize), (1, 0)),
|
|
|
|
((Direction::Forward, 2usize), (3, 0)),
|
2021-07-02 08:36:09 +02:00
|
|
|
((Direction::Forward, 1usize), (4, 0)),
|
2021-06-11 14:57:07 +02:00
|
|
|
((Direction::Backward, 999usize), (0, 0)),
|
2021-07-02 08:36:09 +02:00
|
|
|
((Direction::Forward, 4usize), (4, 0)),
|
|
|
|
((Direction::Forward, 0usize), (4, 0)),
|
|
|
|
((Direction::Backward, 0usize), (4, 0)),
|
|
|
|
((Direction::Forward, 5), (5, 0)),
|
|
|
|
((Direction::Forward, 999usize), (5, 0)),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
for ((direction, amount), coordinates) in moves_and_expected_coordinates {
|
2022-06-21 18:36:36 +02:00
|
|
|
range = move_vertically(slice, range, direction, amount, Movement::Move, 4);
|
2021-06-11 14:57:07 +02:00
|
|
|
assert_eq!(coords_at_pos(slice, range.head), coordinates.into());
|
|
|
|
assert_eq!(range.head, range.anchor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn vertical_moves_jumping_column() {
|
|
|
|
let text = Rope::from(MULTILINE_SAMPLE);
|
|
|
|
let slice = text.slice(..);
|
2021-07-21 03:15:34 +02:00
|
|
|
let position = pos_at_coords(slice, (0, 0).into(), true);
|
2021-06-11 14:57:07 +02:00
|
|
|
let mut range = Range::point(position);
|
|
|
|
|
|
|
|
enum Axis {
|
|
|
|
H,
|
|
|
|
V,
|
|
|
|
}
|
2021-12-18 06:57:49 +01:00
|
|
|
let moves_and_expected_coordinates = [
|
2021-06-11 14:57:07 +02:00
|
|
|
// Places cursor at the end of line
|
|
|
|
((Axis::H, Direction::Forward, 8usize), (0, 8)),
|
|
|
|
// First descent preserves column as the target line is wider
|
|
|
|
((Axis::V, Direction::Forward, 1usize), (1, 8)),
|
|
|
|
// Second descent clamps column as the target line is shorter
|
2021-06-21 00:09:10 +02:00
|
|
|
((Axis::V, Direction::Forward, 1usize), (2, 5)),
|
2021-06-11 14:57:07 +02:00
|
|
|
// Third descent restores the original column
|
|
|
|
((Axis::V, Direction::Forward, 1usize), (3, 8)),
|
|
|
|
// Behaviour is preserved even through long jumps
|
|
|
|
((Axis::V, Direction::Backward, 999usize), (0, 8)),
|
2021-07-02 08:36:09 +02:00
|
|
|
((Axis::V, Direction::Forward, 4usize), (4, 8)),
|
|
|
|
((Axis::V, Direction::Forward, 999usize), (5, 0)),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
for ((axis, direction, amount), coordinates) in moves_and_expected_coordinates {
|
|
|
|
range = match axis {
|
2022-06-21 18:36:36 +02:00
|
|
|
Axis::H => move_horizontally(slice, range, direction, amount, Movement::Move, 0),
|
|
|
|
Axis::V => move_vertically(slice, range, direction, amount, Movement::Move, 4),
|
2021-06-11 14:57:07 +02:00
|
|
|
};
|
|
|
|
assert_eq!(coords_at_pos(slice, range.head), coordinates.into());
|
|
|
|
assert_eq!(range.head, range.anchor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2021-07-21 03:15:34 +02:00
|
|
|
fn multibyte_character_wide_column_jumps() {
|
2021-06-11 14:57:07 +02:00
|
|
|
let text = Rope::from(MULTIBYTE_CHARACTER_SAMPLE);
|
|
|
|
let slice = text.slice(..);
|
2021-07-21 03:15:34 +02:00
|
|
|
let position = pos_at_coords(slice, (0, 0).into(), true);
|
2021-06-11 14:57:07 +02:00
|
|
|
let mut range = Range::point(position);
|
|
|
|
|
|
|
|
// FIXME: The behaviour captured in this test diverges from both Kakoune and Vim. These
|
|
|
|
// will attempt to preserve the horizontal position of the cursor, rather than
|
|
|
|
// placing it at the same character index.
|
|
|
|
enum Axis {
|
|
|
|
H,
|
|
|
|
V,
|
|
|
|
}
|
2021-12-18 06:57:49 +01:00
|
|
|
let moves_and_expected_coordinates = [
|
2021-07-22 22:21:44 +02:00
|
|
|
// Places cursor at the fourth kana.
|
|
|
|
((Axis::H, Direction::Forward, 4), (0, 4)),
|
2022-06-21 18:36:36 +02:00
|
|
|
// Descent places cursor at the 8th character.
|
|
|
|
((Axis::V, Direction::Forward, 1usize), (1, 8)),
|
|
|
|
// Moving back 2 characters.
|
|
|
|
((Axis::H, Direction::Backward, 2usize), (1, 6)),
|
2021-07-22 22:21:44 +02:00
|
|
|
// Jumping back up 1 line.
|
|
|
|
((Axis::V, Direction::Backward, 1usize), (0, 3)),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
for ((axis, direction, amount), coordinates) in moves_and_expected_coordinates {
|
|
|
|
range = match axis {
|
2022-06-21 18:36:36 +02:00
|
|
|
Axis::H => move_horizontally(slice, range, direction, amount, Movement::Move, 0),
|
|
|
|
Axis::V => move_vertically(slice, range, direction, amount, Movement::Move, 4),
|
2021-06-11 14:57:07 +02:00
|
|
|
};
|
|
|
|
assert_eq!(coords_at_pos(slice, range.head), coordinates.into());
|
|
|
|
assert_eq!(range.head, range.anchor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn nonsensical_ranges_panic_on_forward_movement_attempt_in_debug_mode() {
|
|
|
|
move_next_word_start(Rope::from("Sample").slice(..), Range::point(99999999), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn nonsensical_ranges_panic_on_forward_to_end_movement_attempt_in_debug_mode() {
|
|
|
|
move_next_word_end(Rope::from("Sample").slice(..), Range::point(99999999), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[should_panic]
|
|
|
|
fn nonsensical_ranges_panic_on_backwards_movement_attempt_in_debug_mode() {
|
|
|
|
move_prev_word_start(Rope::from("Sample").slice(..), Range::point(99999999), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_start_of_next_words() {
|
2021-12-18 06:57:49 +01:00
|
|
|
let tests = [
|
2021-06-11 14:57:07 +02:00
|
|
|
("Basic forward motion stops at the first space",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 6))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
(" Starting from a boundary advances the anchor",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(1, 10))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Long whitespace gap is bridged by the head",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 11))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Previous anchor is irrelevant for forward motions",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(12, 0), Range::new(0, 9))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
(" Starting from whitespace moves to last space in sequence",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 4))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Starting from mid-word leaves anchor at start position and moves head",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(3, 3), Range::new(3, 9))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Identifiers_with_underscores are considered a single word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 29))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Jumping\n into starting whitespace selects the spaces before 'into'",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 7), Range::new(8, 12))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("alphanumeric.!,and.?=punctuation are considered 'words' for the purposes of word motion",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 12)),
|
|
|
|
(1, Range::new(0, 12), Range::new(12, 15)),
|
|
|
|
(1, Range::new(12, 15), Range::new(15, 18))
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("... ... punctuation and spaces behave as expected",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 6)),
|
|
|
|
(1, Range::new(0, 6), Range::new(6, 10)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
(".._.._ punctuation is not joined by underscores into a single block",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 2))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Newlines\n\nare bridged seamlessly.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 8)),
|
|
|
|
(1, Range::new(0, 8), Range::new(10, 14)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("Jumping\n\n\n\n\n\n from newlines to whitespace selects whitespace.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 9), Range::new(13, 16)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("A failed motion does not modify the range",
|
|
|
|
vec![
|
|
|
|
(3, Range::new(37, 41), Range::new(37, 41)),
|
|
|
|
]),
|
|
|
|
("oh oh oh two character words!",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 3)),
|
|
|
|
(1, Range::new(0, 3), Range::new(3, 6)),
|
|
|
|
(1, Range::new(0, 2), Range::new(1, 3)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("Multiple motions at once resolve correctly",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(3, Range::new(0, 0), Range::new(17, 20)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("Excessive motions are performed partially",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(999, Range::new(0, 0), Range::new(32, 41)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("", // Edge case of moving forward in empty string
|
|
|
|
vec![
|
|
|
|
(1, Range::new(0, 0), Range::new(0, 0)),
|
|
|
|
]),
|
|
|
|
("\n\n\n\n\n", // Edge case of moving forward in all newlines
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(5, 5)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("\n \n \n Jumping through alternated space blocks and newlines selects the space blocks",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(1, 4)),
|
|
|
|
(1, Range::new(1, 4), Range::new(5, 8)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("ヒーリクス multibyte characters behave as normal characters",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 6)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
for (sample, scenario) in tests {
|
|
|
|
for (count, begin, expected_end) in scenario.into_iter() {
|
|
|
|
let range = move_next_word_start(Rope::from(sample).slice(..), begin, count);
|
|
|
|
assert_eq!(range, expected_end, "Case failed: [{}]", sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:51:00 +02:00
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_start_of_next_long_words() {
|
2021-12-18 06:57:49 +01:00
|
|
|
let tests = [
|
2021-06-29 14:51:00 +02:00
|
|
|
("Basic forward motion stops at the first space",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 6))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
(" Starting from a boundary advances the anchor",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(1, 10))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Long whitespace gap is bridged by the head",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 11))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Previous anchor is irrelevant for forward motions",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(12, 0), Range::new(0, 9))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
(" Starting from whitespace moves to last space in sequence",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 4))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Starting from mid-word leaves anchor at start position and moves head",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(3, 3), Range::new(3, 9))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Identifiers_with_underscores are considered a single word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 29))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Jumping\n into starting whitespace selects the spaces before 'into'",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 7), Range::new(8, 12))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("alphanumeric.!,and.?=punctuation are not treated any differently than alphanumerics",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 33)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("... ... punctuation and spaces behave as expected",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 6)),
|
|
|
|
(1, Range::new(0, 6), Range::new(6, 10)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
(".._.._ punctuation is joined by underscores into a single word, as it behaves like alphanumerics",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 7))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Newlines\n\nare bridged seamlessly.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 8)),
|
|
|
|
(1, Range::new(0, 8), Range::new(10, 14)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("Jumping\n\n\n\n\n\n from newlines to whitespace selects whitespace.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 9), Range::new(13, 16)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("A failed motion does not modify the range",
|
|
|
|
vec![
|
|
|
|
(3, Range::new(37, 41), Range::new(37, 41)),
|
|
|
|
]),
|
|
|
|
("oh oh oh two character words!",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 3)),
|
|
|
|
(1, Range::new(0, 3), Range::new(3, 6)),
|
|
|
|
(1, Range::new(0, 1), Range::new(0, 3)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("Multiple motions at once resolve correctly",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(3, Range::new(0, 0), Range::new(17, 20)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("Excessive motions are performed partially",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(999, Range::new(0, 0), Range::new(32, 41)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("", // Edge case of moving forward in empty string
|
|
|
|
vec![
|
|
|
|
(1, Range::new(0, 0), Range::new(0, 0)),
|
|
|
|
]),
|
|
|
|
("\n\n\n\n\n", // Edge case of moving forward in all newlines
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(5, 5)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("\n \n \n Jumping through alternated space blocks and newlines selects the space blocks",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(1, 4)),
|
|
|
|
(1, Range::new(1, 4), Range::new(5, 8)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("ヒー..リクス multibyte characters behave as normal characters, including their interaction with punctuation",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 8)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-29 14:51:00 +02:00
|
|
|
|
|
|
|
for (sample, scenario) in tests {
|
|
|
|
for (count, begin, expected_end) in scenario.into_iter() {
|
|
|
|
let range = move_next_long_word_start(Rope::from(sample).slice(..), begin, count);
|
|
|
|
assert_eq!(range, expected_end, "Case failed: [{}]", sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_start_of_previous_words() {
|
2021-12-18 06:57:49 +01:00
|
|
|
let tests = [
|
2021-06-11 14:57:07 +02:00
|
|
|
("Basic backward motion from the middle of a word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(3, 3), Range::new(4, 0))]),
|
|
|
|
|
|
|
|
// // Why do we want this behavior? The current behavior fails this
|
|
|
|
// // test, but seems better and more consistent.
|
|
|
|
// ("Starting from after boundary retreats the anchor",
|
|
|
|
// vec![(1, Range::new(0, 9), Range::new(8, 0))]),
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
(" Jump to start of a word preceded by whitespace",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(5, 5), Range::new(6, 4))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
(" Jump to start of line from start of word preceded by whitespace",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(4, 4), Range::new(4, 0))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Previous anchor is irrelevant for backward motions",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(12, 5), Range::new(6, 0))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
(" Starting from whitespace moves to first space in sequence",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 4), Range::new(4, 0))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Identifiers_with_underscores are considered a single word",
|
|
|
|
vec![(1, Range::new(0, 20), Range::new(20, 0))]),
|
|
|
|
("Jumping\n \nback through a newline selects whitespace",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 13), Range::new(12, 8))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Jumping to start of word from the end selects the word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(6, 7), Range::new(7, 0))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("alphanumeric.!,and.?=punctuation are considered 'words' for the purposes of word motion",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(29, 30), Range::new(30, 21)),
|
|
|
|
(1, Range::new(30, 21), Range::new(21, 18)),
|
|
|
|
(1, Range::new(21, 18), Range::new(18, 15))
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("... ... punctuation and spaces behave as expected",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 10), Range::new(10, 6)),
|
|
|
|
(1, Range::new(10, 6), Range::new(6, 0)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
(".._.._ punctuation is not joined by underscores into a single block",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 6), Range::new(5, 3))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Newlines\n\nare bridged seamlessly.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 10), Range::new(8, 0)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("Jumping \n\n\n\n\nback from within a newline group selects previous block",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 13), Range::new(11, 0)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("Failed motions do not modify the range",
|
|
|
|
vec![
|
|
|
|
(0, Range::new(3, 0), Range::new(3, 0)),
|
|
|
|
]),
|
|
|
|
("Multiple motions at once resolve correctly",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(3, Range::new(18, 18), Range::new(9, 0)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("Excessive motions are performed partially",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(999, Range::new(40, 40), Range::new(10, 0)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("", // Edge case of moving backwards in empty string
|
|
|
|
vec![
|
|
|
|
(1, Range::new(0, 0), Range::new(0, 0)),
|
|
|
|
]),
|
|
|
|
("\n\n\n\n\n", // Edge case of moving backwards in all newlines
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(5, 5), Range::new(0, 0)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
(" \n \nJumping back through alternated space blocks and newlines selects the space blocks",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 8), Range::new(7, 4)),
|
|
|
|
(1, Range::new(7, 4), Range::new(3, 0)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("ヒーリクス multibyte characters behave as normal characters",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 6), Range::new(6, 0)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
for (sample, scenario) in tests {
|
|
|
|
for (count, begin, expected_end) in scenario.into_iter() {
|
|
|
|
let range = move_prev_word_start(Rope::from(sample).slice(..), begin, count);
|
|
|
|
assert_eq!(range, expected_end, "Case failed: [{}]", sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:51:00 +02:00
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_start_of_previous_long_words() {
|
2021-12-18 06:57:49 +01:00
|
|
|
let tests = [
|
2021-07-19 06:59:31 +02:00
|
|
|
(
|
|
|
|
"Basic backward motion from the middle of a word",
|
|
|
|
vec![(1, Range::new(3, 3), Range::new(4, 0))],
|
|
|
|
),
|
|
|
|
|
|
|
|
// // Why do we want this behavior? The current behavior fails this
|
|
|
|
// // test, but seems better and more consistent.
|
|
|
|
// ("Starting from after boundary retreats the anchor",
|
|
|
|
// vec![(1, Range::new(0, 9), Range::new(8, 0))]),
|
|
|
|
|
|
|
|
(
|
|
|
|
" Jump to start of a word preceded by whitespace",
|
|
|
|
vec![(1, Range::new(5, 5), Range::new(6, 4))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
" Jump to start of line from start of word preceded by whitespace",
|
|
|
|
vec![(1, Range::new(3, 4), Range::new(4, 0))],
|
|
|
|
),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Previous anchor is irrelevant for backward motions",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(12, 5), Range::new(6, 0))]),
|
|
|
|
(
|
|
|
|
" Starting from whitespace moves to first space in sequence",
|
|
|
|
vec![(1, Range::new(0, 4), Range::new(4, 0))],
|
|
|
|
),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Identifiers_with_underscores are considered a single word",
|
|
|
|
vec![(1, Range::new(0, 20), Range::new(20, 0))]),
|
2021-07-19 06:59:31 +02:00
|
|
|
(
|
|
|
|
"Jumping\n \nback through a newline selects whitespace",
|
|
|
|
vec![(1, Range::new(0, 13), Range::new(12, 8))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Jumping to start of word from the end selects the word",
|
|
|
|
vec![(1, Range::new(6, 7), Range::new(7, 0))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"alphanumeric.!,and.?=punctuation are treated exactly the same",
|
|
|
|
vec![(1, Range::new(29, 30), Range::new(30, 0))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"... ... punctuation and spaces behave as expected",
|
2021-06-29 14:51:00 +02:00
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 10), Range::new(10, 6)),
|
|
|
|
(1, Range::new(10, 6), Range::new(6, 0)),
|
|
|
|
],
|
|
|
|
),
|
2021-06-29 14:51:00 +02:00
|
|
|
(".._.._ punctuation is joined by underscores into a single block",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 6), Range::new(6, 0))]),
|
|
|
|
(
|
|
|
|
"Newlines\n\nare bridged seamlessly.",
|
|
|
|
vec![(1, Range::new(0, 10), Range::new(8, 0))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Jumping \n\n\n\n\nback from within a newline group selects previous block",
|
|
|
|
vec![(1, Range::new(0, 13), Range::new(11, 0))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Failed motions do not modify the range",
|
|
|
|
vec![(0, Range::new(3, 0), Range::new(3, 0))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Multiple motions at once resolve correctly",
|
|
|
|
vec![(3, Range::new(19, 19), Range::new(9, 0))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Excessive motions are performed partially",
|
|
|
|
vec![(999, Range::new(40, 40), Range::new(10, 0))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"", // Edge case of moving backwards in empty string
|
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 0))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"\n\n\n\n\n", // Edge case of moving backwards in all newlines
|
|
|
|
vec![(1, Range::new(5, 5), Range::new(0, 0))],
|
|
|
|
),
|
2021-06-29 14:51:00 +02:00
|
|
|
(" \n \nJumping back through alternated space blocks and newlines selects the space blocks",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 8), Range::new(7, 4)),
|
|
|
|
(1, Range::new(7, 4), Range::new(3, 0)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("ヒーリ..クス multibyte characters behave as normal characters, including when interacting with punctuation",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 8), Range::new(8, 0)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-29 14:51:00 +02:00
|
|
|
|
|
|
|
for (sample, scenario) in tests {
|
|
|
|
for (count, begin, expected_end) in scenario.into_iter() {
|
|
|
|
let range = move_prev_long_word_start(Rope::from(sample).slice(..), begin, count);
|
|
|
|
assert_eq!(range, expected_end, "Case failed: [{}]", sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_end_of_next_words() {
|
2021-12-18 06:57:49 +01:00
|
|
|
let tests = [
|
2021-06-11 14:57:07 +02:00
|
|
|
("Basic forward motion from the start of a word to the end of it",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 5))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Basic forward motion from the end of a word to the end of the next",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 5), Range::new(5, 13))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Basic forward motion from the middle of a word to the end of it",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(2, 2), Range::new(2, 5))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
(" Jumping to end of a word preceded by whitespace",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 11))]),
|
|
|
|
|
|
|
|
// // Why do we want this behavior? The current behavior fails this
|
|
|
|
// // test, but seems better and more consistent.
|
|
|
|
// (" Starting from a boundary advances the anchor",
|
|
|
|
// vec![(1, Range::new(0, 0), Range::new(1, 9))]),
|
|
|
|
|
2021-06-11 14:57:07 +02:00
|
|
|
("Previous anchor is irrelevant for end of word motion",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(12, 2), Range::new(2, 8))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Identifiers_with_underscores are considered a single word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 28))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Jumping\n into starting whitespace selects up to the end of next word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 7), Range::new(8, 16))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("alphanumeric.!,and.?=punctuation are considered 'words' for the purposes of word motion",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 12)),
|
|
|
|
(1, Range::new(0, 12), Range::new(12, 15)),
|
|
|
|
(1, Range::new(12, 15), Range::new(15, 18))
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("... ... punctuation and spaces behave as expected",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 3)),
|
|
|
|
(1, Range::new(0, 3), Range::new(3, 9)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
(".._.._ punctuation is not joined by underscores into a single block",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 2))]),
|
2021-06-11 14:57:07 +02:00
|
|
|
("Newlines\n\nare bridged seamlessly.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 8)),
|
|
|
|
(1, Range::new(0, 8), Range::new(10, 13)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("Jumping\n\n\n\n\n\n from newlines to whitespace selects to end of next word.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 8), Range::new(13, 20)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("A failed motion does not modify the range",
|
|
|
|
vec![
|
|
|
|
(3, Range::new(37, 41), Range::new(37, 41)),
|
|
|
|
]),
|
|
|
|
("Multiple motions at once resolve correctly",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(3, Range::new(0, 0), Range::new(16, 19)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("Excessive motions are performed partially",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(999, Range::new(0, 0), Range::new(31, 41)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("", // Edge case of moving forward in empty string
|
|
|
|
vec![
|
|
|
|
(1, Range::new(0, 0), Range::new(0, 0)),
|
|
|
|
]),
|
|
|
|
("\n\n\n\n\n", // Edge case of moving forward in all newlines
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(5, 5)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("\n \n \n Jumping through alternated space blocks and newlines selects the space blocks",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(1, 4)),
|
|
|
|
(1, Range::new(1, 4), Range::new(5, 8)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
|
|
|
("ヒーリクス multibyte characters behave as normal characters",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 5)),
|
2021-06-11 14:57:07 +02:00
|
|
|
]),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-11 14:57:07 +02:00
|
|
|
|
|
|
|
for (sample, scenario) in tests {
|
|
|
|
for (count, begin, expected_end) in scenario.into_iter() {
|
|
|
|
let range = move_next_word_end(Rope::from(sample).slice(..), begin, count);
|
|
|
|
assert_eq!(range, expected_end, "Case failed: [{}]", sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-06-29 14:51:00 +02:00
|
|
|
|
2021-07-03 03:07:49 +02:00
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_end_of_previous_words() {
|
2021-12-18 06:57:49 +01:00
|
|
|
let tests = [
|
2021-07-03 03:07:49 +02:00
|
|
|
("Basic backward motion from the middle of a word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(9, 9), Range::new(10, 5))]),
|
2021-07-03 03:07:49 +02:00
|
|
|
("Starting from after boundary retreats the anchor",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 14), Range::new(13, 8))]),
|
2021-07-03 03:07:49 +02:00
|
|
|
("Jump to end of a word succeeded by whitespace",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(11, 11), Range::new(11, 4))]),
|
2021-07-03 03:07:49 +02:00
|
|
|
(" Jump to start of line from end of word preceded by whitespace",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(8, 8), Range::new(8, 0))]),
|
2021-07-03 03:07:49 +02:00
|
|
|
("Previous anchor is irrelevant for backward motions",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(26, 12), Range::new(13, 8))]),
|
2021-07-03 03:07:49 +02:00
|
|
|
(" Starting from whitespace moves to first space in sequence",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 4), Range::new(4, 0))]),
|
2021-07-03 03:07:49 +02:00
|
|
|
("Test identifiers_with_underscores are considered a single word",
|
|
|
|
vec![(1, Range::new(0, 25), Range::new(25, 4))]),
|
|
|
|
("Jumping\n \nback through a newline selects whitespace",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 13), Range::new(12, 8))]),
|
2021-07-03 03:07:49 +02:00
|
|
|
("Jumping to start of word from the end selects the whole word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(16, 16), Range::new(16, 10))]),
|
2021-07-03 03:07:49 +02:00
|
|
|
("alphanumeric.!,and.?=punctuation are considered 'words' for the purposes of word motion",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(30, 30), Range::new(31, 21)),
|
|
|
|
(1, Range::new(31, 21), Range::new(21, 18)),
|
|
|
|
(1, Range::new(21, 18), Range::new(18, 15))
|
2021-07-03 03:07:49 +02:00
|
|
|
]),
|
|
|
|
|
|
|
|
("... ... punctuation and spaces behave as expected",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 10), Range::new(9, 3)),
|
|
|
|
(1, Range::new(9, 3), Range::new(3, 0)),
|
2021-07-03 03:07:49 +02:00
|
|
|
]),
|
|
|
|
(".._.._ punctuation is not joined by underscores into a single block",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 5), Range::new(5, 3))]),
|
2021-07-03 03:07:49 +02:00
|
|
|
("Newlines\n\nare bridged seamlessly.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 10), Range::new(8, 0)),
|
2021-07-03 03:07:49 +02:00
|
|
|
]),
|
|
|
|
("Jumping \n\n\n\n\nback from within a newline group selects previous block",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 13), Range::new(11, 7)),
|
2021-07-03 03:07:49 +02:00
|
|
|
]),
|
|
|
|
("Failed motions do not modify the range",
|
|
|
|
vec![
|
|
|
|
(0, Range::new(3, 0), Range::new(3, 0)),
|
|
|
|
]),
|
|
|
|
("Multiple motions at once resolve correctly",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(3, Range::new(24, 24), Range::new(16, 8)),
|
2021-07-03 03:07:49 +02:00
|
|
|
]),
|
|
|
|
("Excessive motions are performed partially",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(999, Range::new(40, 40), Range::new(9, 0)),
|
2021-07-03 03:07:49 +02:00
|
|
|
]),
|
|
|
|
("", // Edge case of moving backwards in empty string
|
|
|
|
vec![
|
|
|
|
(1, Range::new(0, 0), Range::new(0, 0)),
|
|
|
|
]),
|
|
|
|
("\n\n\n\n\n", // Edge case of moving backwards in all newlines
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(5, 5), Range::new(0, 0)),
|
2021-07-03 03:07:49 +02:00
|
|
|
]),
|
|
|
|
(" \n \nJumping back through alternated space blocks and newlines selects the space blocks",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 8), Range::new(7, 4)),
|
|
|
|
(1, Range::new(7, 4), Range::new(3, 0)),
|
2021-07-03 03:07:49 +02:00
|
|
|
]),
|
|
|
|
("Test ヒーリクス multibyte characters behave as normal characters",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 10), Range::new(10, 4)),
|
2021-07-03 03:07:49 +02:00
|
|
|
]),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-07-03 03:07:49 +02:00
|
|
|
|
|
|
|
for (sample, scenario) in tests {
|
|
|
|
for (count, begin, expected_end) in scenario.into_iter() {
|
|
|
|
let range = move_prev_word_end(Rope::from(sample).slice(..), begin, count);
|
|
|
|
assert_eq!(range, expected_end, "Case failed: [{}]", sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-29 14:51:00 +02:00
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_end_of_next_long_words() {
|
2021-12-18 06:57:49 +01:00
|
|
|
let tests = [
|
2021-06-29 14:51:00 +02:00
|
|
|
("Basic forward motion from the start of a word to the end of it",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 5))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Basic forward motion from the end of a word to the end of the next",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 5), Range::new(5, 13))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Basic forward motion from the middle of a word to the end of it",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(2, 2), Range::new(2, 5))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
(" Jumping to end of a word preceded by whitespace",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 11))]),
|
|
|
|
|
|
|
|
// // Why do we want this behavior? The current behavior fails this
|
|
|
|
// // test, but seems better and more consistent.
|
|
|
|
// (" Starting from a boundary advances the anchor",
|
|
|
|
// vec![(1, Range::new(0, 0), Range::new(1, 9))]),
|
|
|
|
|
2021-06-29 14:51:00 +02:00
|
|
|
("Previous anchor is irrelevant for end of word motion",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(12, 2), Range::new(2, 8))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Identifiers_with_underscores are considered a single word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 28))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Jumping\n into starting whitespace selects up to the end of next word",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 7), Range::new(8, 16))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("alphanumeric.!,and.?=punctuation are treated the same way",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 32)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("... ... punctuation and spaces behave as expected",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 3)),
|
|
|
|
(1, Range::new(0, 3), Range::new(3, 9)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
(".._.._ punctuation is joined by underscores into a single block",
|
2021-07-19 06:59:31 +02:00
|
|
|
vec![(1, Range::new(0, 0), Range::new(0, 6))]),
|
2021-06-29 14:51:00 +02:00
|
|
|
("Newlines\n\nare bridged seamlessly.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 8)),
|
|
|
|
(1, Range::new(0, 8), Range::new(10, 13)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("Jumping\n\n\n\n\n\n from newlines to whitespace selects to end of next word.",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 9), Range::new(13, 20)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("A failed motion does not modify the range",
|
|
|
|
vec![
|
|
|
|
(3, Range::new(37, 41), Range::new(37, 41)),
|
|
|
|
]),
|
|
|
|
("Multiple motions at once resolve correctly",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(3, Range::new(0, 0), Range::new(16, 19)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("Excessive motions are performed partially",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(999, Range::new(0, 0), Range::new(31, 41)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("", // Edge case of moving forward in empty string
|
|
|
|
vec![
|
|
|
|
(1, Range::new(0, 0), Range::new(0, 0)),
|
|
|
|
]),
|
|
|
|
("\n\n\n\n\n", // Edge case of moving forward in all newlines
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(5, 5)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("\n \n \n Jumping through alternated space blocks and newlines selects the space blocks",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(1, 4)),
|
|
|
|
(1, Range::new(1, 4), Range::new(5, 8)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
|
|
|
("ヒーリ..クス multibyte characters behave as normal characters, including when they interact with punctuation",
|
|
|
|
vec![
|
2021-07-19 06:59:31 +02:00
|
|
|
(1, Range::new(0, 0), Range::new(0, 7)),
|
2021-06-29 14:51:00 +02:00
|
|
|
]),
|
2021-12-18 06:57:49 +01:00
|
|
|
];
|
2021-06-29 14:51:00 +02:00
|
|
|
|
|
|
|
for (sample, scenario) in tests {
|
|
|
|
for (count, begin, expected_end) in scenario.into_iter() {
|
|
|
|
let range = move_next_long_word_end(Rope::from(sample).slice(..), begin, count);
|
|
|
|
assert_eq!(range, expected_end, "Case failed: [{}]", sample);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-07 16:37:09 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_prev_paragraph_single() {
|
|
|
|
let tests = [
|
2022-03-03 01:55:26 +01:00
|
|
|
("#[|]#", "#[|]#"),
|
|
|
|
("#[s|]#tart at\nfirst char\n", "#[|s]#tart at\nfirst char\n"),
|
|
|
|
("start at\nlast char#[\n|]#", "#[|start at\nlast char\n]#"),
|
2022-02-07 16:37:09 +01:00
|
|
|
(
|
2022-03-03 01:55:26 +01:00
|
|
|
"goto\nfirst\n\n#[p|]#aragraph",
|
|
|
|
"#[|goto\nfirst\n\n]#paragraph",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"goto\nfirst\n#[\n|]#paragraph",
|
|
|
|
"#[|goto\nfirst\n\n]#paragraph",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"goto\nsecond\n\np#[a|]#ragraph",
|
|
|
|
"goto\nsecond\n\n#[|pa]#ragraph",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"here\n\nhave\nmultiple\nparagraph\n\n\n\n\n#[|]#",
|
|
|
|
"here\n\n#[|have\nmultiple\nparagraph\n\n\n\n\n]#",
|
2022-02-07 16:37:09 +01:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
2022-02-21 12:07:16 +01:00
|
|
|
for (before, expected) in tests {
|
|
|
|
let (s, selection) = crate::test::print(before);
|
2022-02-07 16:37:09 +01:00
|
|
|
let text = Rope::from(s.as_str());
|
|
|
|
let selection =
|
2022-03-07 14:25:26 +01:00
|
|
|
selection.transform(|r| move_prev_paragraph(text.slice(..), r, 1, Movement::Move));
|
2022-02-07 16:37:09 +01:00
|
|
|
let actual = crate::test::plain(&s, selection);
|
2022-04-08 17:56:50 +02:00
|
|
|
assert_eq!(actual, expected, "\nbefore: `{:?}`", before);
|
2022-02-07 16:37:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-02-21 12:07:16 +01:00
|
|
|
fn test_behaviour_when_moving_to_prev_paragraph_double() {
|
|
|
|
let tests = [
|
2022-03-03 01:55:26 +01:00
|
|
|
(
|
|
|
|
"on#[e|]#\n\ntwo\n\nthree\n\n",
|
|
|
|
"#[|one]#\n\ntwo\n\nthree\n\n",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"one\n\ntwo\n\nth#[r|]#ee\n\n",
|
|
|
|
"one\n\n#[|two\n\nthr]#ee\n\n",
|
|
|
|
),
|
2022-02-21 12:07:16 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
for (before, expected) in tests {
|
|
|
|
let (s, selection) = crate::test::print(before);
|
|
|
|
let text = Rope::from(s.as_str());
|
|
|
|
let selection =
|
2022-03-07 14:25:26 +01:00
|
|
|
selection.transform(|r| move_prev_paragraph(text.slice(..), r, 2, Movement::Move));
|
2022-02-21 12:07:16 +01:00
|
|
|
let actual = crate::test::plain(&s, selection);
|
2022-04-08 17:56:50 +02:00
|
|
|
assert_eq!(actual, expected, "\nbefore: `{:?}`", before);
|
2022-02-21 12:07:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_prev_paragraph_extend() {
|
|
|
|
let tests = [
|
2022-03-03 01:55:26 +01:00
|
|
|
(
|
|
|
|
"one\n\n#[|two\n\n]#three\n\n",
|
|
|
|
"#[|one\n\ntwo\n\n]#three\n\n",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"#[|one\n\ntwo\n\n]#three\n\n",
|
|
|
|
"#[|one\n\ntwo\n\n]#three\n\n",
|
|
|
|
),
|
2022-02-21 12:07:16 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
for (before, expected) in tests {
|
|
|
|
let (s, selection) = crate::test::print(before);
|
|
|
|
let text = Rope::from(s.as_str());
|
2022-03-07 14:25:26 +01:00
|
|
|
let selection = selection
|
|
|
|
.transform(|r| move_prev_paragraph(text.slice(..), r, 1, Movement::Extend));
|
2022-02-21 12:07:16 +01:00
|
|
|
let actual = crate::test::plain(&s, selection);
|
2022-04-08 17:56:50 +02:00
|
|
|
assert_eq!(actual, expected, "\nbefore: `{:?}`", before);
|
2022-02-21 12:07:16 +01:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 16:37:09 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_next_paragraph_single() {
|
|
|
|
let tests = [
|
2022-03-03 01:55:26 +01:00
|
|
|
("#[|]#", "#[|]#"),
|
|
|
|
("#[s|]#tart at\nfirst char\n", "#[start at\nfirst char\n|]#"),
|
|
|
|
("start at\nlast char#[\n|]#", "start at\nlast char#[\n|]#"),
|
2022-02-07 16:37:09 +01:00
|
|
|
(
|
2022-03-03 01:55:26 +01:00
|
|
|
"a\nb\n\n#[g|]#oto\nthird\n\nparagraph",
|
|
|
|
"a\nb\n\n#[goto\nthird\n\n|]#paragraph",
|
2022-02-07 16:37:09 +01:00
|
|
|
),
|
|
|
|
(
|
2022-03-03 01:55:26 +01:00
|
|
|
"a\nb\n#[\n|]#goto\nthird\n\nparagraph",
|
|
|
|
"a\nb\n\n#[goto\nthird\n\n|]#paragraph",
|
2022-02-07 16:37:09 +01:00
|
|
|
),
|
|
|
|
(
|
2022-03-03 01:55:26 +01:00
|
|
|
"a\nb#[\n|]#\ngoto\nsecond\n\nparagraph",
|
|
|
|
"a\nb#[\n\n|]#goto\nsecond\n\nparagraph",
|
2022-02-07 16:37:09 +01:00
|
|
|
),
|
|
|
|
(
|
2022-03-03 01:55:26 +01:00
|
|
|
"here\n\nhave\n#[m|]#ultiple\nparagraph\n\n\n\n\n",
|
|
|
|
"here\n\nhave\n#[multiple\nparagraph\n\n\n\n\n|]#",
|
2022-02-07 16:37:09 +01:00
|
|
|
),
|
2022-04-05 02:43:14 +02:00
|
|
|
(
|
|
|
|
"#[t|]#ext\n\n\nafter two blank lines\n\nmore text\n",
|
|
|
|
"#[text\n\n\n|]#after two blank lines\n\nmore text\n",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"#[text\n\n\n|]#after two blank lines\n\nmore text\n",
|
|
|
|
"text\n\n\n#[after two blank lines\n\n|]#more text\n",
|
|
|
|
),
|
2022-02-07 16:37:09 +01:00
|
|
|
];
|
|
|
|
|
2022-02-21 12:07:16 +01:00
|
|
|
for (before, expected) in tests {
|
|
|
|
let (s, selection) = crate::test::print(before);
|
2022-02-07 16:37:09 +01:00
|
|
|
let text = Rope::from(s.as_str());
|
|
|
|
let selection =
|
2022-03-07 14:25:26 +01:00
|
|
|
selection.transform(|r| move_next_paragraph(text.slice(..), r, 1, Movement::Move));
|
2022-02-07 16:37:09 +01:00
|
|
|
let actual = crate::test::plain(&s, selection);
|
2022-04-08 17:56:50 +02:00
|
|
|
assert_eq!(actual, expected, "\nbefore: `{:?}`", before);
|
2022-02-21 12:07:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_next_paragraph_double() {
|
|
|
|
let tests = [
|
2022-03-03 01:55:26 +01:00
|
|
|
(
|
|
|
|
"one\n\ntwo\n\nth#[r|]#ee\n\n",
|
|
|
|
"one\n\ntwo\n\nth#[ree\n\n|]#",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"on#[e|]#\n\ntwo\n\nthree\n\n",
|
|
|
|
"on#[e\n\ntwo\n\n|]#three\n\n",
|
|
|
|
),
|
2022-02-21 12:07:16 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
for (before, expected) in tests {
|
|
|
|
let (s, selection) = crate::test::print(before);
|
|
|
|
let text = Rope::from(s.as_str());
|
|
|
|
let selection =
|
2022-03-07 14:25:26 +01:00
|
|
|
selection.transform(|r| move_next_paragraph(text.slice(..), r, 2, Movement::Move));
|
2022-02-21 12:07:16 +01:00
|
|
|
let actual = crate::test::plain(&s, selection);
|
2022-04-08 17:56:50 +02:00
|
|
|
assert_eq!(actual, expected, "\nbefore: `{:?}`", before);
|
2022-02-21 12:07:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_behaviour_when_moving_to_next_paragraph_extend() {
|
|
|
|
let tests = [
|
2022-03-03 01:55:26 +01:00
|
|
|
(
|
|
|
|
"one\n\n#[two\n\n|]#three\n\n",
|
|
|
|
"one\n\n#[two\n\nthree\n\n|]#",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"one\n\n#[two\n\nthree\n\n|]#",
|
|
|
|
"one\n\n#[two\n\nthree\n\n|]#",
|
|
|
|
),
|
2022-02-21 12:07:16 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
for (before, expected) in tests {
|
|
|
|
let (s, selection) = crate::test::print(before);
|
|
|
|
let text = Rope::from(s.as_str());
|
2022-03-07 14:25:26 +01:00
|
|
|
let selection = selection
|
|
|
|
.transform(|r| move_next_paragraph(text.slice(..), r, 1, Movement::Extend));
|
2022-02-21 12:07:16 +01:00
|
|
|
let actual = crate::test::plain(&s, selection);
|
2022-04-08 17:56:50 +02:00
|
|
|
assert_eq!(actual, expected, "\nbefore: `{:?}`", before);
|
2022-02-07 16:37:09 +01:00
|
|
|
}
|
|
|
|
}
|
2021-03-18 07:07:02 +01:00
|
|
|
}
|