Skip to content

Commit a8b4ae0

Browse files
authoredNov 2, 2023
fix(graphical): Extend error text span to whole code points (#312)
Fixes: #223 This fixes a panic when an error starts inside a Unicode code point. The range is extended to start (or end) at the beginning (or end) of the character inside which the byte offset is located.
1 parent d37ada8 commit a8b4ae0

File tree

2 files changed

+89
-4
lines changed

2 files changed

+89
-4
lines changed
 

‎src/handlers/graphical.rs

+15-4
Original file line numberDiff line numberDiff line change
@@ -651,11 +651,22 @@ impl GraphicalReportHandler {
651651
}
652652

653653
/// Returns the visual column position of a byte offset on a specific line.
654-
fn visual_offset(&self, line: &Line, offset: usize) -> usize {
654+
///
655+
/// If the offset occurs in the middle of a character, the returned column
656+
/// corresponds to that character's first column in `start` is true, or its
657+
/// last column if `start` is false.
658+
fn visual_offset(&self, line: &Line, offset: usize, start: bool) -> usize {
655659
let line_range = line.offset..=(line.offset + line.length);
656660
assert!(line_range.contains(&offset));
657661

658-
let text_index = offset - line.offset;
662+
let mut text_index = offset - line.offset;
663+
while text_index <= line.text.len() && !line.text.is_char_boundary(text_index) {
664+
if start {
665+
text_index -= 1;
666+
} else {
667+
text_index += 1;
668+
}
669+
}
659670
let text = &line.text[..text_index.min(line.text.len())];
660671
let text_width = self.line_visual_char_width(text).sum();
661672
if text_index > line.text.len() {
@@ -706,8 +717,8 @@ impl GraphicalReportHandler {
706717
.map(|hl| {
707718
let byte_start = hl.offset();
708719
let byte_end = hl.offset() + hl.len();
709-
let start = self.visual_offset(line, byte_start).max(highest);
710-
let end = self.visual_offset(line, byte_end).max(start + 1);
720+
let start = self.visual_offset(line, byte_start, true).max(highest);
721+
let end = self.visual_offset(line, byte_end, false).max(start + 1);
711722

712723
let vbar_offset = (start + end) / 2;
713724
let num_left = vbar_offset - start;

‎tests/graphical.rs

+74
Original file line numberDiff line numberDiff line change
@@ -1247,3 +1247,77 @@ fn primary_label() {
12471247

12481248
assert_eq!(expected, out);
12491249
}
1250+
1251+
#[test]
1252+
fn single_line_with_wide_char_unaligned_span_start() -> Result<(), MietteError> {
1253+
#[derive(Debug, Diagnostic, Error)]
1254+
#[error("oops!")]
1255+
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
1256+
struct MyBad {
1257+
#[source_code]
1258+
src: NamedSource,
1259+
#[label("this bit here")]
1260+
highlight: SourceSpan,
1261+
}
1262+
1263+
let src = "source\n 👼🏼text\n here".to_string();
1264+
let err = MyBad {
1265+
src: NamedSource::new("bad_file.rs", src),
1266+
highlight: (10, 5).into(),
1267+
};
1268+
let out = fmt_report(err.into());
1269+
println!("Error: {}", out);
1270+
let expected = r#"oops::my::bad
1271+
1272+
× oops!
1273+
╭─[bad_file.rs:2:4]
1274+
1 │ source
1275+
2 │ 👼🏼text
1276+
· ──┬─
1277+
· ╰── this bit here
1278+
3 │ here
1279+
╰────
1280+
help: try doing it better next time?
1281+
"#
1282+
.trim_start()
1283+
.to_string();
1284+
assert_eq!(expected, out);
1285+
Ok(())
1286+
}
1287+
1288+
#[test]
1289+
fn single_line_with_wide_char_unaligned_span_end() -> Result<(), MietteError> {
1290+
#[derive(Debug, Diagnostic, Error)]
1291+
#[error("oops!")]
1292+
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
1293+
struct MyBad {
1294+
#[source_code]
1295+
src: NamedSource,
1296+
#[label("this bit here")]
1297+
highlight: SourceSpan,
1298+
}
1299+
1300+
let src = "source\n text 👼🏼\n here".to_string();
1301+
let err = MyBad {
1302+
src: NamedSource::new("bad_file.rs", src),
1303+
highlight: (9, 6).into(),
1304+
};
1305+
let out = fmt_report(err.into());
1306+
println!("Error: {}", out);
1307+
let expected = r#"oops::my::bad
1308+
1309+
× oops!
1310+
╭─[bad_file.rs:2:3]
1311+
1 │ source
1312+
2 │ text 👼🏼
1313+
· ───┬───
1314+
· ╰── this bit here
1315+
3 │ here
1316+
╰────
1317+
help: try doing it better next time?
1318+
"#
1319+
.trim_start()
1320+
.to_string();
1321+
assert_eq!(expected, out);
1322+
Ok(())
1323+
}

0 commit comments

Comments
 (0)
Please sign in to comment.