1
+ use std:: collections:: HashSet ;
2
+
1
3
use crate :: {
2
4
core:: cursor:: Cursor ,
3
5
grapheme:: { Grapheme , Graphemes } ,
@@ -117,41 +119,41 @@ impl TextEditor {
117
119
}
118
120
}
119
121
120
- /// Finds the nearest previous index of any character in `items ` from the cursor position.
121
- fn find_previous_nearest_index ( & self , items : & [ char ] ) -> usize {
122
+ /// Finds the nearest previous index of any character in `word_break_chars ` from the cursor position.
123
+ fn find_previous_nearest_index ( & self , word_break_chars : & HashSet < char > ) -> usize {
122
124
let current_position = self . position ( ) ;
123
125
self . text ( )
124
126
. chars ( )
125
127
. iter ( )
126
128
. enumerate ( )
127
129
. filter ( |& ( i, _) | i < current_position. saturating_sub ( 1 ) )
128
130
. rev ( )
129
- . find ( |& ( _, c) | items . contains ( c) )
131
+ . find ( |& ( _, c) | word_break_chars . contains ( c) )
130
132
. map ( |( i, _) | i + 1 )
131
133
. unwrap_or ( 0 )
132
134
}
133
135
134
- /// Erases the text from the current cursor position to the nearest previous character in `items `.
135
- pub fn erase_to_previous_nearest ( & mut self , items : & [ char ] ) {
136
- let pos = self . find_previous_nearest_index ( items ) ;
136
+ /// Erases the text from the current cursor position to the nearest previous character in `word_break_chars `.
137
+ pub fn erase_to_previous_nearest ( & mut self , word_break_chars : & HashSet < char > ) {
138
+ let pos = self . find_previous_nearest_index ( word_break_chars ) ;
137
139
self . erase_to_position ( pos) ;
138
140
}
139
141
140
- /// Moves the cursor to the nearest previous character in `items `.
141
- pub fn move_to_previous_nearest ( & mut self , items : & [ char ] ) {
142
- let pos = self . find_previous_nearest_index ( items ) ;
142
+ /// Moves the cursor to the nearest previous character in `word_break_chars `.
143
+ pub fn move_to_previous_nearest ( & mut self , word_break_chars : & HashSet < char > ) {
144
+ let pos = self . find_previous_nearest_index ( word_break_chars ) ;
143
145
self . 0 . move_to ( pos) ;
144
146
}
145
147
146
- /// Finds the nearest next index of any character in `items ` from the cursor position.
147
- fn find_next_nearest_index ( & self , items : & [ char ] ) -> usize {
148
+ /// Finds the nearest next index of any character in `word_break_chars ` from the cursor position.
149
+ fn find_next_nearest_index ( & self , word_break_chars : & HashSet < char > ) -> usize {
148
150
let current_position = self . position ( ) ;
149
151
self . text ( )
150
152
. chars ( )
151
153
. iter ( )
152
154
. enumerate ( )
153
155
. filter ( |& ( i, _) | i > current_position)
154
- . find ( |& ( _, c) | items . contains ( c) )
156
+ . find ( |& ( _, c) | word_break_chars . contains ( c) )
155
157
. map ( |( i, _) | {
156
158
if i < self . 0 . contents ( ) . len ( ) - 1 {
157
159
i + 1
@@ -162,15 +164,15 @@ impl TextEditor {
162
164
. unwrap_or ( self . 0 . contents ( ) . len ( ) - 1 )
163
165
}
164
166
165
- /// Erases the text from the current cursor position to the nearest next character in `items `.
166
- pub fn erase_to_next_nearest ( & mut self , items : & [ char ] ) {
167
- let pos = self . find_next_nearest_index ( items ) ;
167
+ /// Erases the text from the current cursor position to the nearest next character in `word_break_chars `.
168
+ pub fn erase_to_next_nearest ( & mut self , word_break_chars : & HashSet < char > ) {
169
+ let pos = self . find_next_nearest_index ( word_break_chars ) ;
168
170
self . erase_to_position ( pos) ;
169
171
}
170
172
171
- /// Moves the cursor to the nearest next character in `items `.
172
- pub fn move_to_next_nearest ( & mut self , items : & [ char ] ) {
173
- let pos = self . find_next_nearest_index ( items ) ;
173
+ /// Moves the cursor to the nearest next character in `word_break_chars `.
174
+ pub fn move_to_next_nearest ( & mut self , word_break_chars : & HashSet < char > ) {
175
+ let pos = self . find_next_nearest_index ( word_break_chars ) ;
174
176
self . 0 . move_to ( pos) ;
175
177
}
176
178
@@ -269,38 +271,42 @@ mod test {
269
271
}
270
272
271
273
mod find_previous_nearest_index {
274
+ use std:: collections:: HashSet ;
275
+
272
276
use crate :: text_editor:: test:: new_with_position;
273
277
274
278
#[ test]
275
279
fn test ( ) {
276
280
let mut txt = new_with_position ( String :: from ( "koko momo jojo " ) , 11 ) ; // indicate `o`.
277
- assert_eq ! ( 10 , txt. find_previous_nearest_index( & [ ' ' ] ) ) ;
281
+ assert_eq ! ( 10 , txt. find_previous_nearest_index( & HashSet :: from ( [ ' ' ] ) ) ) ;
278
282
txt. 0 . move_to ( 10 ) ;
279
- assert_eq ! ( 5 , txt. find_previous_nearest_index( & [ ' ' ] ) ) ;
283
+ assert_eq ! ( 5 , txt. find_previous_nearest_index( & HashSet :: from ( [ ' ' ] ) ) ) ;
280
284
}
281
285
282
286
#[ test]
283
287
fn test_with_no_target ( ) {
284
288
let txt = new_with_position ( String :: from ( "koko momo jojo " ) , 7 ) ; // indicate `m`.
285
- assert_eq ! ( 0 , txt. find_previous_nearest_index( & [ 'z' ] ) ) ;
289
+ assert_eq ! ( 0 , txt. find_previous_nearest_index( & HashSet :: from ( [ 'z' ] ) ) ) ;
286
290
}
287
291
}
288
292
289
293
mod find_next_nearest_index {
294
+ use std:: collections:: HashSet ;
295
+
290
296
use crate :: text_editor:: test:: new_with_position;
291
297
292
298
#[ test]
293
299
fn test ( ) {
294
300
let mut txt = new_with_position ( String :: from ( "koko momo jojo " ) , 7 ) ; // indicate `m`.
295
- assert_eq ! ( 10 , txt. find_next_nearest_index( & [ ' ' ] ) ) ;
301
+ assert_eq ! ( 10 , txt. find_next_nearest_index( & HashSet :: from ( [ ' ' ] ) ) ) ;
296
302
txt. 0 . move_to ( 10 ) ;
297
- assert_eq ! ( 14 , txt. find_next_nearest_index( & [ ' ' ] ) ) ;
303
+ assert_eq ! ( 14 , txt. find_next_nearest_index( & HashSet :: from ( [ ' ' ] ) ) ) ;
298
304
}
299
305
300
306
#[ test]
301
307
fn test_with_no_target ( ) {
302
308
let txt = new_with_position ( String :: from ( "koko momo jojo " ) , 7 ) ; // indicate `m`.
303
- assert_eq ! ( 14 , txt. find_next_nearest_index( & [ 'z' ] ) ) ;
309
+ assert_eq ! ( 14 , txt. find_next_nearest_index( & HashSet :: from ( [ 'z' ] ) ) ) ;
304
310
}
305
311
}
306
312
0 commit comments