diff --git a/src/compile_error.rs b/src/compile_error.rs index 8b4ec8ded6..4a4b9ee43b 100644 --- a/src/compile_error.rs +++ b/src/compile_error.rs @@ -19,6 +19,28 @@ impl<'src> CompileError<'src> { } } +pub(crate) fn render_compile_error(error: &CompileError) { + use ariadne::{Color, Label, Report, ReportKind, Source}; + + let token = error.token; + let source = Source::from(token.src); + + let start = token.offset; + let end = token.offset + token.length; + + let path = token.path.display().to_string(); + let label = Label::new((&path, start..end)); + + let message = error.to_string(); + + let report = Report::build(ReportKind::Custom("error", Color::Red), &path, start) + .with_message(message) + .with_label(label) + .finish(); + + report.eprint((&path, source)).unwrap(); +} + fn capitalize(s: &str) -> String { let mut chars = s.chars(); match chars.next() { diff --git a/src/error.rs b/src/error.rs index dd6955fe41..03f75794ab 100644 --- a/src/error.rs +++ b/src/error.rs @@ -504,6 +504,13 @@ impl<'src> ColorDisplay for Error<'src> { } } +pub(crate) fn render_error(error: &Error, color: Color) { + match error { + Error::Compile { compile_error } => compile_error::render_compile_error(compile_error), + _ => eprintln!("{}", error.color_display(color.stderr())), + } +} + fn format_cmd(binary: &OsString, arguments: &Vec) -> String { iter::once(binary) .chain(arguments) diff --git a/src/run.rs b/src/run.rs index a07b73bbc9..c17ba50236 100644 --- a/src/run.rs +++ b/src/run.rs @@ -29,7 +29,7 @@ pub fn run(args: impl Iterator + Clone>) -> Result<() }) .map_err(|error| { if !verbosity.quiet() && error.print_message() { - eprintln!("{}", error.color_display(color.stderr())); + crate::error::render_error(&error, color); } error.code().unwrap_or(EXIT_FAILURE) })