2021-10-23 06:41:19 +04:00
|
|
|
use std::fmt::Display;
|
|
|
|
|
2021-07-03 05:07:49 +04:00
|
|
|
use ropey::RopeSlice;
|
2021-10-23 06:41:19 +04:00
|
|
|
use tree_sitter::{Node, QueryCursor};
|
2021-07-03 05:07:49 +04:00
|
|
|
|
2021-08-06 04:32:33 +04:00
|
|
|
use crate::chars::{categorize_char, char_is_whitespace, CharCategory};
|
2022-02-21 18:58:54 +04:00
|
|
|
use crate::graphemes::{next_grapheme_boundary, prev_grapheme_boundary};
|
|
|
|
use crate::line_ending::rope_is_line_ending;
|
2021-07-09 03:45:19 +04:00
|
|
|
use crate::movement::Direction;
|
2021-07-03 05:07:49 +04:00
|
|
|
use crate::surround;
|
2021-10-23 06:41:19 +04:00
|
|
|
use crate::syntax::LanguageConfiguration;
|
2021-07-03 05:07:49 +04:00
|
|
|
use crate::Range;
|
|
|
|
|
2021-11-08 04:54:39 +04:00
|
|
|
fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction, long: bool) -> usize {
|
2021-07-09 03:45:19 +04:00
|
|
|
use CharCategory::{Eol, Whitespace};
|
2021-07-03 05:07:49 +04:00
|
|
|
|
|
|
|
let iter = match direction {
|
2021-07-09 03:45:19 +04:00
|
|
|
Direction::Forward => slice.chars_at(pos),
|
2021-07-03 05:07:49 +04:00
|
|
|
Direction::Backward => {
|
|
|
|
let mut iter = slice.chars_at(pos);
|
|
|
|
iter.reverse();
|
|
|
|
iter
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-09 03:45:19 +04: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 => {
|
2021-11-08 04:54:39 +04:00
|
|
|
if !long && category != prev_category && pos != 0 && pos != slice.len_chars() {
|
2021-07-03 05:07:49 +04:00
|
|
|
return pos;
|
2021-07-09 03:45:19 +04:00
|
|
|
} else {
|
2021-07-26 21:51:00 +04:00
|
|
|
match direction {
|
|
|
|
Direction::Forward => pos += 1,
|
|
|
|
Direction::Backward => pos = pos.saturating_sub(1),
|
2021-07-09 03:45:19 +04:00
|
|
|
}
|
|
|
|
prev_category = category;
|
2021-07-03 05:07:49 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 03:45:19 +04:00
|
|
|
|
|
|
|
pos
|
2021-07-03 05:07:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum TextObject {
|
|
|
|
Around,
|
|
|
|
Inside,
|
2022-02-11 07:31:52 +04:00
|
|
|
/// Used for moving between objects.
|
|
|
|
Movement,
|
2021-07-03 05:07:49 +04:00
|
|
|
}
|
|
|
|
|
2021-10-23 06:41:19 +04:00
|
|
|
impl Display for TextObject {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
f.write_str(match self {
|
|
|
|
Self::Around => "around",
|
|
|
|
Self::Inside => "inside",
|
2022-02-11 07:31:52 +04:00
|
|
|
Self::Movement => "movement",
|
2021-10-23 06:41:19 +04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 05:07:49 +04:00
|
|
|
// count doesn't do anything yet
|
|
|
|
pub fn textobject_word(
|
|
|
|
slice: RopeSlice,
|
|
|
|
range: Range,
|
|
|
|
textobject: TextObject,
|
2021-07-09 03:45:19 +04:00
|
|
|
_count: usize,
|
2021-11-08 04:54:39 +04:00
|
|
|
long: bool,
|
2021-07-03 05:07:49 +04:00
|
|
|
) -> Range {
|
2021-07-26 19:40:30 +04:00
|
|
|
let pos = range.cursor(slice);
|
2021-07-09 03:45:19 +04:00
|
|
|
|
2021-11-08 04:54:39 +04:00
|
|
|
let word_start = find_word_boundary(slice, pos, Direction::Backward, long);
|
2021-07-26 19:40:30 +04:00
|
|
|
let word_end = match slice.get_char(pos).map(categorize_char) {
|
|
|
|
None | Some(CharCategory::Whitespace | CharCategory::Eol) => pos,
|
2021-11-08 04:54:39 +04:00
|
|
|
_ => find_word_boundary(slice, pos + 1, Direction::Forward, long),
|
2021-07-09 03:45:19 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Special case.
|
|
|
|
if word_start == word_end {
|
|
|
|
return Range::new(word_start, word_end);
|
|
|
|
}
|
2021-07-03 05:07:49 +04:00
|
|
|
|
|
|
|
match textobject {
|
2021-07-09 03:45:19 +04:00
|
|
|
TextObject::Inside => Range::new(word_start, word_end),
|
2021-08-06 04:32:33 +04:00
|
|
|
TextObject::Around => {
|
|
|
|
let whitespace_count_right = slice
|
|
|
|
.chars_at(word_end)
|
|
|
|
.take_while(|c| char_is_whitespace(*c))
|
|
|
|
.count();
|
|
|
|
|
|
|
|
if whitespace_count_right > 0 {
|
|
|
|
Range::new(word_start, word_end + whitespace_count_right)
|
|
|
|
} else {
|
|
|
|
let whitespace_count_left = {
|
|
|
|
let mut iter = slice.chars_at(word_start);
|
|
|
|
iter.reverse();
|
|
|
|
iter.take_while(|c| char_is_whitespace(*c)).count()
|
|
|
|
};
|
|
|
|
Range::new(word_start - whitespace_count_left, word_end)
|
|
|
|
}
|
|
|
|
}
|
2022-02-11 07:31:52 +04:00
|
|
|
TextObject::Movement => unreachable!(),
|
2021-07-09 03:45:19 +04:00
|
|
|
}
|
2021-07-03 05:07:49 +04:00
|
|
|
}
|
|
|
|
|
2022-03-07 17:25:26 +04:00
|
|
|
pub fn textobject_paragraph(
|
2022-02-21 18:58:54 +04:00
|
|
|
slice: RopeSlice,
|
|
|
|
range: Range,
|
|
|
|
textobject: TextObject,
|
|
|
|
count: usize,
|
|
|
|
) -> Range {
|
|
|
|
let mut line = range.cursor_line(slice);
|
|
|
|
let prev_line_empty = rope_is_line_ending(slice.line(line.saturating_sub(1)));
|
|
|
|
let curr_line_empty = rope_is_line_ending(slice.line(line));
|
|
|
|
let next_line_empty = rope_is_line_ending(slice.line(line.saturating_sub(1)));
|
|
|
|
let last_char =
|
|
|
|
prev_grapheme_boundary(slice, slice.line_to_char(line + 1)) == range.cursor(slice);
|
|
|
|
let prev_empty_to_line = prev_line_empty && !curr_line_empty;
|
|
|
|
let curr_empty_to_line = curr_line_empty && !next_line_empty;
|
|
|
|
|
|
|
|
// skip character before paragraph boundary
|
|
|
|
let mut line_back = line; // line but backwards
|
|
|
|
if prev_empty_to_line || curr_empty_to_line {
|
|
|
|
line_back += 1;
|
|
|
|
}
|
|
|
|
let mut lines = slice.lines_at(line_back);
|
|
|
|
// do not include current paragraph on paragraph end (include next)
|
|
|
|
if !(curr_empty_to_line && last_char) {
|
|
|
|
lines.reverse();
|
|
|
|
let mut lines = lines.map(rope_is_line_ending).peekable();
|
|
|
|
while lines.next_if(|&e| e).is_some() {
|
|
|
|
line_back -= 1;
|
|
|
|
}
|
|
|
|
while lines.next_if(|&e| !e).is_some() {
|
|
|
|
line_back -= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip character after paragraph boundary
|
|
|
|
if curr_empty_to_line && last_char {
|
|
|
|
line += 1;
|
|
|
|
}
|
|
|
|
let mut lines = slice.lines_at(line).map(rope_is_line_ending).peekable();
|
|
|
|
for _ in 0..count - 1 {
|
|
|
|
while lines.next_if(|&e| !e).is_some() {
|
|
|
|
line += 1;
|
|
|
|
}
|
|
|
|
while lines.next_if(|&e| e).is_some() {
|
|
|
|
line += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while lines.next_if(|&e| !e).is_some() {
|
|
|
|
line += 1;
|
|
|
|
}
|
|
|
|
// handle last whitespaces part separately depending on textobject
|
|
|
|
match textobject {
|
|
|
|
TextObject::Around => {
|
|
|
|
while lines.next_if(|&e| e).is_some() {
|
|
|
|
line += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
TextObject::Inside => {}
|
|
|
|
TextObject::Movement => unreachable!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
let anchor = slice.line_to_char(line_back);
|
|
|
|
let head = slice.line_to_char(line);
|
|
|
|
Range::new(anchor, head)
|
|
|
|
}
|
|
|
|
|
2021-07-03 05:07:49 +04:00
|
|
|
pub fn textobject_surround(
|
|
|
|
slice: RopeSlice,
|
|
|
|
range: Range,
|
|
|
|
textobject: TextObject,
|
|
|
|
ch: char,
|
|
|
|
count: usize,
|
|
|
|
) -> Range {
|
2021-11-29 05:33:53 +04:00
|
|
|
surround::find_nth_pairs_pos(slice, ch, range, count)
|
2021-07-03 05:07:49 +04:00
|
|
|
.map(|(anchor, head)| match textobject {
|
2021-08-03 18:22:27 +04:00
|
|
|
TextObject::Inside => Range::new(next_grapheme_boundary(slice, anchor), head),
|
|
|
|
TextObject::Around => Range::new(anchor, next_grapheme_boundary(slice, head)),
|
2022-02-11 07:31:52 +04:00
|
|
|
TextObject::Movement => unreachable!(),
|
2021-07-03 05:07:49 +04:00
|
|
|
})
|
|
|
|
.unwrap_or(range)
|
|
|
|
}
|
|
|
|
|
2021-10-23 06:41:19 +04:00
|
|
|
/// Transform the given range to select text objects based on tree-sitter.
|
|
|
|
/// `object_name` is a query capture base name like "function", "class", etc.
|
|
|
|
/// `slice_tree` is the tree-sitter node corresponding to given text slice.
|
|
|
|
pub fn textobject_treesitter(
|
|
|
|
slice: RopeSlice,
|
|
|
|
range: Range,
|
|
|
|
textobject: TextObject,
|
|
|
|
object_name: &str,
|
|
|
|
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));
|
|
|
|
|
|
|
|
let capture_name = format!("{}.{}", object_name, textobject); // eg. function.inner
|
|
|
|
let mut cursor = QueryCursor::new();
|
|
|
|
let node = lang_config
|
|
|
|
.textobject_query()?
|
|
|
|
.capture_nodes(&capture_name, slice_tree, slice, &mut cursor)?
|
|
|
|
.filter(|node| node.byte_range().contains(&byte_pos))
|
|
|
|
.min_by_key(|node| node.byte_range().len())?;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
Some(Range::new(start_char, end_char))
|
|
|
|
};
|
|
|
|
get_range().unwrap_or(range)
|
|
|
|
}
|
|
|
|
|
2021-07-03 05:07:49 +04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::TextObject::*;
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::Range;
|
|
|
|
use ropey::Rope;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_textobject_word() {
|
2021-11-29 05:33:53 +04:00
|
|
|
// (text, [(char position, textobject, final range), ...])
|
2021-07-03 05:07:49 +04:00
|
|
|
let tests = &[
|
|
|
|
(
|
|
|
|
"cursor at beginning of doc",
|
2021-07-09 03:45:19 +04:00
|
|
|
vec![(0, Inside, (0, 6)), (0, Around, (0, 7))],
|
2021-07-03 05:07:49 +04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor at middle of word",
|
|
|
|
vec![
|
2021-07-09 03:45:19 +04:00
|
|
|
(13, Inside, (10, 16)),
|
|
|
|
(10, Inside, (10, 16)),
|
|
|
|
(15, Inside, (10, 16)),
|
2021-08-06 04:32:33 +04:00
|
|
|
(13, Around, (10, 17)),
|
|
|
|
(10, Around, (10, 17)),
|
|
|
|
(15, Around, (10, 17)),
|
2021-07-03 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor between word whitespace",
|
2021-07-09 03:45:19 +04:00
|
|
|
vec![(6, Inside, (6, 6)), (6, Around, (6, 6))],
|
2021-07-03 05:07:49 +04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on word before newline\n",
|
|
|
|
vec![
|
2021-07-09 03:45:19 +04: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 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on newline\nnext line",
|
2021-07-09 03:45:19 +04:00
|
|
|
vec![(17, Inside, (17, 17)), (17, Around, (17, 17))],
|
2021-07-03 05:07:49 +04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on word after newline\nnext line",
|
|
|
|
vec![
|
2021-07-09 03:45:19 +04: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 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on #$%:;* punctuation",
|
|
|
|
vec![
|
2021-07-09 03:45:19 +04:00
|
|
|
(13, Inside, (10, 16)),
|
|
|
|
(10, Inside, (10, 16)),
|
|
|
|
(15, Inside, (10, 16)),
|
2021-08-06 04:32:33 +04:00
|
|
|
(13, Around, (10, 17)),
|
|
|
|
(10, Around, (10, 17)),
|
|
|
|
(15, Around, (10, 17)),
|
2021-07-03 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor on punc%^#$:;.tuation",
|
|
|
|
vec![
|
2021-07-09 03:45:19 +04:00
|
|
|
(14, Inside, (14, 21)),
|
|
|
|
(20, Inside, (14, 21)),
|
|
|
|
(17, Inside, (14, 21)),
|
2021-08-06 04:32:33 +04:00
|
|
|
(14, Around, (14, 21)),
|
|
|
|
(20, Around, (14, 21)),
|
|
|
|
(17, Around, (14, 21)),
|
2021-07-03 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor in extra whitespace",
|
|
|
|
vec![
|
|
|
|
(9, Inside, (9, 9)),
|
|
|
|
(10, Inside, (10, 10)),
|
|
|
|
(11, Inside, (11, 11)),
|
2021-07-09 03:45:19 +04:00
|
|
|
(9, Around, (9, 9)),
|
|
|
|
(10, Around, (10, 10)),
|
|
|
|
(11, Around, (11, 11)),
|
2021-07-03 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
2021-08-06 04:32:33 +04:00
|
|
|
(
|
|
|
|
"cursor on word with extra whitespace",
|
|
|
|
vec![(11, Inside, (10, 14)), (11, Around, (10, 17))],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"cursor at end with extra whitespace",
|
|
|
|
vec![(28, Inside, (27, 37)), (28, Around, (24, 37))],
|
|
|
|
),
|
2021-07-03 05:07:49 +04:00
|
|
|
(
|
|
|
|
"cursor at end of doc",
|
2021-07-09 03:45:19 +04:00
|
|
|
vec![(19, Inside, (17, 20)), (19, Around, (16, 20))],
|
2021-07-03 05:07:49 +04: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;
|
2021-11-29 05:33:53 +04:00
|
|
|
// cursor is a single width selection
|
|
|
|
let range = Range::new(pos, pos + 1);
|
|
|
|
let result = textobject_word(slice, range, objtype, 1, false);
|
2021-07-03 05:07:49 +04:00
|
|
|
assert_eq!(
|
|
|
|
result,
|
|
|
|
expected_range.into(),
|
|
|
|
"\nCase failed: {:?} - {:?}",
|
|
|
|
sample,
|
|
|
|
case
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-21 18:58:54 +04:00
|
|
|
#[test]
|
|
|
|
fn test_textobject_paragraph_inside_single() {
|
|
|
|
let tests = [
|
2022-03-03 04:55:26 +04:00
|
|
|
("#[|]#", "#[|]#"),
|
|
|
|
("firs#[t|]#\n\nparagraph\n\n", "#[first\n|]#\nparagraph\n\n"),
|
2022-02-21 18:58:54 +04:00
|
|
|
(
|
2022-03-03 04:55:26 +04:00
|
|
|
"second\n\npa#[r|]#agraph\n\n",
|
|
|
|
"second\n\n#[paragraph\n|]#\n",
|
2022-02-21 18:58:54 +04:00
|
|
|
),
|
2022-03-03 04:55:26 +04:00
|
|
|
("#[f|]#irst char\n\n", "#[first char\n|]#\n"),
|
|
|
|
("last char\n#[\n|]#", "last char\n\n#[|]#"),
|
2022-02-21 18:58:54 +04:00
|
|
|
(
|
2022-03-03 04:55:26 +04:00
|
|
|
"empty to line\n#[\n|]#paragraph boundary\n\n",
|
|
|
|
"empty to line\n\n#[paragraph boundary\n|]#\n",
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"line to empty\n\n#[p|]#aragraph boundary\n\n",
|
|
|
|
"line to empty\n\n#[paragraph boundary\n|]#\n",
|
2022-02-21 18:58:54 +04:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (before, expected) in tests {
|
|
|
|
let (s, selection) = crate::test::print(before);
|
|
|
|
let text = Rope::from(s.as_str());
|
2022-03-07 17:25:26 +04:00
|
|
|
let selection = selection
|
|
|
|
.transform(|r| textobject_paragraph(text.slice(..), r, TextObject::Inside, 1));
|
2022-02-21 18:58:54 +04:00
|
|
|
let actual = crate::test::plain(&s, selection);
|
2022-04-08 19:56:50 +04:00
|
|
|
assert_eq!(actual, expected, "\nbefore: `{:?}`", before);
|
2022-02-21 18:58:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_textobject_paragraph_inside_double() {
|
|
|
|
let tests = [
|
|
|
|
(
|
2022-03-03 04:55:26 +04:00
|
|
|
"last two\n\n#[p|]#aragraph\n\nwithout whitespaces\n\n",
|
|
|
|
"last two\n\n#[paragraph\n\nwithout whitespaces\n|]#\n",
|
2022-02-21 18:58:54 +04:00
|
|
|
),
|
|
|
|
(
|
2022-03-03 04:55:26 +04:00
|
|
|
"last two\n#[\n|]#paragraph\n\nwithout whitespaces\n\n",
|
|
|
|
"last two\n\n#[paragraph\n\nwithout whitespaces\n|]#\n",
|
2022-02-21 18:58:54 +04:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (before, expected) in tests {
|
|
|
|
let (s, selection) = crate::test::print(before);
|
|
|
|
let text = Rope::from(s.as_str());
|
2022-03-07 17:25:26 +04:00
|
|
|
let selection = selection
|
|
|
|
.transform(|r| textobject_paragraph(text.slice(..), r, TextObject::Inside, 2));
|
2022-02-21 18:58:54 +04:00
|
|
|
let actual = crate::test::plain(&s, selection);
|
2022-04-08 19:56:50 +04:00
|
|
|
assert_eq!(actual, expected, "\nbefore: `{:?}`", before);
|
2022-02-21 18:58:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_textobject_paragraph_around_single() {
|
|
|
|
let tests = [
|
2022-03-03 04:55:26 +04:00
|
|
|
("#[|]#", "#[|]#"),
|
|
|
|
("firs#[t|]#\n\nparagraph\n\n", "#[first\n\n|]#paragraph\n\n"),
|
|
|
|
(
|
|
|
|
"second\n\npa#[r|]#agraph\n\n",
|
|
|
|
"second\n\n#[paragraph\n\n|]#",
|
|
|
|
),
|
|
|
|
("#[f|]#irst char\n\n", "#[first char\n\n|]#"),
|
|
|
|
("last char\n#[\n|]#", "last char\n\n#[|]#"),
|
2022-02-21 18:58:54 +04:00
|
|
|
(
|
2022-03-03 04:55:26 +04:00
|
|
|
"empty to line\n#[\n|]#paragraph boundary\n\n",
|
|
|
|
"empty to line\n\n#[paragraph boundary\n\n|]#",
|
2022-02-21 18:58:54 +04:00
|
|
|
),
|
|
|
|
(
|
2022-03-03 04:55:26 +04:00
|
|
|
"line to empty\n\n#[p|]#aragraph boundary\n\n",
|
|
|
|
"line to empty\n\n#[paragraph boundary\n\n|]#",
|
2022-02-21 18:58:54 +04:00
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (before, expected) in tests {
|
|
|
|
let (s, selection) = crate::test::print(before);
|
|
|
|
let text = Rope::from(s.as_str());
|
2022-03-07 17:25:26 +04:00
|
|
|
let selection = selection
|
|
|
|
.transform(|r| textobject_paragraph(text.slice(..), r, TextObject::Around, 1));
|
2022-02-21 18:58:54 +04:00
|
|
|
let actual = crate::test::plain(&s, selection);
|
2022-04-08 19:56:50 +04:00
|
|
|
assert_eq!(actual, expected, "\nbefore: `{:?}`", before);
|
2022-02-21 18:58:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 05:07:49 +04:00
|
|
|
#[test]
|
|
|
|
fn test_textobject_surround() {
|
2021-11-29 05:33:53 +04:00
|
|
|
// (text, [(cursor position, textobject, final range, surround char, count), ...])
|
2021-07-03 05:07:49 +04:00
|
|
|
let tests = &[
|
|
|
|
(
|
|
|
|
"simple (single) surround pairs",
|
|
|
|
vec![
|
|
|
|
(3, Inside, (3, 3), '(', 1),
|
2021-07-09 03:45:19 +04:00
|
|
|
(7, Inside, (8, 14), ')', 1),
|
|
|
|
(10, Inside, (8, 14), '(', 1),
|
|
|
|
(14, Inside, (8, 14), ')', 1),
|
2021-07-03 05:07:49 +04:00
|
|
|
(3, Around, (3, 3), '(', 1),
|
2021-07-09 03:45:19 +04:00
|
|
|
(7, Around, (7, 15), ')', 1),
|
|
|
|
(10, Around, (7, 15), '(', 1),
|
|
|
|
(14, Around, (7, 15), ')', 1),
|
2021-07-03 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"samexx 'single' surround pairs",
|
|
|
|
vec![
|
|
|
|
(3, Inside, (3, 3), '\'', 1),
|
2021-07-09 03:45:19 +04:00
|
|
|
(7, Inside, (7, 7), '\'', 1),
|
|
|
|
(10, Inside, (8, 14), '\'', 1),
|
|
|
|
(14, Inside, (14, 14), '\'', 1),
|
2021-07-03 05:07:49 +04:00
|
|
|
(3, Around, (3, 3), '\'', 1),
|
2021-07-09 03:45:19 +04:00
|
|
|
(7, Around, (7, 7), '\'', 1),
|
|
|
|
(10, Around, (7, 15), '\'', 1),
|
|
|
|
(14, Around, (14, 14), '\'', 1),
|
2021-07-03 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"(nested (surround (pairs)) 3 levels)",
|
|
|
|
vec![
|
2021-07-09 03:45:19 +04: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 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"(mixed {surround [pair] same} line)",
|
|
|
|
vec![
|
2021-07-09 03:45:19 +04: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 05:07:49 +04:00
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"(stepped (surround) pairs (should) skip)",
|
2021-07-09 03:45:19 +04:00
|
|
|
vec![(22, Inside, (1, 39), '(', 1), (22, Around, (0, 40), '(', 1)],
|
2021-07-03 05:07:49 +04:00
|
|
|
),
|
|
|
|
(
|
|
|
|
"[surround pairs{\non different]\nlines}",
|
|
|
|
vec![
|
2021-07-09 03:45:19 +04:00
|
|
|
(7, Inside, (1, 29), '[', 1),
|
|
|
|
(15, Inside, (16, 36), '{', 1),
|
|
|
|
(7, Around, (0, 30), '[', 1),
|
|
|
|
(15, Around, (15, 37), '{', 1),
|
2021-07-03 05:07:49 +04: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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|