diff options
author | Nathan Vegdahl | 2021-07-06 03:27:49 +0000 |
---|---|---|
committer | Nathan Vegdahl | 2021-07-06 03:27:49 +0000 |
commit | 85d5b399de70ff075a455ce2858549d1ed012fe3 (patch) | |
tree | 4824fcb47f39551d3b31a62931adaf0ee406c02b /helix-core/src/surround.rs | |
parent | 6e15c9b8745e9708ee5271c8701d41a8393cb038 (diff) | |
parent | 3c31f501164080998975883eb6f93c49bd8d3efb (diff) |
Merge branch 'master' into great_line_ending_and_cursor_range_cleanup
Diffstat (limited to 'helix-core/src/surround.rs')
-rw-r--r-- | helix-core/src/surround.rs | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/helix-core/src/surround.rs b/helix-core/src/surround.rs index 61981d6e..52f60cab 100644 --- a/helix-core/src/surround.rs +++ b/helix-core/src/surround.rs @@ -41,11 +41,14 @@ pub fn find_nth_pairs_pos( let (open, close) = get_pair(ch); let (open_pos, close_pos) = if open == close { - // find_nth* do not consider current character; +1/-1 to include them - ( - search::find_nth_prev(text, open, pos + 1, n, true)?, - search::find_nth_next(text, close, pos - 1, n, true)?, - ) + let prev = search::find_nth_prev(text, open, pos, n, true); + let next = search::find_nth_next(text, close, pos, n, true); + if text.char(pos) == open { + // cursor is *on* a pair + next.map(|n| (pos, n)).or_else(|| prev.map(|p| (p, pos)))? + } else { + (prev?, next?) + } } else { ( find_nth_open_pair(text, open, close, pos, n)?, @@ -198,6 +201,11 @@ mod test { assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 1), Some((10, 15))); assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 2), Some((4, 21))); assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 3), Some((0, 27))); + // cursor on the quotes + assert_eq!(find_nth_pairs_pos(slice, '\'', 10, 1), Some((10, 15))); + // this is the best we can do since opening and closing pairs are same + assert_eq!(find_nth_pairs_pos(slice, '\'', 0, 1), Some((0, 4))); + assert_eq!(find_nth_pairs_pos(slice, '\'', 27, 1), Some((21, 27))); } #[test] |