1
Fork 0

Auto merge of #109786 - estebank:tweak-add-line-sugg, r=compiler-errors

Tweak output for 'add line' suggestion

Closes #108174
This commit is contained in:
bors 2023-04-13 07:02:53 +00:00
commit d37e2f74af
165 changed files with 458 additions and 296 deletions

View file

@ -1832,6 +1832,12 @@ impl EmitterWriter {
}
let show_code_change = if has_deletion && !is_multiline {
DisplaySuggestion::Diff
} else if let [part] = &parts[..]
&& part.snippet.ends_with('\n')
&& part.snippet.trim() == complete.trim()
{
// We are adding a line(s) of code before code that was already there.
DisplaySuggestion::Add
} else if (parts.len() != 1 || parts[0].snippet.trim() != complete.trim())
&& !is_multiline
{
@ -1879,7 +1885,10 @@ impl EmitterWriter {
row_num += line_end - line_start;
}
let mut unhighlighted_lines = Vec::new();
let mut last_pos = 0;
let mut is_item_attribute = false;
for (line_pos, (line, highlight_parts)) in lines.by_ref().zip(highlights).enumerate() {
last_pos = line_pos;
debug!(%line_pos, %line, ?highlight_parts);
// Remember lines that are not highlighted to hide them if needed
@ -1887,6 +1896,12 @@ impl EmitterWriter {
unhighlighted_lines.push((line_pos, line));
continue;
}
if highlight_parts.len() == 1
&& line.trim().starts_with("#[")
&& line.trim().ends_with(']')
{
is_item_attribute = true;
}
match unhighlighted_lines.len() {
0 => (),
@ -1963,13 +1978,41 @@ impl EmitterWriter {
is_multiline,
)
}
if let DisplaySuggestion::Add = show_code_change && is_item_attribute {
// The suggestion adds an entire line of code, ending on a newline, so we'll also
// print the *following* line, to provide context of what we're advicing people to
// do. Otherwise you would only see contextless code that can be confused for
// already existing code, despite the colors and UI elements.
// We special case `#[derive(_)]\n` and other attribute suggestions, because those
// are the ones where context is most useful.
let file_lines = sm
.span_to_lines(span.primary_span().unwrap().shrink_to_hi())
.expect("span_to_lines failed when emitting suggestion");
let line_num = sm.lookup_char_pos(parts[0].span.lo()).line;
if let Some(line) = file_lines.file.get_line(line_num - 1) {
let line = normalize_whitespace(&line);
self.draw_code_line(
&mut buffer,
&mut row_num,
&[],
line_num + last_pos + 1,
&line,
DisplaySuggestion::None,
max_line_num_len,
&file_lines,
is_multiline,
)
}
}
// This offset and the ones below need to be signed to account for replacement code
// that is shorter than the original code.
let mut offsets: Vec<(usize, isize)> = Vec::new();
// Only show an underline in the suggestions if the suggestion is not the
// entirety of the code being shown and the displayed code is not multiline.
if let DisplaySuggestion::Diff | DisplaySuggestion::Underline = show_code_change {
if let DisplaySuggestion::Diff | DisplaySuggestion::Underline | DisplaySuggestion::Add =
show_code_change
{
draw_col_separator_no_space(&mut buffer, row_num, max_line_num_len + 1);
for part in parts {
let span_start_pos = sm.lookup_char_pos(part.span.lo()).col_display;
@ -2247,6 +2290,10 @@ impl EmitterWriter {
}
}
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
} else if let DisplaySuggestion::Add = show_code_change {
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
} else {
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
draw_col_separator(buffer, *row_num, max_line_num_len + 1);
@ -2281,6 +2328,7 @@ enum DisplaySuggestion {
Underline,
Diff,
None,
Add,
}
impl FileWithAnnotatedLines {