@@ -39,13 +39,101 @@ pub fn find_nth_pairs_pos(
40) -> Option<(usize, usize)> {
40) -> Option<(usize, usize)> {
41 let (open, close) = get_pair(ch);
41 let (open, close) = get_pair(ch);
43+ let (open_pos, close_pos) = if open == close {
42− // find_nth* do not consider current character; +1/-1 to include them
44+ // find_nth* do not consider current character; +1/-1 to include them
43− let open_pos = search::find_nth_prev(text, open, pos + 1, n, true)?;
46+ search::find_nth_prev(text, open, pos + 1, n, true)?,
44− let close_pos = search::find_nth_next(text, close, pos - 1, n, true)?;
47+ search::find_nth_next(text, close, pos - 1, n, true)?,
51+ find_nth_open_pair(text, open, close, pos, n)?,
52+ find_nth_close_pair(text, open, close, pos, n)?,
46 Some((open_pos, close_pos))
56 Some((open_pos, close_pos))
59+fn find_nth_open_pair(
66+ let mut chars = text.chars_at(pos + 1);
68+ // Adjusts pos for the first iteration, and handles the case of the
69+ // cursor being *on* the close character which will get falsely stepped over
70+ // if not skipped here
71+ if chars.prev()? == open {
76+ let mut step_over: usize = 0;
79+ let c = chars.prev()?;
80+ pos = pos.saturating_sub(1);
82+ // ignore other surround pairs that are enclosed *within* our search scope
85+ } else if c == open {
90+ step_over = step_over.saturating_sub(1);
98+fn find_nth_close_pair(
105+ if pos >= text.len_chars() {
109+ let mut chars = text.chars_at(pos);
111+ if chars.next()? == close {
116+ let mut step_over: usize = 0;
119+ let c = chars.next()?;
124+ } else if c == close {
129+ step_over = step_over.saturating_sub(1);
49/// Find position of surround characters around every cursor. Returns None
137/// Find position of surround characters around every cursor. Returns None
50/// if any positions overlap. Note that the positions are in a flat Vec.
138/// if any positions overlap. Note that the positions are in a flat Vec.
51/// Use get_surround_pos().chunks(2) to get matching pairs of surround positions.
139/// Use get_surround_pos().chunks(2) to get matching pairs of surround positions.
@@ -101,6 +189,27 @@ mod test {
101 assert_eq!(find_nth_pairs_pos(slice, '(', 13, 3), Some((0, 27)));
189 assert_eq!(find_nth_pairs_pos(slice, '(', 13, 3), Some((0, 27)));
193+ fn test_find_nth_pairs_pos_same() {
194+ let doc = Rope::from("'so 'many 'good' text' here'");
195+ let slice = doc.slice(..);
198+ assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 1), Some((10, 15)));
199+ assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 2), Some((4, 21)));
200+ assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 3), Some((0, 27)));
204+ fn test_find_nth_pairs_pos_step() {
205+ let doc = Rope::from("((so)((many) good (text))(here))");
206+ let slice = doc.slice(..);
209+ assert_eq!(find_nth_pairs_pos(slice, '(', 15, 1), Some((5, 24)));
210+ assert_eq!(find_nth_pairs_pos(slice, '(', 15, 2), Some((0, 31)));
105 fn test_find_nth_pairs_pos_mixed() {
214 fn test_find_nth_pairs_pos_mixed() {
106 let doc = Rope::from("(so [many {good} text] here)");
215 let doc = Rope::from("(so [many {good} text] here)");