Clean up and comment EmitterWriter.draw_code_line

This commit is contained in:
CastilloDel 2023-02-05 18:59:31 +01:00
parent cf0279991d
commit 3dd004470d

View File

@ -1882,9 +1882,8 @@ fn emit_suggestion_default(
&mut buffer,
&mut row_num,
&Vec::new(),
p,
p + line_start,
l,
line_start,
show_code_change,
max_line_num_len,
&file_lines,
@ -1907,9 +1906,8 @@ fn emit_suggestion_default(
&mut buffer,
&mut row_num,
&Vec::new(),
p,
p + line_start,
l,
line_start,
show_code_change,
max_line_num_len,
&file_lines,
@ -1925,9 +1923,8 @@ fn emit_suggestion_default(
&mut buffer,
&mut row_num,
&Vec::new(),
p,
p + line_start,
l,
line_start,
show_code_change,
max_line_num_len,
&file_lines,
@ -1941,9 +1938,8 @@ fn emit_suggestion_default(
&mut buffer,
&mut row_num,
highlight_parts,
line_pos,
line_pos + line_start,
line,
line_start,
show_code_change,
max_line_num_len,
&file_lines,
@ -2167,49 +2163,44 @@ fn draw_code_line(
buffer: &mut StyledBuffer,
row_num: &mut usize,
highlight_parts: &Vec<SubstitutionHighlight>,
line_pos: usize,
line: &str,
line_start: usize,
line_num: usize,
line_to_add: &str,
show_code_change: DisplaySuggestion,
max_line_num_len: usize,
file_lines: &FileLines,
is_multiline: bool,
) {
if let DisplaySuggestion::Diff = show_code_change {
// Add the line number for both addition and removal to drive the point home.
//
// N - fn foo<A: T>(bar: A) {
// N + fn foo(bar: impl T) {
let number_of_lines = file_lines.lines.len();
for (index, line_to_remove) in
file_lines.lines.iter().take(number_of_lines - 1).enumerate()
{
// We need to print more than one line if the span we need to remove is multiline.
// For more info: https://github.com/rust-lang/rust/issues/92741
let lines_to_remove = file_lines.lines.iter().take(file_lines.lines.len() - 1);
for (index, line_to_remove) in lines_to_remove.enumerate() {
buffer.puts(
*row_num - 1,
0,
&self.maybe_anonymized(line_start + line_pos + index),
&self.maybe_anonymized(line_num + index),
Style::LineNumber,
);
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
buffer.puts(
*row_num - 1,
max_line_num_len + 3,
&normalize_whitespace(
&file_lines.file.get_line(line_to_remove.line_index).unwrap(),
),
Style::NoStyle,
let line = normalize_whitespace(
&file_lines.file.get_line(line_to_remove.line_index).unwrap(),
);
buffer.puts(*row_num - 1, max_line_num_len + 3, &line, Style::NoStyle);
*row_num += 1;
}
let last_line = &file_lines
.file
.get_line(file_lines.lines[number_of_lines - 1].line_index)
.unwrap();
if last_line != line {
// If the last line is exactly equal to the line we need to add, we can skip both of them.
// This allows us to avoid output like the following:
// 2 - &
// 2 + if true { true } else { false }
// 3 - if true { true } else { false }
// If those lines aren't equal, we print their diff
let last_line_index = file_lines.lines[file_lines.lines.len() - 1].line_index;
let last_line = &file_lines.file.get_line(last_line_index).unwrap();
if last_line != line_to_add {
buffer.puts(
*row_num - 1,
0,
&self.maybe_anonymized(line_start + line_pos + number_of_lines - 1),
&self.maybe_anonymized(line_num + file_lines.lines.len() - 1),
Style::LineNumber,
);
buffer.puts(*row_num - 1, max_line_num_len + 1, "- ", Style::Removal);
@ -2219,27 +2210,16 @@ fn draw_code_line(
&normalize_whitespace(last_line),
Style::NoStyle,
);
buffer.puts(
*row_num,
0,
&self.maybe_anonymized(line_start + line_pos),
Style::LineNumber,
);
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
// print the suggestion
buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle);
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
} else {
*row_num -= 2;
}
} else if is_multiline {
buffer.puts(
*row_num,
0,
&self.maybe_anonymized(line_start + line_pos),
Style::LineNumber,
);
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
match &highlight_parts[..] {
[SubstitutionHighlight { start: 0, end }] if *end == line.len() => {
[SubstitutionHighlight { start: 0, end }] if *end == line_to_add.len() => {
buffer.puts(*row_num, max_line_num_len + 1, "+ ", Style::Addition);
}
[] => {
@ -2249,24 +2229,17 @@ fn draw_code_line(
buffer.puts(*row_num, max_line_num_len + 1, "~ ", Style::Addition);
}
}
// print the suggestion
buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle);
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
} else {
buffer.puts(
*row_num,
0,
&self.maybe_anonymized(line_start + line_pos),
Style::LineNumber,
);
buffer.puts(*row_num, 0, &self.maybe_anonymized(line_num), Style::LineNumber);
draw_col_separator(buffer, *row_num, max_line_num_len + 1);
// print the suggestion
buffer.append(*row_num, &normalize_whitespace(line), Style::NoStyle);
buffer.append(*row_num, &normalize_whitespace(line_to_add), Style::NoStyle);
}
// Colorize addition/replacements with green.
for &SubstitutionHighlight { start, end } in highlight_parts {
// Account for tabs when highlighting (#87972).
let tabs: usize = line
let tabs: usize = line_to_add
.chars()
.take(start)
.map(|ch| match ch {