@@ -283,20 +283,25 @@ impl CodeSuggestion {
283283 let mut buf = String :: new ( ) ;
284284
285285 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 ;
286289 for part in & substitution. parts {
287290 let cur_lo = sm. lookup_char_pos ( part. span . lo ( ) ) ;
288291 if prev_hi. line == cur_lo. line {
289292 let mut count =
290293 push_trailing ( & mut buf, prev_line. as_ref ( ) , & prev_hi, Some ( & cur_lo) ) ;
291294 while count > 0 {
292295 highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
296+ acc = 0 ;
293297 count -= 1 ;
294298 }
295299 } else {
296300 highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
297301 let mut count = push_trailing ( & mut buf, prev_line. as_ref ( ) , & prev_hi, None ) ;
298302 while count > 0 {
299303 highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
304+ acc = 0 ;
300305 count -= 1 ;
301306 }
302307 // push lines between the previous and current span (if any)
@@ -305,6 +310,7 @@ impl CodeSuggestion {
305310 buf. push_str ( line. as_ref ( ) ) ;
306311 buf. push ( '\n' ) ;
307312 highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
313+ acc = 0 ;
308314 }
309315 }
310316 if let Some ( cur_line) = sf. get_line ( cur_lo. line - 1 ) {
@@ -316,18 +322,22 @@ impl CodeSuggestion {
316322 }
317323 }
318324 // Add a whole line highlight per line in the snippet.
325+ let len = part. snippet . split ( '\n' ) . next ( ) . unwrap_or ( & part. snippet ) . len ( ) ;
319326 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 ,
323329 } ) ;
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 ) ;
324336 for line in part. snippet . split ( '\n' ) . skip ( 1 ) {
337+ acc = 0 ;
325338 highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
326339 line_highlight. push ( SubstitutionHighlight { start : 0 , end : line. len ( ) } ) ;
327340 }
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 ) ;
331341 }
332342 highlights. push ( std:: mem:: take ( & mut line_highlight) ) ;
333343 let only_capitalization = is_case_difference ( sm, & buf, bounding_span) ;
0 commit comments