on master help sign in
Fix surround bug when cursor on same pair
351c1e7Gokul Soumya
helix-core/src/surround.rs+13−5
@@ -41,11 +41,14 @@ pub fn find_nth_pairs_pos(
41 let (open, close) = get_pair(ch);
41 let (open, close) = get_pair(ch);
42
42
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
45− (
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)))?
49+ } else {
50+ (prev?, next?)
51+ }
49 } else {
52 } else {
50 (
53 (
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)));
201 }
209 }
202
210
203 #[test]
211 #[test]
helix-core/src/textobject.rs+4−5
@@ -247,14 +247,13 @@ mod test {
247 "samexx 'single' surround pairs",
247 "samexx 'single' surround pairs",
248 vec![
248 vec![
249 (3, Inside, (3, 3), '\'', 1),
249 (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),
252 (10, Inside, (8, 13), '\'', 1),
251 (10, Inside, (8, 13), '\'', 1),
253− // (14, Inner, (8, 13), '\'', 1),
252+ (14, Inside, (8, 13), '\'', 1),
254 (3, Around, (3, 3), '\'', 1),
253 (3, Around, (3, 3), '\'', 1),
255− // (7, Around, (7, 14), '\'', 1),
254+ (7, Around, (7, 14), '\'', 1),
256 (10, Around, (7, 14), '\'', 1),
255 (10, Around, (7, 14), '\'', 1),
257− // (14, Around, (7, 14), '\'', 1),
256+ (14, Around, (7, 14), '\'', 1),
258 ],
257 ],
259 ),
258 ),
260 (
259 (