Skip to content

Commit 3d6f903

Browse files
authoredNov 9, 2023
fix(formatting): Fix formatting bug when an empty span is not aligned to a char boundary (#314)
Previous output looked like this: ---- single_line_with_wide_char_unaligned_span_empty stdout ---- Error: oops::my::bad × oops! ╭─[bad_file.rs:2:4] 1 │ source 2 │ 👼🏼text · ─▲ · ╰── this bit here 3 │ here ╰──── help: try doing it better next time? Note that the .max(start + 1) term is still necessary in the nonempty branch, since it's possible to have a nonempty span covering zero-width text. * remove uncessary if statement start > end in all cases.
1 parent a8b4ae0 commit 3d6f903

File tree

2 files changed

+60
-21
lines changed

2 files changed

+60
-21
lines changed
 

‎src/handlers/graphical.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -718,31 +718,33 @@ impl GraphicalReportHandler {
718718
let byte_start = hl.offset();
719719
let byte_end = hl.offset() + hl.len();
720720
let start = self.visual_offset(line, byte_start, true).max(highest);
721-
let end = self.visual_offset(line, byte_end, false).max(start + 1);
721+
let end = if hl.len() == 0 {
722+
start + 1
723+
} else {
724+
self.visual_offset(line, byte_end, false).max(start + 1)
725+
};
722726

723727
let vbar_offset = (start + end) / 2;
724728
let num_left = vbar_offset - start;
725729
let num_right = end - vbar_offset - 1;
726-
if start < end {
727-
underlines.push_str(
728-
&format!(
729-
"{:width$}{}{}{}",
730-
"",
731-
chars.underline.to_string().repeat(num_left),
732-
if hl.len() == 0 {
733-
chars.uarrow
734-
} else if hl.label().is_some() {
735-
chars.underbar
736-
} else {
737-
chars.underline
738-
},
739-
chars.underline.to_string().repeat(num_right),
740-
width = start.saturating_sub(highest),
741-
)
742-
.style(hl.style)
743-
.to_string(),
744-
);
745-
}
730+
underlines.push_str(
731+
&format!(
732+
"{:width$}{}{}{}",
733+
"",
734+
chars.underline.to_string().repeat(num_left),
735+
if hl.len() == 0 {
736+
chars.uarrow
737+
} else if hl.label().is_some() {
738+
chars.underbar
739+
} else {
740+
chars.underline
741+
},
742+
chars.underline.to_string().repeat(num_right),
743+
width = start.saturating_sub(highest),
744+
)
745+
.style(hl.style)
746+
.to_string(),
747+
);
746748
highest = std::cmp::max(highest, end);
747749

748750
(hl, vbar_offset)

‎tests/graphical.rs

+37
Original file line numberDiff line numberDiff line change
@@ -1321,3 +1321,40 @@ fn single_line_with_wide_char_unaligned_span_end() -> Result<(), MietteError> {
13211321
assert_eq!(expected, out);
13221322
Ok(())
13231323
}
1324+
1325+
#[test]
1326+
fn single_line_with_wide_char_unaligned_span_empty() -> Result<(), MietteError> {
1327+
#[derive(Debug, Diagnostic, Error)]
1328+
#[error("oops!")]
1329+
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
1330+
struct MyBad {
1331+
#[source_code]
1332+
src: NamedSource,
1333+
#[label("this bit here")]
1334+
highlight: SourceSpan,
1335+
}
1336+
1337+
let src = "source\n 👼🏼text\n here".to_string();
1338+
let err = MyBad {
1339+
src: NamedSource::new("bad_file.rs", src),
1340+
highlight: (10, 0).into(),
1341+
};
1342+
let out = fmt_report(err.into());
1343+
println!("Error: {}", out);
1344+
let expected = r#"oops::my::bad
1345+
1346+
× oops!
1347+
╭─[bad_file.rs:2:4]
1348+
1 │ source
1349+
2 │ 👼🏼text
1350+
· ▲
1351+
· ╰── this bit here
1352+
3 │ here
1353+
╰────
1354+
help: try doing it better next time?
1355+
"#
1356+
.trim_start()
1357+
.to_string();
1358+
assert_eq!(expected, out);
1359+
Ok(())
1360+
}

0 commit comments

Comments
 (0)
Please sign in to comment.