Skip to content

Commit

Permalink
fix(graphical): handle an empty source (#183)
Browse files Browse the repository at this point in the history
In some cases the source can be completely empty -- handle that in a reasonable fashion.
  • Loading branch information
sunshowers authored Jul 9, 2022
1 parent ccf1b8a commit 12dc400
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,9 @@ impl GraphicalReportHandler {
// numbers need!
let linum_width = lines[..]
.last()
.expect("get_lines should always return at least one line?")
.line_number
.map(|line| line.line_number)
// It's possible for the source to be an empty string.
.unwrap_or(0)
.to_string()
.len();

Expand All @@ -402,7 +403,7 @@ impl GraphicalReportHandler {
contents.line() + 1,
contents.column() + 1
)?;
} else if lines.len() == 1 {
} else if lines.len() <= 1 {
writeln!(f, "{}", self.theme.characters.hbar.to_string().repeat(3))?;
} else {
writeln!(f, "[{}:{}]", contents.line() + 1, contents.column() + 1)?;
Expand Down
33 changes: 33 additions & 0 deletions tests/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,39 @@ fn fmt_report(diag: Report) -> String {
out
}

#[test]
fn empty_source() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]
#[error("oops!")]
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
struct MyBad {
#[source_code]
src: NamedSource,
#[label("this bit here")]
highlight: SourceSpan,
}

let src = "".to_string();
let err = MyBad {
src: NamedSource::new("bad_file.rs", src),
highlight: (0, 0).into(),
};
let out = fmt_report(err.into());
println!("Error: {}", out);
// For an empty string, the label cannot be rendered.
let expected = r#"oops::my::bad
× oops!
╭─[bad_file.rs:1:1]
╰────
help: try doing it better next time?
"#
.trim_start()
.to_string();
assert_eq!(expected, out);
Ok(())
}

#[test]
fn single_line_with_wide_char() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)]
Expand Down

0 comments on commit 12dc400

Please sign in to comment.