From 351c1e7e5533a05a8e4da20d1e5227c356098b7b Mon Sep 17 00:00:00 2001 From: Gokul Soumya Date: Sat, 3 Jul 2021 15:46:56 +0530 Subject: [PATCH] Fix surround bug when cursor on same pair For example when the cursor is _on_ the `'` in `'word'`, the cursor wouldn't move because the search for a matching pair started _from_ the position of the cursor and simply found itself. --- helix-core/src/surround.rs | 18 +++++++++++++----- helix-core/src/textobject.rs | 9 ++++----- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/helix-core/src/surround.rs b/helix-core/src/surround.rs index 61981d6e..841288c2 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 { + // curosr 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] diff --git a/helix-core/src/textobject.rs b/helix-core/src/textobject.rs index d29eb03c..fbf66256 100644 --- a/helix-core/src/textobject.rs +++ b/helix-core/src/textobject.rs @@ -247,14 +247,13 @@ mod test { "samexx 'single' surround pairs", vec![ (3, Inside, (3, 3), '\'', 1), - // FIXME: surround doesn't work when *on* same chars pair - // (7, Inner, (8, 13), '\'', 1), + (7, Inside, (8, 13), '\'', 1), (10, Inside, (8, 13), '\'', 1), - // (14, Inner, (8, 13), '\'', 1), + (14, Inside, (8, 13), '\'', 1), (3, Around, (3, 3), '\'', 1), - // (7, Around, (7, 14), '\'', 1), + (7, Around, (7, 14), '\'', 1), (10, Around, (7, 14), '\'', 1), - // (14, Around, (7, 14), '\'', 1), + (14, Around, (7, 14), '\'', 1), ], ), (