This repository was archived by the owner on Nov 24, 2023. It is now read-only.
File tree 1 file changed +24
-7
lines changed
1 file changed +24
-7
lines changed Original file line number Diff line number Diff line change @@ -210,22 +210,39 @@ pub fn collect_suggestions<S: ::std::hash::BuildHasher>(
210
210
}
211
211
}
212
212
213
- pub fn apply_suggestions ( code : & str , suggestions : & [ Suggestion ] ) -> Result < String , Error > {
214
- use replace:: Data ;
213
+ pub struct CodeFix {
214
+ data : replace:: Data ,
215
+ }
215
216
216
- let mut fixed = Data :: new ( code. as_bytes ( ) ) ;
217
+ impl CodeFix {
218
+ pub fn new ( s : & str ) -> CodeFix {
219
+ CodeFix {
220
+ data : replace:: Data :: new ( s. as_bytes ( ) ) ,
221
+ }
222
+ }
217
223
218
- for sug in suggestions . iter ( ) . rev ( ) {
219
- for sol in & sug . solutions {
224
+ pub fn apply ( & mut self , suggestion : & Suggestion ) -> Result < ( ) , Error > {
225
+ for sol in & suggestion . solutions {
220
226
for r in & sol. replacements {
221
- fixed . replace_range (
227
+ self . data . replace_range (
222
228
r. snippet . range . start ,
223
229
r. snippet . range . end . saturating_sub ( 1 ) ,
224
230
r. replacement . as_bytes ( ) ,
225
231
) ?;
226
232
}
227
233
}
234
+ Ok ( ( ) )
228
235
}
229
236
230
- Ok ( String :: from_utf8 ( fixed. to_vec ( ) ) ?)
237
+ pub fn finish ( & self ) -> Result < String , Error > {
238
+ Ok ( String :: from_utf8 ( self . data . to_vec ( ) ) ?)
239
+ }
240
+ }
241
+
242
+ pub fn apply_suggestions ( code : & str , suggestions : & [ Suggestion ] ) -> Result < String , Error > {
243
+ let mut fix = CodeFix :: new ( code) ;
244
+ for suggestion in suggestions. iter ( ) . rev ( ) {
245
+ fix. apply ( suggestion) ?;
246
+ }
247
+ fix. finish ( )
231
248
}
You can’t perform that action at this time.
0 commit comments