ruff_annotate_snippets: support overriding the "cut indicator"

We do this because `...` is valid Python, which makes it pretty likely
that some line trimming will lead to ambiguous output. So we add support
for overriding the cut indicator. This also requires changing some of
the alignment math, which was previously tightly coupled to `...`.

For Ruff, we go with `…` (`U+2026 HORIZONTAL ELLIPSIS`) for our cut
indicator.

For more details, see the patch sent to upstream:
https://github.com/rust-lang/annotate-snippets-rs/pull/172
This commit is contained in:
Andrew Gallant 2025-01-09 13:08:35 -05:00 committed by Andrew Gallant
parent a45f4de683
commit 84ba4ecaf5
5 changed files with 94 additions and 17 deletions

View file

@ -990,3 +990,42 @@ error: title
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn long_line_cut() {
let source = "abcd abcd abcd abcd abcd abcd abcd";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.line_start(1)
.annotation(Level::Error.span(0..4)),
);
let expected = str![[r#"
error
|
1 | abcd abcd a...
| ^^^^
|
"#]];
let renderer = Renderer::plain().term_width(18);
assert_data_eq!(renderer.render(input).to_string(), expected);
}
#[test]
fn long_line_cut_custom() {
let source = "abcd abcd abcd abcd abcd abcd abcd";
let input = Level::Error.title("").snippet(
Snippet::source(source)
.line_start(1)
.annotation(Level::Error.span(0..4)),
);
// This trims a little less because `…` is visually smaller than `...`.
let expected = str![[r#"
error
|
1 | abcd abcd abc
| ^^^^
|
"#]];
let renderer = Renderer::plain().term_width(18).cut_indicator("");
assert_data_eq!(renderer.render(input).to_string(), expected);
}