mirror of https://github.com/zkat/miette.git
fix(printer): clamp highlight length to at least 1 (#32)
Fixes: https://github.com/zkat/miette/issues/31
This commit is contained in:
parent
61283e9efe
commit
9d6015996b
|
|
@ -382,12 +382,13 @@ impl GraphicalReportPrinter {
|
||||||
let mut highest = 0;
|
let mut highest = 0;
|
||||||
let chars = &self.theme.characters;
|
let chars = &self.theme.characters;
|
||||||
for hl in single_liners {
|
for hl in single_liners {
|
||||||
|
let hl_len = std::cmp::max(1, hl.len());
|
||||||
let local_offset = hl.offset() - line.offset;
|
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_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 start = std::cmp::max(local_offset, highest);
|
||||||
let end = local_offset + hl.len();
|
let end = local_offset + hl_len;
|
||||||
if start < end {
|
if start < end {
|
||||||
underlines.push_str(
|
underlines.push_str(
|
||||||
&format!(
|
&format!(
|
||||||
|
|
@ -414,9 +415,10 @@ impl GraphicalReportPrinter {
|
||||||
if let Some(label) = hl.label() {
|
if let Some(label) = hl.label() {
|
||||||
self.write_no_linum(f, linum_width)?;
|
self.write_no_linum(f, linum_width)?;
|
||||||
self.render_highlight_gutter(f, max_gutter, line, all_highlights)?;
|
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 local_offset = hl.offset() - line.offset;
|
||||||
let vbar_offset = local_offset + (hl.len() / 2);
|
let vbar_offset = local_offset + (hl_len / 2);
|
||||||
let num_right = local_offset + hl.len() - vbar_offset - 1;
|
let num_right = local_offset + hl_len - vbar_offset - 1;
|
||||||
let lines = format!(
|
let lines = format!(
|
||||||
"{:width$}{}{} {}",
|
"{:width$}{}{} {}",
|
||||||
" ",
|
" ",
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,48 @@ fn single_line_highlight() -> Result<(), MietteError> {
|
||||||
Ok(())
|
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]
|
#[test]
|
||||||
fn single_line_highlight_no_label() -> Result<(), MietteError> {
|
fn single_line_highlight_no_label() -> Result<(), MietteError> {
|
||||||
#[derive(Debug, Diagnostic, Error)]
|
#[derive(Debug, Diagnostic, Error)]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue