Write function for lyneate integration
This commit is contained in:
parent
7003c37aac
commit
ee4f37080e
@ -7,6 +7,7 @@ pub(crate) mod rw_lock_error;
|
|||||||
mod syntax_error;
|
mod syntax_error;
|
||||||
mod validation_error;
|
mod validation_error;
|
||||||
|
|
||||||
|
use lyneate::Report;
|
||||||
pub use runtime_error::RuntimeError;
|
pub use runtime_error::RuntimeError;
|
||||||
pub use syntax_error::SyntaxError;
|
pub use syntax_error::SyntaxError;
|
||||||
pub use validation_error::ValidationError;
|
pub use validation_error::ValidationError;
|
||||||
@ -28,6 +29,34 @@ pub enum Error {
|
|||||||
Language(LanguageError),
|
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 {
|
impl From<SyntaxError> for Error {
|
||||||
fn from(error: SyntaxError) -> Self {
|
fn from(error: SyntaxError) -> Self {
|
||||||
Error::Syntax(error)
|
Error::Syntax(error)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
use std::fmt::{self, Display, Formatter};
|
use std::fmt::{self, Display, Formatter};
|
||||||
|
|
||||||
use lyneate::Report;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use tree_sitter::{Node as SyntaxNode, Point};
|
use tree_sitter::{Node as SyntaxNode, Point};
|
||||||
|
|
||||||
@ -63,25 +62,6 @@ impl From<RwLockError> for SyntaxError {
|
|||||||
|
|
||||||
impl Display for SyntaxError {
|
impl Display for SyntaxError {
|
||||||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
|
||||||
if let SyntaxError::InvalidSource { source, position } = self {
|
todo!()
|
||||||
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:?}")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -113,7 +113,7 @@ fn main() {
|
|||||||
println!("{value}")
|
println!("{value}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(error) => eprintln!("{error}"),
|
Err(error) => eprintln!("{}", error.create_report(&source)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user