@@ -283,20 +283,25 @@ impl CodeSuggestion {
283
283
let mut buf = String :: new ( ) ;
284
284
285
285
let mut line_highlight = vec ! [ ] ;
286
+ // We need to keep track of the difference between the existing code and the added
287
+ // or deleted code in order to point at the correct column *after* substitution.
288
+ let mut acc = 0 ;
286
289
for part in & substitution. parts {
287
290
let cur_lo = sm. lookup_char_pos ( part. span . lo ( ) ) ;
288
291
if prev_hi. line == cur_lo. line {
289
292
let mut count =
290
293
push_trailing ( & mut buf, prev_line. as_ref ( ) , & prev_hi, Some ( & cur_lo) ) ;
291
294
while count > 0 {
292
295
highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
296
+ acc = 0 ;
293
297
count -= 1 ;
294
298
}
295
299
} else {
296
300
highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
297
301
let mut count = push_trailing ( & mut buf, prev_line. as_ref ( ) , & prev_hi, None ) ;
298
302
while count > 0 {
299
303
highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
304
+ acc = 0 ;
300
305
count -= 1 ;
301
306
}
302
307
// push lines between the previous and current span (if any)
@@ -305,6 +310,7 @@ impl CodeSuggestion {
305
310
buf. push_str ( line. as_ref ( ) ) ;
306
311
buf. push ( '\n' ) ;
307
312
highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
313
+ acc = 0 ;
308
314
}
309
315
}
310
316
if let Some ( cur_line) = sf. get_line ( cur_lo. line - 1 ) {
@@ -316,18 +322,22 @@ impl CodeSuggestion {
316
322
}
317
323
}
318
324
// Add a whole line highlight per line in the snippet.
325
+ let len = part. snippet . split ( '\n' ) . next ( ) . unwrap_or ( & part. snippet ) . len ( ) ;
319
326
line_highlight. push ( SubstitutionHighlight {
320
- start : cur_lo. col . 0 ,
321
- end : cur_lo. col . 0
322
- + part. snippet . split ( '\n' ) . next ( ) . unwrap_or ( & part. snippet ) . len ( ) ,
327
+ start : ( cur_lo. col . 0 as isize + acc) as usize ,
328
+ end : ( cur_lo. col . 0 as isize + acc + len as isize ) as usize ,
323
329
} ) ;
330
+ buf. push_str ( & part. snippet ) ;
331
+ prev_hi = sm. lookup_char_pos ( part. span . hi ( ) ) ;
332
+ if prev_hi. line == cur_lo. line {
333
+ acc += len as isize - ( prev_hi. col . 0 - cur_lo. col . 0 ) as isize ;
334
+ }
335
+ prev_line = sf. get_line ( prev_hi. line - 1 ) ;
324
336
for line in part. snippet . split ( '\n' ) . skip ( 1 ) {
337
+ acc = 0 ;
325
338
highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
326
339
line_highlight. push ( SubstitutionHighlight { start : 0 , end : line. len ( ) } ) ;
327
340
}
328
- buf. push_str ( & part. snippet ) ;
329
- prev_hi = sm. lookup_char_pos ( part. span . hi ( ) ) ;
330
- prev_line = sf. get_line ( prev_hi. line - 1 ) ;
331
341
}
332
342
highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
333
343
let only_capitalization = is_case_difference ( sm, & buf, bounding_span) ;
0 commit comments