fix(printer): clamp highlight length to at least 1 (#32)

Fixes: https://github.com/zkat/miette/issues/31
This commit is contained in:
Tyler Bindon 2021-08-22 19:09:19 -07:00 committed by Kat Marchán
parent 61283e9efe
commit 9d6015996b
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
2 changed files with 49 additions and 5 deletions

View File

@ -382,12 +382,13 @@ impl GraphicalReportPrinter {
let mut highest = 0;
let chars = &self.theme.characters;
for hl in single_liners {
let hl_len = std::cmp::max(1, hl.len());
let local_offset = hl.offset() - line.offset;
let vbar_offset = local_offset + (hl.len() / 2);
let vbar_offset = local_offset + (hl_len / 2);
let num_left = vbar_offset - local_offset;
let num_right = local_offset + hl.len() - vbar_offset - 1;
let num_right = local_offset + hl_len - vbar_offset - 1;
let start = std::cmp::max(local_offset, highest);
let end = local_offset + hl.len();
let end = local_offset + hl_len;
if start < end {
underlines.push_str(
&format!(
@ -414,9 +415,10 @@ impl GraphicalReportPrinter {
if let Some(label) = hl.label() {
self.write_no_linum(f, linum_width)?;
self.render_highlight_gutter(f, max_gutter, line, all_highlights)?;
let hl_len = std::cmp::max(1, hl.len());
let local_offset = hl.offset() - line.offset;
let vbar_offset = local_offset + (hl.len() / 2);
let num_right = local_offset + hl.len() - vbar_offset - 1;
let vbar_offset = local_offset + (hl_len / 2);
let num_right = local_offset + hl_len - vbar_offset - 1;
let lines = format!(
"{:width$}{}{} {}",
" ",

View File

@ -65,6 +65,48 @@ fn single_line_highlight() -> Result<(), MietteError> {
Ok(())
}
#[test]
fn single_line_highlight_with_empty_span() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]
#[error("oops!")]
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
struct MyBad {
src: String,
#[snippet(src, "This is the part that broke")]
ctx: SourceSpan,
#[highlight(ctx)]
highlight: SourceSpan,
}
let src = "source\n text\n here".to_string();
let len = src.len();
let err = MyBad {
src,
ctx: ("bad_file.rs", 0, len).into(),
highlight: ("this bit here", 9, 0).into(),
};
let out = fmt_report(err.into());
println!("{}", out);
let expected = r#"
[oops::my::bad]
× oops!
[bad_file.rs:1:1] This is the part that broke:
1 source
2 text
·
· this bit here
3 here
try doing it better next time?
"#
.trim_start()
.to_string();
assert_eq!(expected, out);
Ok(())
}
#[test]
fn single_line_highlight_no_label() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]