1
0

Begin making pretty errors with lyneate

This commit is contained in:
Jeff 2024-02-16 16:49:01 -05:00
parent 4b0910a545
commit a53f83f03a
4 changed files with 54 additions and 4 deletions

28
Cargo.lock generated
View File

@ -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"

View File

@ -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"

View File

@ -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."),
}

View File

@ -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<RwLockError> 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:?}")
}
}
}