Skip to content

Commit a8b61d7

Browse files
authored
Merge pull request #385 from Muscraft/unifi-single-multi
refactor: Unify single-line and multi-line removal highlighting in suggestions
2 parents c98dfa8 + eb068a3 commit a8b61d7

1 file changed

Lines changed: 40 additions & 38 deletions

File tree

src/renderer/render.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,62 +1707,64 @@ fn emit_suggestion_default(
17071707
// | <- row_num
17081708

17091709
let newlines = snippet.lines().count();
1710-
if newlines > 0 && row_num > newlines {
1711-
let offset = match prev_lines {
1712-
Some((start, end)) => {
1713-
file_lines.len().saturating_sub(end.saturating_sub(start))
1710+
let offset = match prev_lines {
1711+
Some((start, end)) => {
1712+
file_lines.len().saturating_sub(end.saturating_sub(start))
1713+
}
1714+
None => file_lines.len(),
1715+
};
1716+
// FIXME: We check the number of rows because in some cases, like in
1717+
// `tests/ui/lint/invalid-nan-comparison-suggestion.rs`, the rendered
1718+
// suggestion will only show the first line of code being replaced. The
1719+
// proper way of doing this would be to change the suggestion rendering
1720+
// logic to show the whole prior snippet, but the current output is not
1721+
// too bad to begin with, so we side-step that issue here.
1722+
for (i, line) in snippet.lines().enumerate() {
1723+
let norm_line = normalize_whitespace(line);
1724+
// Going lower than buffer_offset (+ 1) would mean
1725+
// overwriting existing content in the buffer
1726+
let min_row = buffer_offset + usize::from(!matches_previous_suggestion);
1727+
let row = (row_num - 2 - (offset - i - 1)).max(min_row);
1728+
let (start, end) = match i {
1729+
0 if span_start.line == span_end.line => {
1730+
// If the removed code fits all in one line, highlight between the
1731+
// start and end columns of the part span.
1732+
let full_line = sm.get_line(span_start.line).unwrap_or_default();
1733+
// We calculate the extra width from tabs for both the start and end of
1734+
// the span, as tabs could be present in the middle of the span
1735+
(
1736+
span_start.char + extra_width_from_tabs(full_line, span_start.char),
1737+
span_end.char + extra_width_from_tabs(full_line, span_end.char),
1738+
)
17141739
}
1715-
None => file_lines.len(),
1716-
};
1717-
// Account for removals where the part being removed spans multiple
1718-
// lines.
1719-
// FIXME: We check the number of rows because in some cases, like in
1720-
// `tests/ui/lint/invalid-nan-comparison-suggestion.rs`, the rendered
1721-
// suggestion will only show the first line of code being replaced. The
1722-
// proper way of doing this would be to change the suggestion rendering
1723-
// logic to show the whole prior snippet, but the current output is not
1724-
// too bad to begin with, so we side-step that issue here.
1725-
for (i, line) in snippet.lines().enumerate() {
1726-
let norm_line = normalize_whitespace(line);
1727-
// Going lower than buffer_offset (+ 1) would mean
1728-
// overwriting existing content in the buffer
1729-
let min_row = buffer_offset + usize::from(!matches_previous_suggestion);
1730-
let row = (row_num - 2 - (offset - i - 1)).max(min_row);
1731-
let (start, end) = if i == 0 {
1740+
0 => {
17321741
// On the first line, we highlight between the start of the part
17331742
// span, and the end of that line.
17341743
let full_line = sm.get_line(span_start.line).unwrap_or_default();
17351744
let extra_width = extra_width_from_tabs(full_line, span_start.char);
17361745
let start = span_start.char + extra_width;
17371746
(start, start + norm_line.chars().count())
1738-
} else if i == newlines - 1 {
1747+
}
1748+
x if x == newlines - 1 => {
17391749
// On the last line, we highlight between the start of the line, and
17401750
// the column of the part span end.
17411751
let extra_width = extra_width_from_tabs(line, span_end.char);
17421752
(0, span_end.char + extra_width)
1743-
} else {
1753+
}
1754+
_ => {
17441755
// On all others, we highlight the whole line.
17451756
(0, norm_line.chars().count())
1746-
};
1747-
buffer.set_style_range(
1748-
row,
1749-
padding + start,
1750-
padding + end,
1751-
ElementStyle::Removal,
1752-
true,
1753-
);
1754-
}
1755-
} else {
1756-
let extra_width: usize = extra_width_from_tabs(snippet, span_start.char);
1757-
// The removed code fits all in one line.
1757+
}
1758+
};
17581759
buffer.set_style_range(
1759-
row_num - 2,
1760-
span_start.char + extra_width,
1761-
span_end.char + extra_width,
1760+
row,
1761+
padding + start,
1762+
padding + end,
17621763
ElementStyle::Removal,
17631764
true,
17641765
);
17651766
}
1767+
17661768
prev_lines = Some((span_start.line, span_end.line));
17671769
}
17681770

0 commit comments

Comments
 (0)