Skip to content

Commit 75fd1bf

Browse files
committed
Account for tabs when highlighting multiline code suggestions
1 parent 33fdb79 commit 75fd1bf

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

compiler/rustc_errors/src/emitter.rs

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,7 +1623,7 @@ impl EmitterWriter {
16231623
let line_start = sm.lookup_char_pos(parts[0].span.lo()).line;
16241624
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
16251625
let mut lines = complete.lines();
1626-
for (line_pos, (line, parts)) in
1626+
for (line_pos, (line, highlight_parts)) in
16271627
lines.by_ref().zip(highlights).take(MAX_SUGGESTION_HIGHLIGHT_LINES).enumerate()
16281628
{
16291629
// Print the span column to avoid confusion
@@ -1658,7 +1658,7 @@ impl EmitterWriter {
16581658
);
16591659
buffer.puts(row_num, max_line_num_len + 1, "+ ", Style::Addition);
16601660
} else if is_multiline {
1661-
match &parts[..] {
1661+
match &highlight_parts[..] {
16621662
[SubstitutionHighlight { start: 0, end }] if *end == line.len() => {
16631663
buffer.puts(row_num, max_line_num_len + 1, "+ ", Style::Addition);
16641664
}
@@ -1676,16 +1676,33 @@ impl EmitterWriter {
16761676
// print the suggestion
16771677
buffer.append(row_num, &replace_tabs(line), Style::NoStyle);
16781678

1679-
if is_multiline {
1680-
for SubstitutionHighlight { start, end } in parts {
1681-
buffer.set_style_range(
1682-
row_num,
1683-
max_line_num_len + 3 + start,
1684-
max_line_num_len + 3 + end,
1685-
Style::Addition,
1686-
true,
1687-
);
1688-
}
1679+
// Colorize addition/replacements with green.
1680+
for &SubstitutionHighlight { start, end } in highlight_parts {
1681+
// Account for tabs when highlighting (#87972).
1682+
let start: usize = line
1683+
.chars()
1684+
.take(start)
1685+
.map(|ch| match ch {
1686+
'\t' => 4,
1687+
_ => 1,
1688+
})
1689+
.sum();
1690+
1691+
let end: usize = line
1692+
.chars()
1693+
.take(end)
1694+
.map(|ch| match ch {
1695+
'\t' => 4,
1696+
_ => 1,
1697+
})
1698+
.sum();
1699+
buffer.set_style_range(
1700+
row_num,
1701+
max_line_num_len + 3 + start,
1702+
max_line_num_len + 3 + end,
1703+
Style::Addition,
1704+
true,
1705+
);
16891706
}
16901707
row_num += 1;
16911708
}
@@ -1723,13 +1740,6 @@ impl EmitterWriter {
17231740
assert!(underline_start >= 0 && underline_end >= 0);
17241741
let padding: usize = max_line_num_len + 3;
17251742
for p in underline_start..underline_end {
1726-
// Colorize addition/replacements with green.
1727-
buffer.set_style(
1728-
row_num - 1,
1729-
(padding as isize + p) as usize,
1730-
Style::Addition,
1731-
true,
1732-
);
17331743
if !show_diff {
17341744
// If this is a replacement, underline with `^`, if this is an addition
17351745
// underline with `+`.

compiler/rustc_errors/src/lib.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)