1
- use crate :: graphemes:: next_grapheme_boundary;
2
1
use crate :: { search, Selection } ;
3
2
use ropey:: RopeSlice ;
4
3
@@ -49,9 +48,9 @@ pub fn find_nth_pairs_pos(
49
48
if Some ( open) == text. get_char ( pos) {
50
49
// Special case: cursor is directly on a matching char.
51
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
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
55
// We return no match because there's no way to know which
57
56
// side of the char we should be searching on.
@@ -60,13 +59,13 @@ pub fn find_nth_pairs_pos(
60
59
} else {
61
60
Some ( (
62
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
65
} else {
67
66
Some ( (
68
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
184
let slice = doc. slice ( ..) ;
186
185
187
186
// 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 ) ) ) ;
190
189
// cursor on so[m]e
191
190
assert_eq ! ( find_nth_pairs_pos( slice, '(' , 2 , 1 ) , None ) ;
192
191
// 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 ) ) ) ;
195
194
}
196
195
197
196
#[ test]
@@ -200,9 +199,9 @@ mod test {
200
199
let slice = doc. slice ( ..) ;
201
200
202
201
// 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 ) ) ) ;
206
205
}
207
206
208
207
#[ test]
@@ -211,14 +210,14 @@ mod test {
211
210
let slice = doc. slice ( ..) ;
212
211
213
212
// 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 ) ) ) ;
217
216
// cursor on the quotes
218
217
assert_eq ! ( find_nth_pairs_pos( slice, '\'' , 10 , 1 ) , None ) ;
219
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 ) ) ) ;
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 ) ) ) ;
222
221
}
223
222
224
223
#[ test]
@@ -227,8 +226,8 @@ mod test {
227
226
let slice = doc. slice ( ..) ;
228
227
229
228
// 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 ) ) ) ;
232
231
}
233
232
234
233
#[ test]
@@ -237,9 +236,9 @@ mod test {
237
236
let slice = doc. slice ( ..) ;
238
237
239
238
// 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 ) ) ) ;
243
242
}
244
243
245
244
#[ test]
@@ -256,7 +255,7 @@ mod test {
256
255
get_surround_pos( slice, & selection, '(' , 1 )
257
256
. unwrap( )
258
257
. as_slice( ) ,
259
- & [ 0 , 6 , 7 , 14 , 15 , 24 ]
258
+ & [ 0 , 5 , 7 , 13 , 15 , 23 ]
260
259
) ;
261
260
}
262
261
0 commit comments