Skip to content

Commit 29cfa73

Browse files
committed
Display annotation correctly for double width characters across lines
1 parent ac31f8a commit 29cfa73

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/display_list/from_snippet.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,11 @@ fn format_body(
409409
});
410410
}
411411
} else {
412-
let range = (start - line_start_index, start - line_start_index + 1);
412+
let annotation_start_col = char_widths
413+
.iter()
414+
.take(start - line_start_index)
415+
.sum::<usize>();
416+
let range = (annotation_start_col, annotation_start_col + 1);
413417
body.insert(
414418
body_idx + 1,
415419
DisplayLine::Source {
@@ -466,7 +470,11 @@ fn format_body(
466470
});
467471
}
468472

469-
let end_mark = (end - line_start_index).saturating_sub(1);
473+
let end_mark = char_widths
474+
.iter()
475+
.take(end - line_start_index)
476+
.sum::<usize>()
477+
.saturating_sub(1);
470478
let range = (end_mark - margin_left, (end_mark + 1) - margin_left);
471479
body.insert(
472480
body_idx + 1,

tests/formatter.rs

+30
Original file line numberDiff line numberDiff line change
@@ -578,3 +578,33 @@ fn test_point_to_double_width_characters() {
578578

579579
assert_eq!(DisplayList::from(snippets).to_string(), expected);
580580
}
581+
582+
#[test]
583+
fn test_point_to_double_width_characters_across_lines() {
584+
let snippets = Snippet {
585+
slices: vec![snippet::Slice {
586+
source: "おはよう\nございます",
587+
line_start: 1,
588+
origin: Some("<current file>"),
589+
annotations: vec![snippet::SourceAnnotation {
590+
range: (2, 8),
591+
label: "Good morning",
592+
annotation_type: snippet::AnnotationType::Error,
593+
}],
594+
fold: false,
595+
}],
596+
title: None,
597+
footer: vec![],
598+
opt: Default::default(),
599+
};
600+
601+
let expected = r#" --> <current file>:1:3
602+
|
603+
1 | おはよう
604+
| _____^
605+
2 | | ございます
606+
| |______^ Good morning
607+
|"#;
608+
609+
assert_eq!(DisplayList::from(snippets).to_string(), expected);
610+
}

0 commit comments

Comments
 (0)