on master help sign in
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.

commit351c1e7e5533a05a8e4da20d1e5227c356098b7b authorGokul Soumya <gokulps15@gmail.com> dateSat 3 Jul 2021, 15:46 (5 years ago) parent37f0b9e
2 files changed+17−10
helix-core/src/surround.rs+13−5 helix-core/src/textobject.rs+4−5
helix-core/src/surround.rs+13−5
@@ -41,11 +41,14 @@ pub fn find_nth_pairs_pos(
4141 let (open, close) = get_pair(ch);
4242
4343 let (open_pos, close_pos) = if open == close {
44− // find_nth* do not consider current character; +1/-1 to include them
45− (
46− search::find_nth_prev(text, open, pos + 1, n, true)?,
47− search::find_nth_next(text, close, pos - 1, n, true)?,
48− )
44+ let prev = search::find_nth_prev(text, open, pos, n, true);
45+ let next = search::find_nth_next(text, close, pos, n, true);
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)))?
49+ } else {
50+ (prev?, next?)
51+ }
4952 } else {
5053 (
5154 find_nth_open_pair(text, open, close, pos, n)?,
@@ -198,6 +201,11 @@ mod test {
198201 assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 1), Some((10, 15)));
199202 assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 2), Some((4, 21)));
200203 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)));
201209 }
202210
203211 #[test]
helix-core/src/textobject.rs+4−5
@@ -247,14 +247,13 @@ mod test {
247247 "samexx 'single' surround pairs",
248248 vec![
249249 (3, Inside, (3, 3), '\'', 1),
250− // FIXME: surround doesn't work when *on* same chars pair
251− // (7, Inner, (8, 13), '\'', 1),
250+ (7, Inside, (8, 13), '\'', 1),
252251 (10, Inside, (8, 13), '\'', 1),
253− // (14, Inner, (8, 13), '\'', 1),
252+ (14, Inside, (8, 13), '\'', 1),
254253 (3, Around, (3, 3), '\'', 1),
255− // (7, Around, (7, 14), '\'', 1),
254+ (7, Around, (7, 14), '\'', 1),
256255 (10, Around, (7, 14), '\'', 1),
257− // (14, Around, (7, 14), '\'', 1),
256+ (14, Around, (7, 14), '\'', 1),
258257 ],
259258 ),
260259 (