Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions src/renderer/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1721,6 +1721,14 @@ fn emit_suggestion_default(
// logic to show the whole prior snippet, but the current output is not
// too bad to begin with, so we side-step that issue here.
for (i, line) in snippet.lines().enumerate() {
let tabs: usize = line
.chars()
.take(span_start.char)
.map(|ch| match ch {
'\t' => 3,
_ => 0,
})
.sum();
let line = normalize_whitespace(line);
// Going lower than buffer_offset (+ 1) would mean
// overwriting existing content in the buffer
Expand All @@ -1732,26 +1740,36 @@ fn emit_suggestion_default(
// the column of the part span end.
// On all others, we highlight the whole line.
let start = if i == 0 {
(padding as isize + span_start_pos as isize) as usize
(padding as isize + (span_start.char + tabs) as isize) as usize
} else {
padding
};
let end = if i == 0 {
(padding as isize + span_start_pos as isize + line.len() as isize)
(padding as isize
+ (span_start.char + tabs) as isize
+ line.chars().count() as isize)
as usize
} else if i == newlines - 1 {
(padding as isize + span_end_pos as isize) as usize
(padding as isize + (span_end.char + tabs) as isize) as usize
} else {
(padding as isize + line.len() as isize) as usize
(padding as isize + line.chars().count() as isize) as usize
};
buffer.set_style_range(row, start, end, ElementStyle::Removal, true);
}
} else {
let tabs: usize = snippet
.chars()
.take(span_start.char)
.map(|ch| match ch {
'\t' => 3,
_ => 0,
})
.sum();
// The removed code fits all in one line.
buffer.set_style_range(
row_num - 2,
(padding as isize + span_start_pos as isize) as usize,
(padding as isize + span_end_pos as isize) as usize,
(padding as isize + (span_start.char + tabs) as isize) as usize,
(padding as isize + (span_end.char + tabs) as isize) as usize,
ElementStyle::Removal,
true,
);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions tests/color/highlight_diff_line_with_wide_characters.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use annotate_snippets::{renderer::DecorStyle, AnnotationKind, Level, Patch, Renderer, Snippet};

use snapbox::{assert_data_eq, file};

#[test]
fn case() {
let source = r#"struct 啊啊啊啊 {}

const 哦哦: 啊啊啊啊 = 哈哈哈哈 {}; // some comment"#;

let path = "$DIR/highlight_diff_line_with_wide_characters.rs";

let report = &[
Level::ERROR
.primary_title("cannot find struct, variant or union type `哈哈哈哈` in this scope")
.id("E0422")
.element(
Snippet::source(source)
.path(path)
.annotation(AnnotationKind::Primary.span(53..65).label("here"))
.annotation(
AnnotationKind::Context
.span(0..22)
.label("similarly named struct `啊啊啊啊` defined here"),
),
),
Level::HELP
.secondary_title("a struct with a similar name exists: `啊啊啊啊`")
.element(
Snippet::source(source)
.path(path)
.patch(Patch::new(53..65, "啊啊啊啊")),
),
];

let expected_ascii = file!["highlight_diff_line_with_wide_characters.ascii.term.svg": TermSvg];
let renderer = Renderer::styled();
assert_data_eq!(renderer.render(report), expected_ascii);

let expected_unicode =
file!["highlight_diff_line_with_wide_characters.unicode.term.svg": TermSvg];
let renderer = renderer.decor_style(DecorStyle::Unicode);
assert_data_eq!(renderer.render(report), expected_unicode);
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/color/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod fold_ann_multiline;
mod fold_bad_origin_line;
mod fold_leading;
mod fold_trailing;
mod highlight_diff_line_with_wide_characters;
mod highlight_duplicated_diff_lines;
mod highlight_source;
mod issue_9;
Expand Down