@@ -41,11 +41,14 @@ pub fn find_nth_pairs_pos(
41 let (open, close) = get_pair(ch);
41 let (open, close) = get_pair(ch);
43 let (open_pos, close_pos) = if open == close {
43 let (open_pos, close_pos) = if open == close {
44− // find_nth* do not consider current character; +1/-1 to include them
46− search::find_nth_prev(text, open, pos + 1, n, true)?,
44+ let prev = search::find_nth_prev(text, open, pos, n, true);
47− search::find_nth_next(text, close, pos - 1, n, true)?,
45+ let next = search::find_nth_next(text, close, pos, n, true);
48− )
46+ if text.char(pos) == open {
47+ // curosr is *on* a pair
48+ next.map(|n| (pos, n)).or_else(|| prev.map(|p| (p, pos)))?
51 find_nth_open_pair(text, open, close, pos, n)?,
54 find_nth_open_pair(text, open, close, pos, n)?,
@@ -198,6 +201,11 @@ mod test {
198 assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 1), Some((10, 15)));
201 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)));
202 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)));
203 assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 3), Some((0, 27)));
204+ // cursor on the quotes
205+ assert_eq!(find_nth_pairs_pos(slice, '\'', 10, 1), Some((10, 15)));
206+ // this is the best we can do since opening and closing pairs are same
207+ assert_eq!(find_nth_pairs_pos(slice, '\'', 0, 1), Some((0, 4)));
208+ assert_eq!(find_nth_pairs_pos(slice, '\'', 27, 1), Some((21, 27)));