From 1a36fa7ec80de77e910e04cdb902270970611b39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Fri, 22 Apr 2022 19:31:51 -0700 Subject: [PATCH] fix(graphical): fix issue with duplicate labels when span len is 0 (#159) Fixes: https://github.com/zkat/miette/issues/130 --- src/handlers/graphical.rs | 5 +++-- tests/graphical.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/handlers/graphical.rs b/src/handlers/graphical.rs index fd1000e..377922f 100644 --- a/src/handlers/graphical.rs +++ b/src/handlers/graphical.rs @@ -776,12 +776,13 @@ impl Line { } fn span_applies(&self, span: &FancySpan) -> bool { + let spanlen = if span.len() == 0 { 1 } else { span.len() }; // Span starts in this line (span.offset() >= self.offset && span.offset() < self.offset + self.length) // Span passes through this line - || (span.offset() < self.offset && span.offset() + span.len() > self.offset + self.length) //todo + || (span.offset() < self.offset && span.offset() + spanlen > self.offset + self.length) //todo // Span ends on this line - || (span.offset() + span.len() > self.offset && span.offset() + span.len() <= self.offset + self.length) + || (span.offset() + spanlen > self.offset && span.offset() + spanlen <= self.offset + self.length) } // A 'flyby' is a multi-line span that technically covers this line, but diff --git a/tests/graphical.rs b/tests/graphical.rs index 53c7d1d..594b3f4 100644 --- a/tests/graphical.rs +++ b/tests/graphical.rs @@ -881,3 +881,35 @@ Error: oops::my::bad assert_eq!(expected, out); Ok(()) } + +#[test] +fn zero_length_eol_span() { + #[derive(Error, Debug, Diagnostic)] + #[error("oops!")] + #[diagnostic(severity(Error))] + struct MyBad { + #[source_code] + src: NamedSource, + #[label("This bit here")] + bad_bit: SourceSpan, + } + let err = MyBad { + src: NamedSource::new("issue", "this is the first line\nthis is the second line"), + bad_bit: (23, 0).into(), + }; + let out = fmt_report(err.into()); + println!("Error: {}", out); + + let expected = r#" + × oops! + ╭─[issue:1:1] + 1 │ this is the first line + 2 │ this is the second line + · ▲ + · ╰── This bit here + ╰──── +"# + .to_string(); + + assert_eq!(expected, out); +}