@@ -102,7 +102,7 @@ impl FileChecker for FixTypos {
102102 }
103103 }
104104 if !fixes. is_empty ( ) || path == std:: path:: Path :: new ( "-" ) {
105- let buffer = fix_buffer ( buffer, fixes. into_iter ( ) ) ;
105+ let buffer = fix_buffer ( buffer, fixes, None ) ;
106106 write_file ( path, content_type, buffer, reporter) ?;
107107 }
108108 }
@@ -127,11 +127,7 @@ impl FileChecker for FixTypos {
127127 }
128128 }
129129 if !fixes. is_empty ( ) {
130- let file_name = file_name. to_owned ( ) . into_bytes ( ) ;
131- let new_name = fix_buffer ( file_name, fixes. into_iter ( ) ) ;
132- let new_name =
133- String :: from_utf8 ( new_name) . expect ( "corrections are valid utf-8" ) ;
134- let new_path = path. with_file_name ( new_name) ;
130+ let new_path = fix_file_name ( path, file_name, fixes, None ) ?;
135131 std:: fs:: rename ( path, new_path) ?;
136132 }
137133 }
@@ -179,7 +175,7 @@ impl FileChecker for DiffTypos {
179175 }
180176 }
181177 if !fixes. is_empty ( ) {
182- new_content = fix_buffer ( buffer. clone ( ) , fixes. into_iter ( ) ) ;
178+ new_content = fix_buffer ( buffer. clone ( ) , fixes, None ) ;
183179 content = buffer;
184180 }
185181 }
@@ -205,11 +201,7 @@ impl FileChecker for DiffTypos {
205201 }
206202 }
207203 if !fixes. is_empty ( ) {
208- let file_name = file_name. to_owned ( ) . into_bytes ( ) ;
209- let new_name = fix_buffer ( file_name, fixes. into_iter ( ) ) ;
210- let new_name =
211- String :: from_utf8 ( new_name) . expect ( "corrections are valid utf-8" ) ;
212- new_path = Some ( path. with_file_name ( new_name) ) ;
204+ new_path = fix_file_name ( path, file_name, fixes, None ) . ok ( ) ;
213205 }
214206 }
215207 }
@@ -650,10 +642,24 @@ fn is_fixable(typo: &typos::Typo<'_>) -> bool {
650642 extract_fix ( typo) . is_some ( )
651643}
652644
653- fn fix_buffer ( mut buffer : Vec < u8 > , typos : impl Iterator < Item = typos:: Typo < ' static > > ) -> Vec < u8 > {
645+ fn fix_buffer (
646+ mut buffer : Vec < u8 > ,
647+ typos : Vec < typos:: Typo < ' _ > > ,
648+ correction_indices : Option < Vec < usize > > ,
649+ ) -> Vec < u8 > {
654650 let mut offset = 0isize ;
655- for typo in typos {
656- let fix = extract_fix ( & typo) . expect ( "Caller only provides fixable typos" ) ;
651+ for typo_index in 0 ..typos. len ( ) - 1 {
652+ let typo = & typos[ typo_index] ;
653+ let correction_index = match & correction_indices {
654+ Some ( correction_indices) => correction_indices[ typo_index] ,
655+ None => 0 ,
656+ } ;
657+
658+ let fix = match & typo. corrections {
659+ typos:: Status :: Corrections ( c) => Some ( c[ correction_index] . as_ref ( ) ) ,
660+ _ => None ,
661+ }
662+ . expect ( "Caller provided invalid fix index" ) ;
657663 let start = ( ( typo. byte_offset as isize ) + offset) as usize ;
658664 let end = start + typo. typo . len ( ) ;
659665
@@ -664,6 +670,19 @@ fn fix_buffer(mut buffer: Vec<u8>, typos: impl Iterator<Item = typos::Typo<'stat
664670 buffer
665671}
666672
673+ fn fix_file_name < ' a > (
674+ path : & std:: path:: Path ,
675+ file_name : & ' a str ,
676+ fixes : Vec < typos:: Typo < ' a > > ,
677+ correction_indices : Option < Vec < usize > > ,
678+ ) -> Result < std:: path:: PathBuf , std:: io:: Error > {
679+ let file_name = file_name. to_owned ( ) . into_bytes ( ) ;
680+ let new_name = fix_buffer ( file_name, fixes, correction_indices) ;
681+ let new_name = String :: from_utf8 ( new_name) . expect ( "corrections are valid utf-8" ) ;
682+ let new_path = path. with_file_name ( new_name) ;
683+ Ok ( new_path)
684+ }
685+
667686pub fn walk_path (
668687 walk : ignore:: Walk ,
669688 checks : & dyn FileChecker ,
@@ -777,8 +796,9 @@ mod test {
777796 byte_offset,
778797 typo : typo. into ( ) ,
779798 corrections : typos:: Status :: Corrections ( vec ! [ correction. into( ) ] ) ,
780- } ) ;
781- let actual = fix_buffer ( line, corrections) ;
799+ } )
800+ . collect ( ) ;
801+ let actual = fix_buffer ( line, corrections, None ) ;
782802 String :: from_utf8 ( actual) . unwrap ( )
783803 }
784804
0 commit comments