Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix end of previous line wrongly being included in highlight #52

Merged
merged 1 commit into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ impl Line {

fn span_applies(&self, span: &FancySpan) -> bool {
// Span starts in this line
(span.offset() >= self.offset && span.offset() <= self.offset +self.length)
(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 ends on this line
Expand Down
44 changes: 44 additions & 0 deletions tests/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,50 @@ fn single_line_highlight_no_label() -> Result<(), MietteError> {
Ok(())
}


#[test]
fn single_line_highlight_at_line_start() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]
#[error("oops!")]
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
struct MyBad {
src: NamedSource,
#[snippet(src, message("This is the part that broke"))]
ctx: SourceSpan,
#[highlight(ctx, label = "this bit here")]
highlight: SourceSpan,
}

let src = "source\ntext\n here".to_string();
let len = src.len();
let err = MyBad {
src: NamedSource::new("bad_file.rs", src),
ctx: (0, len).into(),
highlight: (7, 4).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 multiple_same_line_highlights() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]
Expand Down