Skip to content

Commit c9cbc34

Browse files
cessenpickfire
authored andcommitted
Fix buggy surround behavior from #376.
Fixes #543.
1 parent 8c3a5b1 commit c9cbc34

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

helix-core/src/surround.rs

+22-23
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use crate::graphemes::next_grapheme_boundary;
21
use crate::{search, Selection};
32
use ropey::RopeSlice;
43

@@ -49,9 +48,9 @@ pub fn find_nth_pairs_pos(
4948
if Some(open) == text.get_char(pos) {
5049
// Special case: cursor is directly on a matching char.
5150
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)?)),
5352
_ 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))
5554
}
5655
// We return no match because there's no way to know which
5756
// side of the char we should be searching on.
@@ -60,13 +59,13 @@ pub fn find_nth_pairs_pos(
6059
} else {
6160
Some((
6261
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)?,
6463
))
6564
}
6665
} else {
6766
Some((
6867
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)?,
7069
))
7170
}
7271
}
@@ -185,13 +184,13 @@ mod test {
185184
let slice = doc.slice(..);
186185

187186
// cursor on [t]ext
188-
assert_eq!(find_nth_pairs_pos(slice, '(', 6, 1), Some((5, 11)));
189-
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)));
188+
assert_eq!(find_nth_pairs_pos(slice, ')', 6, 1), Some((5, 10)));
190189
// cursor on so[m]e
191190
assert_eq!(find_nth_pairs_pos(slice, '(', 2, 1), None);
192191
// cursor on bracket itself
193-
assert_eq!(find_nth_pairs_pos(slice, '(', 5, 1), Some((5, 11)));
194-
assert_eq!(find_nth_pairs_pos(slice, '(', 10, 1), Some((5, 11)));
192+
assert_eq!(find_nth_pairs_pos(slice, '(', 5, 1), Some((5, 10)));
193+
assert_eq!(find_nth_pairs_pos(slice, '(', 10, 1), Some((5, 10)));
195194
}
196195

197196
#[test]
@@ -200,9 +199,9 @@ mod test {
200199
let slice = doc.slice(..);
201200

202201
// cursor on go[o]d
203-
assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((10, 16)));
204-
assert_eq!(find_nth_pairs_pos(slice, '(', 13, 2), Some((4, 22)));
205-
assert_eq!(find_nth_pairs_pos(slice, '(', 13, 3), Some((0, 28)));
202+
assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((10, 15)));
203+
assert_eq!(find_nth_pairs_pos(slice, '(', 13, 2), Some((4, 21)));
204+
assert_eq!(find_nth_pairs_pos(slice, '(', 13, 3), Some((0, 27)));
206205
}
207206

208207
#[test]
@@ -211,14 +210,14 @@ mod test {
211210
let slice = doc.slice(..);
212211

213212
// cursor on go[o]d
214-
assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 1), Some((10, 16)));
215-
assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 2), Some((4, 22)));
216-
assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 3), Some((0, 28)));
213+
assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 1), Some((10, 15)));
214+
assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 2), Some((4, 21)));
215+
assert_eq!(find_nth_pairs_pos(slice, '\'', 13, 3), Some((0, 27)));
217216
// cursor on the quotes
218217
assert_eq!(find_nth_pairs_pos(slice, '\'', 10, 1), None);
219218
// 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)));
221-
assert_eq!(find_nth_pairs_pos(slice, '\'', 27, 1), Some((21, 28)));
219+
assert_eq!(find_nth_pairs_pos(slice, '\'', 0, 1), Some((0, 4)));
220+
assert_eq!(find_nth_pairs_pos(slice, '\'', 27, 1), Some((21, 27)));
222221
}
223222

224223
#[test]
@@ -227,8 +226,8 @@ mod test {
227226
let slice = doc.slice(..);
228227

229228
// cursor on go[o]d
230-
assert_eq!(find_nth_pairs_pos(slice, '(', 15, 1), Some((5, 25)));
231-
assert_eq!(find_nth_pairs_pos(slice, '(', 15, 2), Some((0, 32)));
229+
assert_eq!(find_nth_pairs_pos(slice, '(', 15, 1), Some((5, 24)));
230+
assert_eq!(find_nth_pairs_pos(slice, '(', 15, 2), Some((0, 31)));
232231
}
233232

234233
#[test]
@@ -237,9 +236,9 @@ mod test {
237236
let slice = doc.slice(..);
238237

239238
// cursor on go[o]d
240-
assert_eq!(find_nth_pairs_pos(slice, '{', 13, 1), Some((10, 16)));
241-
assert_eq!(find_nth_pairs_pos(slice, '[', 13, 1), Some((4, 22)));
242-
assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((0, 28)));
239+
assert_eq!(find_nth_pairs_pos(slice, '{', 13, 1), Some((10, 15)));
240+
assert_eq!(find_nth_pairs_pos(slice, '[', 13, 1), Some((4, 21)));
241+
assert_eq!(find_nth_pairs_pos(slice, '(', 13, 1), Some((0, 27)));
243242
}
244243

245244
#[test]
@@ -256,7 +255,7 @@ mod test {
256255
get_surround_pos(slice, &selection, '(', 1)
257256
.unwrap()
258257
.as_slice(),
259-
&[0, 6, 7, 14, 15, 24]
258+
&[0, 5, 7, 13, 15, 23]
260259
);
261260
}
262261

helix-core/src/textobject.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,8 @@ pub fn textobject_surround(
9898
) -> Range {
9999
surround::find_nth_pairs_pos(slice, ch, range.head, count)
100100
.map(|(anchor, head)| match textobject {
101-
TextObject::Inside => Range::new(
102-
next_grapheme_boundary(slice, anchor),
103-
prev_grapheme_boundary(slice, head),
104-
),
105-
TextObject::Around => Range::new(anchor, head),
101+
TextObject::Inside => Range::new(next_grapheme_boundary(slice, anchor), head),
102+
TextObject::Around => Range::new(anchor, next_grapheme_boundary(slice, head)),
106103
})
107104
.unwrap_or(range)
108105
}

0 commit comments

Comments
 (0)