From 24538cdecef2a0bfd3d409d6a957e119d21cac27 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Wed, 9 Oct 2024 23:13:09 -0700 Subject: [PATCH] Use ariadne https://github.com/casey/just/issues/1323 --- src/compile_error.rs | 20 ++++++++++++++++++++ src/error.rs | 7 +++++++ src/run.rs | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/compile_error.rs b/src/compile_error.rs index 8b4ec8ded6..b201f3a8f2 100644 --- a/src/compile_error.rs +++ b/src/compile_error.rs @@ -19,6 +19,26 @@ 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 = format!("{}", token.path.display()); + let label = Label::new((&path, start..end)); + + let report = Report::build(ReportKind::Custom("error", Color::Red), &path, start); + + let message = format!("{error}"); + let report = report.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) })