Skip to content

Commit 31c90cb

Browse files
authored
Merge pull request #318 from Muscraft/fix-highlight-duplicated-diff
Fix highlight duplicated diff
2 parents ecbe58b + 474d3a5 commit 31c90cb

12 files changed

+778
-1
lines changed

src/renderer/render.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1774,7 +1774,10 @@ fn emit_suggestion_default(
17741774
// too bad to begin with, so we side-step that issue here.
17751775
for (i, line) in snippet.lines().enumerate() {
17761776
let line = normalize_whitespace(line);
1777-
let row = row_num - 2 - (newlines - i - 1);
1777+
// Going lower than buffer_offset (+ 1) would mean
1778+
// overwriting existing content in the buffer
1779+
let min_row = buffer_offset + usize::from(!matches_previous_suggestion);
1780+
let row = (row_num - 2 - (newlines - i - 1)).max(min_row);
17781781
// On the first line, we highlight between the start of the part
17791782
// span, and the end of that line.
17801783
// On the last line, we highlight between the start of the line, and
Lines changed: 62 additions & 0 deletions
Loading
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Level, Patch, Renderer, Snippet};
2+
3+
use snapbox::{assert_data_eq, file};
4+
5+
#[test]
6+
fn case() {
7+
// https://github.com/rust-lang/rust/blob/4b94758d2ba7d0ef71ccf5fde29ce4bc5d6fe2a4/tests/ui/argument-suggestions/wrong-highlight-span-extra-arguments-147070.rs
8+
9+
let source = r#"struct Thingie;
10+
11+
impl Thingie {
12+
pub(crate) fn new(
13+
_a: String,
14+
_b: String,
15+
_c: String,
16+
_d: String,
17+
_e: String,
18+
_f: String,
19+
) -> Self {
20+
unimplemented!()
21+
}
22+
}
23+
24+
fn main() {
25+
let foo = Thingie::new(
26+
String::from(""),
27+
String::from(""),
28+
String::from(""),
29+
String::from(""),
30+
String::from(""),
31+
String::from(""),
32+
String::from(""),
33+
);
34+
}"#;
35+
36+
let path = "$DIR/wrong-highlight-span-extra-arguments-147070.rs";
37+
38+
let report = &[
39+
Level::ERROR
40+
.primary_title("this function takes 6 arguments but 7 arguments were supplied")
41+
.id("E0061")
42+
.element(
43+
Snippet::source(source)
44+
.path(path)
45+
.annotation(
46+
AnnotationKind::Context
47+
.span(429..445)
48+
.label("unexpected argument #7 of type `String`"),
49+
)
50+
.annotation(AnnotationKind::Primary.span(251..263)),
51+
),
52+
Level::NOTE
53+
.secondary_title("associated function defined here")
54+
.element(
55+
Snippet::source(source)
56+
.path(path)
57+
.annotation(AnnotationKind::Primary.span(50..53)),
58+
),
59+
Level::HELP
60+
.secondary_title("remove the extra argument")
61+
.element(
62+
Snippet::source(source)
63+
.path(path)
64+
.patch(Patch::new(419..445, "")),
65+
),
66+
];
67+
68+
let expected_ascii = file!["highlight_duplicated_diff_lines.ascii.term.svg": TermSvg];
69+
let renderer = Renderer::styled();
70+
assert_data_eq!(renderer.render(report), expected_ascii);
71+
72+
let expected_unicode = file!["highlight_duplicated_diff_lines.unicode.term.svg": TermSvg];
73+
let renderer = renderer.decor_style(DecorStyle::Unicode);
74+
assert_data_eq!(renderer.render(report), expected_unicode);
75+
}
Lines changed: 62 additions & 0 deletions
Loading

tests/color/main.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,13 @@ mod fold_ann_multiline;
99
mod fold_bad_origin_line;
1010
mod fold_leading;
1111
mod fold_trailing;
12+
mod highlight_duplicated_diff_lines;
1213
mod highlight_source;
1314
mod issue_9;
1415
mod multiline_removal_suggestion;
1516
mod multiple_annotations;
17+
mod multiple_highlight_duplicated;
18+
mod multiple_multiline_removal;
1619
mod primary_title_second_group;
1720
mod simple;
1821
mod strip_line;
Lines changed: 78 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)