2022-02-28 12:56:39 +04:00
|
|
|
|
use std::fmt::Display;
|
|
|
|
|
|
2023-02-10 23:52:57 +04:00
|
|
|
|
use crate::{movement::Direction, search, Range, Selection};
|
2021-06-19 21:25:50 +04:00
|
|
|
|
use ropey::RopeSlice;
|
|
|
|
|
|
2021-06-21 20:49:05 +04:00
|
|
|
|
pub const PAIRS: &[(char, char)] = &[
|
|
|
|
|
('(', ')'),
|
|
|
|
|
('[', ']'),
|
|
|
|
|
('{', '}'),
|
|
|
|
|
('<', '>'),
|
|
|
|
|
('«', '»'),
|
|
|
|
|
('「', '」'),
|
|
|
|
|
('(', ')'),
|
|
|
|
|
];
|
2021-06-19 21:25:50 +04:00
|
|
|
|
|
2022-11-04 16:01:17 +04:00
|
|
|
|
#[derive(Debug, PartialEq, Eq)]
|
2022-02-28 12:56:39 +04:00
|
|
|
|
pub enum Error {
|
|
|
|
|
PairNotFound,
|
|
|
|
|
CursorOverlap,
|
|
|
|
|
RangeExceedsText,
|
|
|
|
|
CursorOnAmbiguousPair,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Display for Error {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
f.write_str(match *self {
|
|
|
|
|
Error::PairNotFound => "Surround pair not found around all cursors",
|
|
|
|
|
Error::CursorOverlap => "Cursors overlap for a single surround pair range",
|
|
|
|
|
Error::RangeExceedsText => "Cursor range exceeds text length",
|
|
|
|
|
Error::CursorOnAmbiguousPair => "Cursor on ambiguous surround pair",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
2021-06-19 21:25:50 +04:00
|
|
|
|
/// Given any char in [PAIRS], return the open and closing chars. If not found in
|
|
|
|
|
/// [PAIRS] return (ch, ch).
|
2021-06-21 22:33:05 +04:00
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use helix_core::surround::get_pair;
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(get_pair('['), ('[', ']'));
|
|
|
|
|
/// assert_eq!(get_pair('}'), ('{', '}'));
|
|
|
|
|
/// assert_eq!(get_pair('"'), ('"', '"'));
|
|
|
|
|
/// ```
|
2021-06-19 21:25:50 +04:00
|
|
|
|
pub fn get_pair(ch: char) -> (char, char) {
|
|
|
|
|
PAIRS
|
|
|
|
|
.iter()
|
|
|
|
|
.find(|(open, close)| *open == ch || *close == ch)
|
|
|
|
|
.copied()
|
|
|
|
|
.unwrap_or((ch, ch))
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-03 20:01:43 +04:00
|
|
|
|
pub fn find_nth_closest_pairs_pos(
|
|
|
|
|
text: RopeSlice,
|
|
|
|
|
range: Range,
|
2023-02-10 23:52:57 +04:00
|
|
|
|
mut skip: usize,
|
2022-04-03 20:01:43 +04:00
|
|
|
|
) -> Result<(usize, usize)> {
|
|
|
|
|
let is_open_pair = |ch| PAIRS.iter().any(|(open, _)| *open == ch);
|
|
|
|
|
let is_close_pair = |ch| PAIRS.iter().any(|(_, close)| *close == ch);
|
|
|
|
|
|
|
|
|
|
let mut stack = Vec::with_capacity(2);
|
2023-02-10 23:52:57 +04:00
|
|
|
|
let pos = range.from();
|
|
|
|
|
let mut close_pos = pos.saturating_sub(1);
|
2022-04-03 20:01:43 +04:00
|
|
|
|
|
|
|
|
|
for ch in text.chars_at(pos) {
|
2023-02-10 23:52:57 +04:00
|
|
|
|
close_pos += 1;
|
|
|
|
|
|
2022-04-03 20:01:43 +04:00
|
|
|
|
if is_open_pair(ch) {
|
|
|
|
|
// Track open pairs encountered so that we can step over
|
2022-06-01 21:01:37 +04:00
|
|
|
|
// the corresponding close pairs that will come up further
|
2022-04-03 20:01:43 +04:00
|
|
|
|
// down the loop. We want to find a lone close pair whose
|
|
|
|
|
// open pair is before the cursor position.
|
|
|
|
|
stack.push(ch);
|
|
|
|
|
continue;
|
2023-02-10 23:52:57 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !is_close_pair(ch) {
|
|
|
|
|
// We don't care if this character isn't a brace pair item,
|
|
|
|
|
// so short circuit here.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let (open, close) = get_pair(ch);
|
|
|
|
|
|
|
|
|
|
if stack.last() == Some(&open) {
|
|
|
|
|
// If we are encountering the closing pair for an opener
|
|
|
|
|
// we just found while traversing, then its inside the
|
|
|
|
|
// selection and should be skipped over.
|
|
|
|
|
stack.pop();
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match find_nth_open_pair(text, open, close, close_pos, 1) {
|
|
|
|
|
// Before we accept this pair, we want to ensure that the
|
|
|
|
|
// pair encloses the range rather than just the cursor.
|
|
|
|
|
Some(open_pos)
|
|
|
|
|
if open_pos <= pos.saturating_add(1)
|
|
|
|
|
&& close_pos >= range.to().saturating_sub(1) =>
|
|
|
|
|
{
|
|
|
|
|
// Since we have special conditions for when to
|
|
|
|
|
// accept, we can't just pass the skip parameter on
|
|
|
|
|
// through to the find_nth_*_pair methods, so we
|
|
|
|
|
// track skips manually here.
|
|
|
|
|
if skip > 1 {
|
|
|
|
|
skip -= 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return match range.direction() {
|
|
|
|
|
Direction::Forward => Ok((open_pos, close_pos)),
|
|
|
|
|
Direction::Backward => Ok((close_pos, open_pos)),
|
|
|
|
|
};
|
2022-04-03 20:01:43 +04:00
|
|
|
|
}
|
2023-02-10 23:52:57 +04:00
|
|
|
|
_ => continue,
|
2022-04-03 20:01:43 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Err(Error::PairNotFound)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 21:25:50 +04:00
|
|
|
|
/// Find the position of surround pairs of `ch` which can be either a closing
|
|
|
|
|
/// or opening pair. `n` will skip n - 1 pairs (eg. n=2 will discard (only)
|
|
|
|
|
/// the first pair found and keep looking)
|
|
|
|
|
pub fn find_nth_pairs_pos(
|
|
|
|
|
text: RopeSlice,
|
|
|
|
|
ch: char,
|
2021-11-29 05:33:53 +04:00
|
|
|
|
range: Range,
|
2021-06-19 21:25:50 +04:00
|
|
|
|
n: usize,
|
2022-02-28 12:56:39 +04:00
|
|
|
|
) -> Result<(usize, usize)> {
|
|
|
|
|
if text.len_chars() < 2 {
|
|
|
|
|
return Err(Error::PairNotFound);
|
|
|
|
|
}
|
|
|
|
|
if range.to() >= text.len_chars() {
|
|
|
|
|
return Err(Error::RangeExceedsText);
|
2021-07-09 03:45:19 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-29 05:33:53 +04:00
|
|
|
|
let (open, close) = get_pair(ch);
|
|
|
|
|
let pos = range.cursor(text);
|
|
|
|
|
|
2022-02-28 12:56:39 +04:00
|
|
|
|
let (open, close) = if open == close {
|
2021-07-09 03:45:19 +04:00
|
|
|
|
if Some(open) == text.get_char(pos) {
|
2021-11-29 05:33:53 +04:00
|
|
|
|
// Cursor is directly on match char. We return no match
|
|
|
|
|
// because there's no way to know which side of the char
|
|
|
|
|
// we should be searching on.
|
2022-02-28 12:56:39 +04:00
|
|
|
|
return Err(Error::CursorOnAmbiguousPair);
|
2021-07-03 14:16:56 +04:00
|
|
|
|
}
|
2022-02-28 12:56:39 +04:00
|
|
|
|
(
|
|
|
|
|
search::find_nth_prev(text, open, pos, n),
|
|
|
|
|
search::find_nth_next(text, close, pos, n),
|
|
|
|
|
)
|
2021-06-23 22:50:19 +04:00
|
|
|
|
} else {
|
2022-02-28 12:56:39 +04:00
|
|
|
|
(
|
|
|
|
|
find_nth_open_pair(text, open, close, pos, n),
|
|
|
|
|
find_nth_close_pair(text, open, close, pos, n),
|
|
|
|
|
)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Option::zip(open, close).ok_or(Error::PairNotFound)
|
2021-06-19 21:25:50 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-06-23 22:50:19 +04:00
|
|
|
|
fn find_nth_open_pair(
|
|
|
|
|
text: RopeSlice,
|
|
|
|
|
open: char,
|
|
|
|
|
close: char,
|
|
|
|
|
mut pos: usize,
|
|
|
|
|
n: usize,
|
|
|
|
|
) -> Option<usize> {
|
|
|
|
|
let mut chars = text.chars_at(pos + 1);
|
|
|
|
|
|
|
|
|
|
// Adjusts pos for the first iteration, and handles the case of the
|
|
|
|
|
// cursor being *on* the close character which will get falsely stepped over
|
|
|
|
|
// if not skipped here
|
|
|
|
|
if chars.prev()? == open {
|
|
|
|
|
return Some(pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _ in 0..n {
|
|
|
|
|
let mut step_over: usize = 0;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
let c = chars.prev()?;
|
|
|
|
|
pos = pos.saturating_sub(1);
|
|
|
|
|
|
|
|
|
|
// ignore other surround pairs that are enclosed *within* our search scope
|
|
|
|
|
if c == close {
|
|
|
|
|
step_over += 1;
|
|
|
|
|
} else if c == open {
|
|
|
|
|
if step_over == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
step_over = step_over.saturating_sub(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(pos)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn find_nth_close_pair(
|
|
|
|
|
text: RopeSlice,
|
|
|
|
|
open: char,
|
|
|
|
|
close: char,
|
|
|
|
|
mut pos: usize,
|
|
|
|
|
n: usize,
|
|
|
|
|
) -> Option<usize> {
|
|
|
|
|
if pos >= text.len_chars() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut chars = text.chars_at(pos);
|
|
|
|
|
|
|
|
|
|
if chars.next()? == close {
|
|
|
|
|
return Some(pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _ in 0..n {
|
|
|
|
|
let mut step_over: usize = 0;
|
|
|
|
|
|
|
|
|
|
loop {
|
|
|
|
|
let c = chars.next()?;
|
|
|
|
|
pos += 1;
|
|
|
|
|
|
|
|
|
|
if c == open {
|
|
|
|
|
step_over += 1;
|
|
|
|
|
} else if c == close {
|
|
|
|
|
if step_over == 0 {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
step_over = step_over.saturating_sub(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Some(pos)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-19 21:25:50 +04:00
|
|
|
|
/// Find position of surround characters around every cursor. Returns None
|
|
|
|
|
/// if any positions overlap. Note that the positions are in a flat Vec.
|
|
|
|
|
/// Use get_surround_pos().chunks(2) to get matching pairs of surround positions.
|
2022-04-03 20:30:26 +04:00
|
|
|
|
/// `ch` can be either closing or opening pair. If `ch` is None, surround pairs
|
|
|
|
|
/// are automatically detected around each cursor (note that this may result
|
|
|
|
|
/// in them selecting different surround characters for each selection).
|
2021-06-19 21:25:50 +04:00
|
|
|
|
pub fn get_surround_pos(
|
|
|
|
|
text: RopeSlice,
|
|
|
|
|
selection: &Selection,
|
2022-04-03 20:30:26 +04:00
|
|
|
|
ch: Option<char>,
|
2021-06-19 21:25:50 +04:00
|
|
|
|
skip: usize,
|
2022-02-28 12:56:39 +04:00
|
|
|
|
) -> Result<Vec<usize>> {
|
2021-06-19 21:25:50 +04:00
|
|
|
|
let mut change_pos = Vec::new();
|
|
|
|
|
|
2021-11-29 05:33:53 +04:00
|
|
|
|
for &range in selection {
|
2022-04-03 20:30:26 +04:00
|
|
|
|
let (open_pos, close_pos) = match ch {
|
|
|
|
|
Some(ch) => find_nth_pairs_pos(text, ch, range, skip)?,
|
|
|
|
|
None => find_nth_closest_pairs_pos(text, range, skip)?,
|
|
|
|
|
};
|
2021-06-21 22:33:05 +04:00
|
|
|
|
if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) {
|
2022-02-28 12:56:39 +04:00
|
|
|
|
return Err(Error::CursorOverlap);
|
2021-06-19 21:25:50 +04:00
|
|
|
|
}
|
2021-06-21 22:33:05 +04:00
|
|
|
|
change_pos.extend_from_slice(&[open_pos, close_pos]);
|
2021-06-19 21:25:50 +04:00
|
|
|
|
}
|
2022-02-28 12:56:39 +04:00
|
|
|
|
Ok(change_pos)
|
2021-06-19 21:25:50 +04:00
|
|
|
|
}
|
2021-06-21 22:33:05 +04:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use super::*;
|
|
|
|
|
use crate::Range;
|
|
|
|
|
|
|
|
|
|
use ropey::Rope;
|
|
|
|
|
use smallvec::SmallVec;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_get_surround_pos() {
|
2023-03-08 06:10:55 +04:00
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
let (doc, selection, expectations) =
|
|
|
|
|
rope_with_selections_and_expectations(
|
|
|
|
|
"(some) (chars)\n(newline)",
|
|
|
|
|
"_ ^ _ _ ^ _\n_ ^ _"
|
|
|
|
|
);
|
2021-06-21 22:33:05 +04:00
|
|
|
|
|
|
|
|
|
assert_eq!(
|
2023-03-08 06:10:55 +04:00
|
|
|
|
get_surround_pos(doc.slice(..), &selection, Some('('), 1).unwrap(),
|
|
|
|
|
expectations
|
2021-06-21 22:33:05 +04:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2023-03-08 06:10:55 +04:00
|
|
|
|
fn test_get_surround_pos_bail_different_surround_chars() {
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
let (doc, selection, _) =
|
|
|
|
|
rope_with_selections_and_expectations(
|
|
|
|
|
"[some]\n(chars)xx\n(newline)",
|
|
|
|
|
" ^ \n ^ \n "
|
|
|
|
|
);
|
2021-06-21 22:33:05 +04:00
|
|
|
|
|
|
|
|
|
assert_eq!(
|
2023-03-08 06:10:55 +04:00
|
|
|
|
get_surround_pos(doc.slice(..), &selection, Some('('), 1),
|
|
|
|
|
Err(Error::PairNotFound)
|
2021-06-21 22:33:05 +04:00
|
|
|
|
);
|
2023-03-08 06:10:55 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_get_surround_pos_bail_overlapping_surround_chars() {
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
let (doc, selection, _) =
|
|
|
|
|
rope_with_selections_and_expectations(
|
|
|
|
|
"[some]\n(chars)xx\n(newline)",
|
|
|
|
|
" \n ^ \n ^ "
|
|
|
|
|
);
|
2021-06-21 22:33:05 +04:00
|
|
|
|
|
|
|
|
|
assert_eq!(
|
2023-03-08 06:10:55 +04:00
|
|
|
|
get_surround_pos(doc.slice(..), &selection, Some('('), 1),
|
2022-02-28 12:56:39 +04:00
|
|
|
|
Err(Error::PairNotFound) // overlapping surround chars
|
|
|
|
|
);
|
2023-03-08 06:10:55 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_get_surround_pos_bail_cursor_overlap() {
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
let (doc, selection, _) =
|
|
|
|
|
rope_with_selections_and_expectations(
|
|
|
|
|
"[some]\n(chars)xx\n(newline)",
|
|
|
|
|
" ^^ \n \n "
|
|
|
|
|
);
|
2022-02-28 12:56:39 +04:00
|
|
|
|
|
|
|
|
|
assert_eq!(
|
2023-03-08 06:10:55 +04:00
|
|
|
|
get_surround_pos(doc.slice(..), &selection, Some('['), 1),
|
2022-02-28 12:56:39 +04:00
|
|
|
|
Err(Error::CursorOverlap)
|
2021-06-21 22:33:05 +04:00
|
|
|
|
);
|
|
|
|
|
}
|
2023-03-08 06:10:55 +04:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_find_nth_pairs_pos_quote_success() {
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
let (doc, selection, expectations) =
|
|
|
|
|
rope_with_selections_and_expectations(
|
|
|
|
|
"some 'quoted text' on this 'line'\n'and this one'",
|
|
|
|
|
" _ ^ _ \n "
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(2, expectations.len());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), 1)
|
|
|
|
|
.expect("find should succeed"),
|
|
|
|
|
(expectations[0], expectations[1])
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_find_nth_pairs_pos_nested_quote_success() {
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
let (doc, selection, expectations) =
|
|
|
|
|
rope_with_selections_and_expectations(
|
|
|
|
|
"some 'nested 'quoted' text' on this 'line'\n'and this one'",
|
|
|
|
|
" _ ^ _ \n "
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(2, expectations.len());
|
|
|
|
|
assert_eq!(
|
|
|
|
|
find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), 2)
|
|
|
|
|
.expect("find should succeed"),
|
|
|
|
|
(expectations[0], expectations[1])
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_find_nth_pairs_pos_inside_quote_ambiguous() {
|
|
|
|
|
#[rustfmt::skip]
|
|
|
|
|
let (doc, selection, _) =
|
|
|
|
|
rope_with_selections_and_expectations(
|
|
|
|
|
"some 'nested 'quoted' text' on this 'line'\n'and this one'",
|
|
|
|
|
" ^ \n "
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
|
find_nth_pairs_pos(doc.slice(..), '\'', selection.primary(), 1),
|
|
|
|
|
Err(Error::CursorOnAmbiguousPair)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create a Rope and a matching Selection using a specification language.
|
|
|
|
|
// ^ is a single-point selection.
|
|
|
|
|
// _ is an expected index. These are returned as a Vec<usize> for use in assertions.
|
|
|
|
|
fn rope_with_selections_and_expectations(
|
|
|
|
|
text: &str,
|
|
|
|
|
spec: &str,
|
|
|
|
|
) -> (Rope, Selection, Vec<usize>) {
|
|
|
|
|
if text.len() != spec.len() {
|
|
|
|
|
panic!("specification must match text length -- are newlines aligned?");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let rope = Rope::from(text);
|
|
|
|
|
|
|
|
|
|
let selections: SmallVec<[Range; 1]> = spec
|
|
|
|
|
.match_indices('^')
|
|
|
|
|
.map(|(i, _)| Range::point(i))
|
|
|
|
|
.collect();
|
|
|
|
|
|
2023-05-11 09:44:52 +04:00
|
|
|
|
let expectations: Vec<usize> = spec.match_indices('_').map(|(i, _)| i).collect();
|
2023-03-08 06:10:55 +04:00
|
|
|
|
|
|
|
|
|
(rope, Selection::new(selections, 0), expectations)
|
|
|
|
|
}
|
2021-06-21 22:33:05 +04:00
|
|
|
|
}
|