diff --git a/Cargo.lock b/Cargo.lock index 5fb6e65..977b38c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -263,6 +263,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys 0.48.0", +] + [[package]] name = "core-foundation" version = "0.9.4" @@ -363,6 +373,7 @@ version = "0.4.1" dependencies = [ "cc", "clap", + "colored", "crossterm", "csv", "enum-iterator", @@ -371,6 +382,7 @@ dependencies = [ "humantime", "libc", "log", + "lyneate", "nu-ansi-term", "rand", "rayon", @@ -830,6 +842,16 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "lyneate" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db93f0347ea71252796f5c6a61f2b75ed8434635ab2ad84238837a89125fa5e3" +dependencies = [ + "colored", + "widestring", +] + [[package]] name = "malloc_buf" version = "0.0.6" @@ -1833,6 +1855,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "widestring" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "653f141f39ec16bba3c5abe400a0c60da7468261cc2cbf36805022876bc721a8" + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 295ce07..a02526b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,8 @@ crossterm = "0.27.0" nu-ansi-term = "0.49.0" humantime = "2.1.0" stanza = "0.5.1" +colored = "2.1.0" +lyneate = "0.2.1" [target.'cfg(not(target_arch = "wasm32"))'.dependencies] env_logger = "0.10" diff --git a/src/error/mod.rs b/src/error/mod.rs index 7bec030..912d8f3 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs @@ -65,9 +65,9 @@ impl fmt::Display for Error { use Error::*; match self { - Syntax(error) => write!(f, "Syntax error: {error}"), - Validation(error) => write!(f, "Validation error: {error}"), - Runtime(error) => write!(f, "Runtime error: {error}"), + Syntax(error) => write!(f, "{error}"), + Validation(error) => write!(f, "{error}"), + Runtime(error) => write!(f, "{error}"), ParserCancelled => write!(f, "Parsing was cancelled because the parser took too long."), Language(_error) => write!(f, "Parser failed to load language grammar."), } diff --git a/src/error/syntax_error.rs b/src/error/syntax_error.rs index 544f57f..e6cac72 100644 --- a/src/error/syntax_error.rs +++ b/src/error/syntax_error.rs @@ -1,5 +1,6 @@ use std::fmt::{self, Display, Formatter}; +use lyneate::Report; use serde::{Deserialize, Serialize}; use tree_sitter::{Node as SyntaxNode, Point}; @@ -62,6 +63,25 @@ impl From for SyntaxError { impl Display for SyntaxError { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - write!(f, "{self:?}") + if let SyntaxError::InvalidSource { source, position } = self { + let report = Report::new_char_spanned( + &source, + [( + position.start_byte..position.end_byte, + format!( + "Syntax error at ({}, {}) to ({}, {}).", + position.start_row, + position.start_column, + position.end_row, + position.end_column + ), + (255, 100, 100), + )], + ); + + f.write_str(&report.display_str()) + } else { + write!(f, "{self:?}") + } } }