From ee4f37080ee0e86e99d0e408583cf5a0bc445427 Mon Sep 17 00:00:00 2001 From: Jeff Date: Fri, 16 Feb 2024 17:28:57 -0500 Subject: [PATCH] Write function for lyneate integration --- src/error/mod.rs | 29 +++++++++++++++++++++++++++++ src/error/syntax_error.rs | 22 +--------------------- src/main.rs | 2 +- 3 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/error/mod.rs b/src/error/mod.rs index 912d8f3..ef49a1f 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -7,6 +7,7 @@ pub(crate) mod rw_lock_error; mod syntax_error; mod validation_error; +use lyneate::Report; pub use runtime_error::RuntimeError; pub use syntax_error::SyntaxError; pub use validation_error::ValidationError; @@ -28,6 +29,34 @@ pub enum Error { Language(LanguageError), } +impl Error { + /// Create a pretty error report with `lyneate`. + /// + /// The `source` argument should be the full source code document that was + /// used to create this error. + pub fn create_report(&self, source: &str) -> String { + let markers = if let Error::Syntax(SyntaxError::InvalidSource { source, position }) = self { + vec![( + position.start_byte..position.end_byte, + format!( + "Invalid syntax from ({}, {}) to ({}, {}).", + position.start_row, + position.start_column, + position.end_column, + position.end_row + ), + (255, 100, 100), + )] + } else { + vec![] + }; + + let report = Report::new_byte_spanned(source, markers); + + report.display_str() + } +} + impl From for Error { fn from(error: SyntaxError) -> Self { Error::Syntax(error) diff --git a/src/error/syntax_error.rs b/src/error/syntax_error.rs index 25c0c60..99527e0 100644 --- a/src/error/syntax_error.rs +++ b/src/error/syntax_error.rs @@ -1,6 +1,5 @@ use std::fmt::{self, Display, Formatter}; -use lyneate::Report; use serde::{Deserialize, Serialize}; use tree_sitter::{Node as SyntaxNode, Point}; @@ -63,25 +62,6 @@ impl From for SyntaxError { impl Display for SyntaxError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - if let SyntaxError::InvalidSource { source, position } = self { - let report = Report::new_char_spanned( - &source, - [( - position.start_byte..position.end_byte, - format!( - "Invalid syntax from ({}, {}) to ({}, {}).", - position.start_row, - position.start_column, - position.end_column, - position.end_row - ), - (255, 100, 100), - )], - ); - - f.write_str(&report.display_str()) - } else { - write!(f, "{self:?}") - } + todo!() } } diff --git a/src/main.rs b/src/main.rs index 35ad447..c824846 100644 --- a/src/main.rs +++ b/src/main.rs @@ -113,7 +113,7 @@ fn main() { println!("{value}") } } - Err(error) => eprintln!("{error}"), + Err(error) => eprintln!("{}", error.create_report(&source)), } }