Skip to content

Commit

Permalink
fix(graphical): fix issue with duplicate labels when span len is 0 (#159
Browse files Browse the repository at this point in the history
)

Fixes: #130
  • Loading branch information
zkat committed Apr 23, 2022
1 parent 084ed13 commit 1a36fa7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/handlers/graphical.rs
Expand Up @@ -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
Expand Down
32 changes: 32 additions & 0 deletions tests/graphical.rs
Expand Up @@ -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);
}

0 comments on commit 1a36fa7

Please sign in to comment.