move expect panic formatting to reporting crate

This commit is contained in:
Folkert 2022-07-26 18:33:43 +02:00
parent f011748526
commit 7014fbb5ec
No known key found for this signature in database
GPG Key ID: 1F17F6FFD112B97C
2 changed files with 39 additions and 37 deletions

View File

@ -1091,53 +1091,23 @@ unsafe fn roc_run_native_debug(
}
fn render_expect_panic<'a>(
_arena: &'a Bump,
arena: &'a Bump,
expect: ToplevelExpect,
message: &str,
expectations: &mut VecMap<ModuleId, Expectations>,
interns: &'a Interns,
) {
use roc_reporting::report::Report;
use roc_reporting::report::RocDocAllocator;
use ven_pretty::DocAllocator;
let module_id = expect.symbol.module_id();
let data = expectations.get_mut(&module_id).unwrap();
// TODO cache these line offsets?
let path = &data.path;
let filename = data.path.to_owned();
let file_string = std::fs::read_to_string(path).unwrap();
let src_lines: Vec<_> = file_string.lines().collect();
let source = std::fs::read_to_string(path).unwrap();
let line_info = roc_region::all::LineInfo::new(&file_string);
let line_col_region = line_info.convert_region(expect.region);
let alloc = RocDocAllocator::new(&src_lines, module_id, interns);
let doc = alloc.stack([
alloc.text("This expectation crashed while running:"),
alloc.region(line_col_region),
alloc.text("The crash reported this message:"),
alloc.text(message),
]);
let report = Report {
title: "EXPECT FAILED".into(),
doc,
filename,
severity: roc_reporting::report::Severity::RuntimeError,
};
let mut buf = String::new();
report.render(
roc_reporting::report::RenderTarget::ColorTerminal,
&mut buf,
&alloc,
&roc_reporting::report::DEFAULT_PALETTE,
);
use roc_reporting::error::expect::Renderer;
let renderer = Renderer::new(arena, interns, module_id, filename, &source);
let buf = renderer.render_panic(message, expect.region);
println!("{}", buf);
}
@ -1209,7 +1179,7 @@ fn render_expect_failure<'a>(
use roc_reporting::error::expect::Renderer;
let renderer = Renderer::new(arena, interns, module_id, filename, &source);
let buf = renderer.render(
let buf = renderer.render_failure(
subs,
&symbols,
&variables,

View File

@ -121,7 +121,7 @@ impl<'a> Renderer<'a> {
self.line_info.convert_region(display_region)
}
pub fn render(
pub fn render_failure(
&self,
subs: &mut Subs,
symbols: &[Symbol],
@ -153,4 +153,36 @@ impl<'a> Renderer<'a> {
buf
}
pub fn render_panic(&self, message: &str, expect_region: Region) -> String {
use crate::report::Report;
use ven_pretty::DocAllocator;
let line_col_region = self.line_info.convert_region(expect_region);
let doc = self.alloc.stack([
self.alloc.text("This expectation crashed while running:"),
self.alloc.region(line_col_region),
self.alloc.text("The crash reported this message:"),
self.alloc.text(message),
]);
let report = Report {
title: "EXPECT PANICKED".into(),
doc,
filename: self.filename.clone(),
severity: crate::report::Severity::RuntimeError,
};
let mut buf = String::new();
report.render(
crate::report::RenderTarget::ColorTerminal,
&mut buf,
&self.alloc,
&crate::report::DEFAULT_PALETTE,
);
buf
}
}