on master help sign in
Fix buggy surround behavior from #376.
c9cbc34Nathan Vegdahl
helix-core/src/surround.rs+22−23
@@ -1,4 +1,3 @@
1−use crate::graphemes::next_grapheme_boundary;
2use crate::{search, Selection};
1use crate::{search, Selection};
3use ropey::RopeSlice;
2use ropey::RopeSlice;
4
3
@@ -49,9 +48,9 @@ pub fn find_nth_pairs_pos(
49 if Some(open) == text.get_char(pos) {
48 if Some(open) == text.get_char(pos) {
50 // Special case: cursor is directly on a matching char.
49 // Special case: cursor is directly on a matching char.
51 match pos {
50 match pos {
52− 0 => Some((pos, search::find_nth_next(text, close, pos + 1, n)? + 1)),
51+ 0 => Some((pos, search::find_nth_next(text, close, pos + 1, n)?)),
53 _ if (pos + 1) == text.len_chars() => {
52 _ if (pos + 1) == text.len_chars() => {
54− Some((search::find_nth_prev(text, open, pos, n)?, text.len_chars()))
53+ Some((search::find_nth_prev(text, open, pos, n)?, pos))
55 }
54 }
56 // We return no match because there's no way to know which
55 // We return no match because there's no way to know which
57 // side of the char we should be searching on.
56 // side of the char we should be searching on.
@@ -60,13 +59,13 @@ pub fn find_nth_pairs_pos(
60 } else {
59 } else {
61 Some((
60 Some((
62 search::find_nth_prev(text, open, pos, n)?,
61 search::find_nth_prev(text, open, pos, n)?,
63− search::find_nth_next(text, close, pos, n)? + 1,
62+ search::find_nth_next(text, close, pos, n)?,
64 ))
63 ))
65 }
64 }
66 } else {
65 } else {
67 Some((
66 Some((
68 find_nth_open_pair(text, open, close, pos, n)?,
67 find_nth_open_pair(text, open, close, pos, n)?,
69− next_grapheme_boundary(text, find_nth_close_pair(text, open, close, pos, n)?),
68+ find_nth_close_pair(text, open, close, pos, n)?,
70 ))
69 ))
71 }
70 }
72}
71}
@@ -185,13 +184,13 @@ mod test {
185 let slice = doc.slice(..);
184 let slice = doc.slice(..);
186
185
187 // cursor on [t]ext
186 // cursor on [t]ext
188− assert_eq!(find_nth_pairs_pos(slice, '(', 6, 1), Some((5, 11)));
187+ assert_eq!(find_nth_pairs_pos(slice, '(', 6, 1), Some((5, 10)));
189− assert_eq!(find_nth_pairs_pos(slice, ')', 6, 1), Some((5, 11)));
188+ assert_eq!(find_nth_pairs_pos(slice, ')', 6, 1), Some((5, 10)));
190 // cursor on so[m]e
189 // cursor on so[m]e
191 assert_eq!(find_nth_pairs_pos(slice, '(', 2, 1), None);
190 assert_eq!(find_nth_pairs_pos(slice, '(', 2, 1), None);
192 // cursor on bracket itself
191 // cursor on bracket itself
193− assert_eq!(find_nth_pairs_pos(slice, '(', 5, 1), Some((5, 11)));
192+ assert_eq!(find_nth_pairs_pos(slice, '(', 5, 1), Some((5, 10)));
194− assert_eq!(find_nth_pairs_pos(slice, '(', 10, 1), Some((5, 11)));
193+ assert_eq!(find_nth_pairs_pos(slice, '(', 10, 1), Some((5, 10)));
195 }
194 }
196
195
197 #[test]
196 #[test]
@@ -200,9 +199,9 @@ mod test {
200 let slice = doc.slice(..);
199 let slice = doc.slice(..);
201
200
202 // cursor on go[o]d
201 // cursor on go[o]d
203− assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((10, 16)));
202+ assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((10, 15)));
204− assert_eq!(find_nth_pairs_pos(slice, '(', 13, 2), Some((4, 22)));
203+ assert_eq!(find_nth_pairs_pos(slice, '(', 13, 2), Some((4, 21)));
205− assert_eq!(find_nth_pairs_pos(slice, '(', 13, 3), Some((0, 28)));
204+ assert_eq!(find_nth_pairs_pos(slice, '(', 13, 3), Some((0, 27)));
206 }
205 }
207
206
208 #[test]
207 #[test]
@@ -211,14 +210,14 @@ mod test {
211 let slice = doc.slice(..);
210 let slice = doc.slice(..);
212
211
213 // cursor on go[o]d
212 // cursor on go[o]d
214− assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 1), Some((10, 16)));
213+ assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 1), Some((10, 15)));
215− assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 2), Some((4, 22)));
214+ assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 2), Some((4, 21)));
216− assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 3), Some((0, 28)));
215+ assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 3), Some((0, 27)));
217 // cursor on the quotes
216 // cursor on the quotes
218 assert_eq!(find_nth_pairs_pos(slice, '\'', 10, 1), None);
217 assert_eq!(find_nth_pairs_pos(slice, '\'', 10, 1), None);
219 // this is the best we can do since opening and closing pairs are same
218 // this is the best we can do since opening and closing pairs are same
220− assert_eq!(find_nth_pairs_pos(slice, '\'', 0, 1), Some((0, 5)));
219+ assert_eq!(find_nth_pairs_pos(slice, '\'', 0, 1), Some((0, 4)));
221− assert_eq!(find_nth_pairs_pos(slice, '\'', 27, 1), Some((21, 28)));
220+ assert_eq!(find_nth_pairs_pos(slice, '\'', 27, 1), Some((21, 27)));
222 }
221 }
223
222
224 #[test]
223 #[test]
@@ -227,8 +226,8 @@ mod test {
227 let slice = doc.slice(..);
226 let slice = doc.slice(..);
228
227
229 // cursor on go[o]d
228 // cursor on go[o]d
230− assert_eq!(find_nth_pairs_pos(slice, '(', 15, 1), Some((5, 25)));
229+ assert_eq!(find_nth_pairs_pos(slice, '(', 15, 1), Some((5, 24)));
231− assert_eq!(find_nth_pairs_pos(slice, '(', 15, 2), Some((0, 32)));
230+ assert_eq!(find_nth_pairs_pos(slice, '(', 15, 2), Some((0, 31)));
232 }
231 }
233
232
234 #[test]
233 #[test]
@@ -237,9 +236,9 @@ mod test {
237 let slice = doc.slice(..);
236 let slice = doc.slice(..);
238
237
239 // cursor on go[o]d
238 // cursor on go[o]d
240− assert_eq!(find_nth_pairs_pos(slice, '{', 13, 1), Some((10, 16)));
239+ assert_eq!(find_nth_pairs_pos(slice, '{', 13, 1), Some((10, 15)));
241− assert_eq!(find_nth_pairs_pos(slice, '[', 13, 1), Some((4, 22)));
240+ assert_eq!(find_nth_pairs_pos(slice, '[', 13, 1), Some((4, 21)));
242− assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((0, 28)));
241+ assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((0, 27)));
243 }
242 }
244
243
245 #[test]
244 #[test]
@@ -256,7 +255,7 @@ mod test {
256 get_surround_pos(slice, &selection, '(', 1)
255 get_surround_pos(slice, &selection, '(', 1)
257 .unwrap()
256 .unwrap()
258 .as_slice(),
257 .as_slice(),
259− &[0, 6, 7, 14, 15, 24]
258+ &[0, 5, 7, 13, 15, 23]
260 );
259 );
261 }
260 }
262
261
helix-core/src/textobject.rs+2−5
@@ -98,11 +98,8 @@ pub fn textobject_surround(
98) -> Range {
98) -> Range {
99 surround::find_nth_pairs_pos(slice, ch, range.head, count)
99 surround::find_nth_pairs_pos(slice, ch, range.head, count)
100 .map(|(anchor, head)| match textobject {
100 .map(|(anchor, head)| match textobject {
101− TextObject::Inside => Range::new(
102− next_grapheme_boundary(slice, anchor),
101+ TextObject::Inside => Range::new(next_grapheme_boundary(slice, anchor), head),
103− prev_grapheme_boundary(slice, head),
104− ),
105− TextObject::Around => Range::new(anchor, head),
102+ TextObject::Around => Range::new(anchor, next_grapheme_boundary(slice, head)),
106 })
103 })
107 .unwrap_or(range)
104 .unwrap_or(range)
108}
105}