Write function for lyneate integration

This commit is contained in:
Jeff 2024-02-16 17:28:57 -05:00
parent 7003c37aac
commit ee4f37080e
3 changed files with 31 additions and 22 deletions

View File

@ -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<SyntaxError> for Error {
fn from(error: SyntaxError) -> Self {
Error::Syntax(error)

View File

@ -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<RwLockError> 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!()
}
}

View File

@ -113,7 +113,7 @@ fn main() {
println!("{value}")
}
}
Err(error) => eprintln!("{error}"),
Err(error) => eprintln!("{}", error.create_report(&source)),
}
}