@@ -1,3 +1,5 @@
1use crate::{search, Range, Selection};
3use crate::{search, Range, Selection};
2use ropey::RopeSlice;
4use ropey::RopeSlice;
@@ -11,6 +13,27 @@ pub const PAIRS: &[(char, char)] = &[
11 ('(', ')'),
13 ('(', ')'),
16+#[derive(Debug, PartialEq)]
21+ CursorOnAmbiguousPair,
24+impl Display for Error {
25+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26+ f.write_str(match *self {
27+ Error::PairNotFound => "Surround pair not found around all cursors",
28+ Error::CursorOverlap => "Cursors overlap for a single surround pair range",
29+ Error::RangeExceedsText => "Cursor range exceeds text length",
30+ Error::CursorOnAmbiguousPair => "Cursor on ambiguous surround pair",
35+type Result<T> = std::result::Result<T, Error>;
14/// Given any char in [PAIRS], return the open and closing chars. If not found in
37/// Given any char in [PAIRS], return the open and closing chars. If not found in
15/// [PAIRS] return (ch, ch).
38/// [PAIRS] return (ch, ch).
@@ -37,31 +60,36 @@ pub fn find_nth_pairs_pos(
38 range: Range,
61 range: Range,
40−) -> Option<(usize, usize)> {
63+) -> Result<(usize, usize)> {
64+ if text.len_chars() < 2 {
65+ return Err(Error::PairNotFound);
41− if text.len_chars() < 2 || range.to() >= text.len_chars() {
67+ if range.to() >= text.len_chars() {
42− return None;
68+ return Err(Error::RangeExceedsText);
45 let (open, close) = get_pair(ch);
71 let (open, close) = get_pair(ch);
46 let pos = range.cursor(text);
72 let pos = range.cursor(text);
48− if open == close {
74+ let (open, close) = if open == close {
49 if Some(open) == text.get_char(pos) {
75 if Some(open) == text.get_char(pos) {
50 // Cursor is directly on match char. We return no match
76 // Cursor is directly on match char. We return no match
51 // because there's no way to know which side of the char
77 // because there's no way to know which side of the char
52 // we should be searching on.
78 // we should be searching on.
53− return None;
79+ return Err(Error::CursorOnAmbiguousPair);
56− search::find_nth_prev(text, open, pos, n)?,
82+ search::find_nth_prev(text, open, pos, n),
57− search::find_nth_next(text, close, pos, n)?,
83+ search::find_nth_next(text, close, pos, n),
61− find_nth_open_pair(text, open, close, pos, n)?,
87+ find_nth_open_pair(text, open, close, pos, n),
62− find_nth_close_pair(text, open, close, pos, n)?,
88+ find_nth_close_pair(text, open, close, pos, n),
92+ Option::zip(open, close).ok_or(Error::PairNotFound)
67fn find_nth_open_pair(
95fn find_nth_open_pair(
@@ -151,17 +179,17 @@ pub fn get_surround_pos(
151 selection: &Selection,
179 selection: &Selection,
152 ch: char,
180 ch: char,
153 skip: usize,
181 skip: usize,
154−) -> Option<Vec<usize>> {
182+) -> Result<Vec<usize>> {
155 let mut change_pos = Vec::new();
183 let mut change_pos = Vec::new();
157 for &range in selection {
185 for &range in selection {
158 let (open_pos, close_pos) = find_nth_pairs_pos(text, ch, range, skip)?;
186 let (open_pos, close_pos) = find_nth_pairs_pos(text, ch, range, skip)?;
159 if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) {
187 if change_pos.contains(&open_pos) || change_pos.contains(&close_pos) {
160− return None;
188+ return Err(Error::CursorOverlap);
162 change_pos.extend_from_slice(&[open_pos, close_pos]);
190 change_pos.extend_from_slice(&[open_pos, close_pos]);
164− Some(change_pos)
192+ Ok(change_pos)
167#[cfg(test)]
195#[cfg(test)]
@@ -175,7 +203,7 @@ mod test {
175 #[allow(clippy::type_complexity)]
203 #[allow(clippy::type_complexity)]
176 fn check_find_nth_pair_pos(
204 fn check_find_nth_pair_pos(
177 text: &str,
205 text: &str,
178− cases: Vec<(usize, char, usize, Option<(usize, usize)>)>,
206+ cases: Vec<(usize, char, usize, Result<(usize, usize)>)>,
180 let doc = Rope::from(text);
208 let doc = Rope::from(text);
181 let slice = doc.slice(..);
209 let slice = doc.slice(..);
@@ -196,13 +224,13 @@ mod test {
196 "some (text) here",
224 "some (text) here",
198 // cursor on [t]ext
226 // cursor on [t]ext
199− (6, '(', 1, Some((5, 10))),
227+ (6, '(', 1, Ok((5, 10))),
200− (6, ')', 1, Some((5, 10))),
228+ (6, ')', 1, Ok((5, 10))),
201 // cursor on so[m]e
229 // cursor on so[m]e
202− (2, '(', 1, None),
230+ (2, '(', 1, Err(Error::PairNotFound)),
203 // cursor on bracket itself
231 // cursor on bracket itself
204− (5, '(', 1, Some((5, 10))),
232+ (5, '(', 1, Ok((5, 10))),
205− (10, '(', 1, Some((5, 10))),
233+ (10, '(', 1, Ok((5, 10))),
@@ -213,9 +241,9 @@ mod test {
213 "(so (many (good) text) here)",
241 "(so (many (good) text) here)",
215 // cursor on go[o]d
243 // cursor on go[o]d
216− (13, '(', 1, Some((10, 15))),
244+ (13, '(', 1, Ok((10, 15))),
217− (13, '(', 2, Some((4, 21))),
245+ (13, '(', 2, Ok((4, 21))),
218− (13, '(', 3, Some((0, 27))),
246+ (13, '(', 3, Ok((0, 27))),
@@ -226,11 +254,11 @@ mod test {
226 "'so 'many 'good' text' here'",
254 "'so 'many 'good' text' here'",
228 // cursor on go[o]d
256 // cursor on go[o]d
229− (13, '\'', 1, Some((10, 15))),
257+ (13, '\'', 1, Ok((10, 15))),
230− (13, '\'', 2, Some((4, 21))),
258+ (13, '\'', 2, Ok((4, 21))),
231− (13, '\'', 3, Some((0, 27))),
259+ (13, '\'', 3, Ok((0, 27))),
232 // cursor on the quotes
260 // cursor on the quotes
233− (10, '\'', 1, None),
261+ (10, '\'', 1, Err(Error::CursorOnAmbiguousPair)),
@@ -241,8 +269,8 @@ mod test {
241 "((so)((many) good (text))(here))",
269 "((so)((many) good (text))(here))",
243 // cursor on go[o]d
271 // cursor on go[o]d
244− (15, '(', 1, Some((5, 24))),
272+ (15, '(', 1, Ok((5, 24))),
245− (15, '(', 2, Some((0, 31))),
273+ (15, '(', 2, Ok((0, 31))),
@@ -253,9 +281,9 @@ mod test {
253 "(so [many {good} text] here)",
281 "(so [many {good} text] here)",
255 // cursor on go[o]d
283 // cursor on go[o]d
256− (13, '{', 1, Some((10, 15))),
284+ (13, '{', 1, Ok((10, 15))),
257− (13, '[', 1, Some((4, 21))),
285+ (13, '[', 1, Ok((4, 21))),
258− (13, '(', 1, Some((0, 27))),
286+ (13, '(', 1, Ok((0, 27))),
@@ -285,11 +313,10 @@ mod test {
286 let selection =
314 let selection =
287 Selection::new(SmallVec::from_slice(&[Range::point(2), Range::point(9)]), 0);
315 Selection::new(SmallVec::from_slice(&[Range::point(2), Range::point(9)]), 0);
289 // cursor on s[o]me, c[h]ars
316 // cursor on s[o]me, c[h]ars
290 assert_eq!(
317 assert_eq!(
291 get_surround_pos(slice, &selection, '(', 1),
318 get_surround_pos(slice, &selection, '(', 1),
292− None // different surround chars
319+ Err(Error::PairNotFound) // different surround chars
295 let selection = Selection::new(
322 let selection = Selection::new(
@@ -299,7 +326,15 @@ mod test {
299 // cursor on [x]x, newli[n]e
326 // cursor on [x]x, newli[n]e
300 assert_eq!(
327 assert_eq!(
301 get_surround_pos(slice, &selection, '(', 1),
328 get_surround_pos(slice, &selection, '(', 1),
302− None // overlapping surround chars
329+ Err(Error::PairNotFound) // overlapping surround chars
333+ Selection::new(SmallVec::from_slice(&[Range::point(2), Range::point(3)]), 0);
334+ // cursor on s[o][m]e
336+ get_surround_pos(slice, &selection, '[', 1),
337+ Err(Error::CursorOverlap)