1+ use std:: collections:: HashSet ;
2+
13use crate :: {
24 core:: cursor:: Cursor ,
35 grapheme:: { Grapheme , Graphemes } ,
@@ -117,41 +119,41 @@ impl TextEditor {
117119 }
118120 }
119121
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 {
122124 let current_position = self . position ( ) ;
123125 self . text ( )
124126 . chars ( )
125127 . iter ( )
126128 . enumerate ( )
127129 . filter ( |& ( i, _) | i < current_position. saturating_sub ( 1 ) )
128130 . rev ( )
129- . find ( |& ( _, c) | items . contains ( c) )
131+ . find ( |& ( _, c) | word_break_chars . contains ( c) )
130132 . map ( |( i, _) | i + 1 )
131133 . unwrap_or ( 0 )
132134 }
133135
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 ) ;
137139 self . erase_to_position ( pos) ;
138140 }
139141
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 ) ;
143145 self . 0 . move_to ( pos) ;
144146 }
145147
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 {
148150 let current_position = self . position ( ) ;
149151 self . text ( )
150152 . chars ( )
151153 . iter ( )
152154 . enumerate ( )
153155 . filter ( |& ( i, _) | i > current_position)
154- . find ( |& ( _, c) | items . contains ( c) )
156+ . find ( |& ( _, c) | word_break_chars . contains ( c) )
155157 . map ( |( i, _) | {
156158 if i < self . 0 . contents ( ) . len ( ) - 1 {
157159 i + 1
@@ -162,15 +164,15 @@ impl TextEditor {
162164 . unwrap_or ( self . 0 . contents ( ) . len ( ) - 1 )
163165 }
164166
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 ) ;
168170 self . erase_to_position ( pos) ;
169171 }
170172
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 ) ;
174176 self . 0 . move_to ( pos) ;
175177 }
176178
@@ -269,38 +271,42 @@ mod test {
269271 }
270272
271273 mod find_previous_nearest_index {
274+ use std:: collections:: HashSet ;
275+
272276 use crate :: text_editor:: test:: new_with_position;
273277
274278 #[ test]
275279 fn test ( ) {
276280 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 ( [ ' ' ] ) ) ) ;
278282 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 ( [ ' ' ] ) ) ) ;
280284 }
281285
282286 #[ test]
283287 fn test_with_no_target ( ) {
284288 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' ] ) ) ) ;
286290 }
287291 }
288292
289293 mod find_next_nearest_index {
294+ use std:: collections:: HashSet ;
295+
290296 use crate :: text_editor:: test:: new_with_position;
291297
292298 #[ test]
293299 fn test ( ) {
294300 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 ( [ ' ' ] ) ) ) ;
296302 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 ( [ ' ' ] ) ) ) ;
298304 }
299305
300306 #[ test]
301307 fn test_with_no_target ( ) {
302308 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' ] ) ) ) ;
304310 }
305311 }
306312
0 commit comments