Skip to content

Commit 6557d93

Browse files
committed
ruff_annotate_snippets: fix false positive line trimming
This fix was sent upstream and the PR description includes more details: rust-lang/annotate-snippets-rs#170 Without this fix, there was an errant snapshot diff that looked like this: | 1 | version = "0.1.0" 2 | # Ensure that the spans from toml handle utf-8 correctly 3 | authors = [ | ___________^ 4 | | { name = "Z...ALGO", email = 1 } 5 | | ] | |_^ RUF200 | That ellipsis should _not_ be inserted since the line is not actually truncated. The handling of line length (in bytes versus actual rendered length) wasn't quite being handled correctly in all cases. With this fix, there's (correctly) no snapshot diff.
1 parent 8ce00fe commit 6557d93

File tree

2 files changed

+16
-29
lines changed

2 files changed

+16
-29
lines changed

crates/ruff_annotate_snippets/src/renderer/display_list.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -331,28 +331,27 @@ impl DisplaySet<'_> {
331331

332332
// On long lines, we strip the source line, accounting for unicode.
333333
let mut taken = 0;
334-
let code: String = text
335-
.chars()
336-
.skip(left)
337-
.take_while(|ch| {
338-
// Make sure that the trimming on the right will fall within the terminal width.
339-
// FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char`
340-
// is. For now, just accept that sometimes the code line will be longer than
341-
// desired.
342-
let next = unicode_width::UnicodeWidthChar::width(*ch).unwrap_or(1);
343-
if taken + next > right - left {
344-
return false;
345-
}
346-
taken += next;
347-
true
348-
})
349-
.collect();
334+
let mut was_cut_right = false;
335+
let mut code = String::new();
336+
for ch in text.chars().skip(left) {
337+
// Make sure that the trimming on the right will fall within the terminal width.
338+
// FIXME: `unicode_width` sometimes disagrees with terminals on how wide a `char`
339+
// is. For now, just accept that sometimes the code line will be longer than
340+
// desired.
341+
let next = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1);
342+
if taken + next > right - left {
343+
was_cut_right = true;
344+
break;
345+
}
346+
taken += next;
347+
code.push(ch);
348+
}
350349
buffer.puts(line_offset, code_offset, &code, Style::new());
351350
if self.margin.was_cut_left() {
352351
// We have stripped some code/whitespace from the beginning, make it clear.
353352
buffer.puts(line_offset, code_offset, "...", *lineno_color);
354353
}
355-
if self.margin.was_cut_right(line_len) {
354+
if was_cut_right {
356355
buffer.puts(line_offset, code_offset + taken - 3, "...", *lineno_color);
357356
}
358357

crates/ruff_annotate_snippets/src/renderer/margin.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,18 +58,6 @@ impl Margin {
5858
self.computed_left > 0
5959
}
6060

61-
pub(crate) fn was_cut_right(&self, line_len: usize) -> bool {
62-
let right =
63-
if self.computed_right == self.span_right || self.computed_right == self.label_right {
64-
// Account for the "..." padding given above. Otherwise we end up with code lines that
65-
// do fit but end in "..." as if they were trimmed.
66-
self.computed_right - ELLIPSIS_PASSING
67-
} else {
68-
self.computed_right
69-
};
70-
right < line_len && self.computed_left + self.term_width < line_len
71-
}
72-
7361
fn compute(&mut self, max_line_len: usize) {
7462
// When there's a lot of whitespace (>20), we want to trim it as it is useless.
7563
self.computed_left = if self.whitespace_left > LONG_WHITESPACE {

0 commit comments

Comments
 (0)