Skip to content

Commit 2daa013

Browse files
committed
Underline multiple suggested replacements in the same line
Follow up to #50943. Fix #50977.
1 parent 7426f5c commit 2daa013

File tree

1 file changed

+47
-18
lines changed

1 file changed

+47
-18
lines changed

src/librustc_errors/emitter.rs

Lines changed: 47 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,19 +1215,20 @@ impl EmitterWriter {
12151215

12161216
let mut row_num = 2;
12171217
for &(ref complete, ref parts) in suggestions.iter().take(MAX_SUGGESTIONS) {
1218-
let show_underline = parts.len() == 1
1219-
&& complete.lines().count() == 1
1220-
&& parts[0].snippet.trim() != complete.trim();
1218+
// Only show underline if the suggestion spans a single line and doesn't cover the
1219+
// entirety of the code output. If you have multiple replacements in the same line
1220+
// of code, show the underline.
1221+
let show_underline = !(parts.len() == 1
1222+
&& parts[0].snippet.trim() == complete.trim())
1223+
&& complete.lines().count() == 1;
12211224

12221225
let lines = cm.span_to_lines(parts[0].span).unwrap();
12231226

12241227
assert!(!lines.lines.is_empty());
12251228

1226-
let span_start_pos = cm.lookup_char_pos(parts[0].span.lo());
1227-
let line_start = span_start_pos.line;
1229+
let line_start = cm.lookup_char_pos(parts[0].span.lo()).line;
12281230
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
12291231
let mut line_pos = 0;
1230-
// Only show underline if there's a single suggestion and it is a single line
12311232
let mut lines = complete.lines();
12321233
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
12331234
// Print the span column to avoid confusion
@@ -1241,22 +1242,50 @@ impl EmitterWriter {
12411242
line_pos += 1;
12421243
row_num += 1;
12431244
}
1245+
let mut extra = 0;
12441246
// Only show an underline in the suggestions if the suggestion is not the
12451247
// entirety of the code being shown and the displayed code is not multiline.
12461248
if show_underline {
12471249
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
1248-
let start = parts[0].snippet.len() - parts[0].snippet.trim_left().len();
1249-
// account for substitutions containing unicode characters
1250-
let sub_len = parts[0].snippet.trim().chars().fold(0, |acc, ch| {
1251-
acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0)
1252-
});
1253-
let underline_start = span_start_pos.col_display + start;
1254-
let underline_end = span_start_pos.col_display + start + sub_len;
1255-
for p in underline_start..underline_end {
1256-
buffer.putc(row_num,
1257-
max_line_num_len + 3 + p,
1258-
'^',
1259-
Style::UnderlinePrimary);
1250+
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
1259+
let sub_len = part.snippet.trim().chars().fold(0, |acc, ch| {
1260+
acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0)
1261+
});
1262+
1263+
let underline_start = span_start_pos.col_display + start + extra;
1264+
let underline_end = span_start_pos.col_display + start + sub_len + extra;
1265+
for p in underline_start..underline_end {
1266+
buffer.putc(row_num,
1267+
max_line_num_len + 3 + p,
1268+
'^',
1269+
Style::UnderlinePrimary);
1270+
}
1271+
// underline removals too
1272+
if underline_start == underline_end {
1273+
for p in underline_start-1..underline_start+1 {
1274+
buffer.putc(row_num,
1275+
max_line_num_len + 3 + p,
1276+
'-',
1277+
Style::UnderlineSecondary);
1278+
}
1279+
}
1280+
1281+
// length of the code after substitution
1282+
let full_sub_len = part.snippet.chars().fold(0, |acc, ch| {
1283+
acc + unicode_width::UnicodeWidthChar::width(ch).unwrap_or(0)
1284+
});
1285+
1286+
// For multiple substitutions, use the position *after* the previous
1287+
// substitutions have happened.
1288+
extra += full_sub_len - snippet_len;
12601289
}
12611290
row_num += 1;
12621291
}

0 commit comments

Comments
 (0)