mirror of https://github.com/zkat/miette.git
fix(graphical): handle an empty source (#183)
In some cases the source can be completely empty -- handle that in a reasonable fashion.
This commit is contained in:
parent
ccf1b8ade5
commit
12dc40070a
|
|
@ -379,8 +379,9 @@ impl GraphicalReportHandler {
|
||||||
// numbers need!
|
// numbers need!
|
||||||
let linum_width = lines[..]
|
let linum_width = lines[..]
|
||||||
.last()
|
.last()
|
||||||
.expect("get_lines should always return at least one line?")
|
.map(|line| line.line_number)
|
||||||
.line_number
|
// It's possible for the source to be an empty string.
|
||||||
|
.unwrap_or(0)
|
||||||
.to_string()
|
.to_string()
|
||||||
.len();
|
.len();
|
||||||
|
|
||||||
|
|
@ -402,7 +403,7 @@ impl GraphicalReportHandler {
|
||||||
contents.line() + 1,
|
contents.line() + 1,
|
||||||
contents.column() + 1
|
contents.column() + 1
|
||||||
)?;
|
)?;
|
||||||
} else if lines.len() == 1 {
|
} else if lines.len() <= 1 {
|
||||||
writeln!(f, "{}", self.theme.characters.hbar.to_string().repeat(3))?;
|
writeln!(f, "{}", self.theme.characters.hbar.to_string().repeat(3))?;
|
||||||
} else {
|
} else {
|
||||||
writeln!(f, "[{}:{}]", contents.line() + 1, contents.column() + 1)?;
|
writeln!(f, "[{}:{}]", contents.line() + 1, contents.column() + 1)?;
|
||||||
|
|
|
||||||
|
|
@ -34,6 +34,39 @@ fn fmt_report(diag: Report) -> String {
|
||||||
out
|
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]
|
#[test]
|
||||||
fn single_line_with_wide_char() -> Result<(), MietteError> {
|
fn single_line_with_wide_char() -> Result<(), MietteError> {
|
||||||
#[derive(Debug, Diagnostic, Error)]
|
#[derive(Debug, Diagnostic, Error)]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue