@@ -1242,50 +1242,55 @@ impl EmitterWriter {
1242
1242
line_pos += 1 ;
1243
1243
row_num += 1 ;
1244
1244
}
1245
- let mut extra = 0 ;
1245
+
1246
+ // This offset and the ones below need to be signed to account for replacement code
1247
+ // that is shorter than the original code.
1248
+ let mut offset: isize = 0 ;
1246
1249
// Only show an underline in the suggestions if the suggestion is not the
1247
1250
// entirety of the code being shown and the displayed code is not multiline.
1248
1251
if show_underline {
1249
1252
draw_col_separator ( & mut buffer, row_num, max_line_num_len + 1 ) ;
1250
1253
for part in parts {
1251
- let span_start_pos = cm. lookup_char_pos ( part. span . lo ( ) ) ;
1252
- let span_end_pos = cm. lookup_char_pos ( part. span . hi ( ) ) ;
1253
- // length of the code to be substituted
1254
- let snippet_len = span_end_pos . col_display - span_start_pos . col_display ;
1255
-
1256
- // Do not underline the leading or trailing spaces.
1257
- let start = part . snippet . len ( ) - part . snippet . trim_left ( ) . len ( ) ;
1258
- // account for substitutions containing unicode characters
1254
+ let span_start_pos = cm. lookup_char_pos ( part. span . lo ( ) ) . col_display ;
1255
+ let span_end_pos = cm. lookup_char_pos ( part. span . hi ( ) ) . col_display ;
1256
+
1257
+ // Do not underline the leading...
1258
+ let start = part . snippet . len ( )
1259
+ . saturating_sub ( part . snippet . trim_left ( ) . len ( ) ) ;
1260
+ // ...or trailing spaces. Account for substitutions containing unicode
1261
+ // characters.
1259
1262
let sub_len = part. snippet . trim ( ) . chars ( ) . fold ( 0 , |acc, ch| {
1260
1263
acc + unicode_width:: UnicodeWidthChar :: width ( ch) . unwrap_or ( 0 )
1261
1264
} ) ;
1262
1265
1263
- let underline_start = span_start_pos. col_display + start + extra ;
1264
- let underline_end = span_start_pos. col_display + start + sub_len + extra ;
1266
+ let underline_start = ( span_start_pos + start) as isize + offset ;
1267
+ let underline_end = ( span_start_pos + start + sub_len) as isize + offset ;
1265
1268
for p in underline_start..underline_end {
1266
1269
buffer. putc ( row_num,
1267
- max_line_num_len + 3 + p,
1270
+ max_line_num_len + 3 + p as usize ,
1268
1271
'^' ,
1269
1272
Style :: UnderlinePrimary ) ;
1270
1273
}
1271
1274
// underline removals too
1272
1275
if underline_start == underline_end {
1273
1276
for p in underline_start-1 ..underline_start+1 {
1274
1277
buffer. putc ( row_num,
1275
- max_line_num_len + 3 + p,
1278
+ max_line_num_len + 3 + p as usize ,
1276
1279
'-' ,
1277
1280
Style :: UnderlineSecondary ) ;
1278
1281
}
1279
1282
}
1280
1283
1281
1284
// length of the code after substitution
1282
1285
let full_sub_len = part. snippet . chars ( ) . fold ( 0 , |acc, ch| {
1283
- acc + unicode_width:: UnicodeWidthChar :: width ( ch) . unwrap_or ( 0 )
1286
+ acc + unicode_width:: UnicodeWidthChar :: width ( ch) . unwrap_or ( 0 ) as isize
1284
1287
} ) ;
1285
1288
1289
+ // length of the code to be substituted
1290
+ let snippet_len = ( span_end_pos - span_start_pos) as isize ;
1286
1291
// For multiple substitutions, use the position *after* the previous
1287
1292
// substitutions have happened.
1288
- extra += full_sub_len - snippet_len;
1293
+ offset += full_sub_len - snippet_len;
1289
1294
}
1290
1295
row_num += 1 ;
1291
1296
}
0 commit comments