feat: store raw diag in error (#1271)

This commit is contained in:
Myriad-Dreamin 2025-02-08 15:11:48 +08:00 committed by GitHub
parent a4ac6780fe
commit 6b31d4418d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 24 additions and 2 deletions

View file

@ -29,7 +29,7 @@ semver.workspace = true
serde.workspace = true
serde_json.workspace = true
tinymist-world = { workspace = true }
tinymist-std = { workspace = true }
tinymist-std = { workspace = true, features = ["typst"] }
tinymist-derive.workspace = true
toml.workspace = true
typst.workspace = true

View file

@ -28,6 +28,7 @@ serde_json.workspace = true
serde_with.workspace = true
siphasher.workspace = true
web-time.workspace = true
lsp-types.workspace = true
tempfile = { workspace = true, optional = true }
same-file = { workspace = true, optional = true }

View file

@ -4,8 +4,10 @@ use core::fmt;
use ecow::EcoString;
use serde::{Deserialize, Serialize};
#[cfg(feature = "typst")]
use typst::diag::SourceDiagnostic;
use crate::debug_loc::LspRange;
use lsp_types::Range as LspRange;
/// The severity of a diagnostic message, following the LSP specification.
#[derive(serde_repr::Serialize_repr, serde_repr::Deserialize_repr, Debug, Clone)]
@ -60,6 +62,9 @@ pub enum ErrKind {
/// A string message.
Msg(EcoString),
/// A source diagnostic message.
#[cfg(feature = "typst")]
RawDiag(ecow::EcoVec<SourceDiagnostic>),
/// A source diagnostic message.
Diag(Box<DiagMessage>),
/// An inner error.
Inner(Error),
@ -192,6 +197,10 @@ impl fmt::Display for Error {
if err.loc.is_empty() {
match &err.kind {
ErrKind::Msg(msg) => write!(f, "{msg} with {:?}", err.args),
#[cfg(feature = "typst")]
ErrKind::RawDiag(diag) => {
write!(f, "{diag:?} with {:?}", err.args)
}
ErrKind::Diag(diag) => {
write!(f, "{} with {:?}", diag.message, err.args)
}
@ -201,6 +210,10 @@ impl fmt::Display for Error {
} else {
match &err.kind {
ErrKind::Msg(msg) => write!(f, "{}: {} with {:?}", err.loc, msg, err.args),
#[cfg(feature = "typst")]
ErrKind::RawDiag(diag) => {
write!(f, "{}: {diag:?} with {:?}", err.loc, err.args)
}
ErrKind::Diag(diag) => {
write!(f, "{}: {} with {:?}", err.loc, diag.message, err.args)
}
@ -217,6 +230,13 @@ impl From<anyhow::Error> for Error {
}
}
#[cfg(feature = "typst")]
impl From<ecow::EcoVec<SourceDiagnostic>> for Error {
fn from(e: ecow::EcoVec<SourceDiagnostic>) -> Self {
Error::new("", ErrKind::RawDiag(e), None)
}
}
impl std::error::Error for Error {}
#[cfg(feature = "web")]