forked from NomicFoundation/slang
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce Diagnostic trait and remove error rendering in public API
- Loading branch information
Showing
39 changed files
with
590 additions
and
227 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::borrow::Cow; | ||
use std::error::Error; | ||
|
||
use crate::text_index::TextRange; | ||
|
||
#[repr(u8)] | ||
pub enum Severity { | ||
Error = 1, | ||
Warning = 2, | ||
Information = 3, | ||
Hint = 4, | ||
} | ||
|
||
pub trait Diagnostic: Error { | ||
fn range(&self) -> TextRange; | ||
fn code(&self) -> Option<Cow<'_, str>> { | ||
None | ||
} | ||
fn severity(&self) -> Severity; | ||
fn message(&self) -> String; | ||
} | ||
|
||
#[cfg(feature = "__private_ariadne")] | ||
pub fn render<D: Diagnostic>(error: &D, source_id: &str, source: &str, with_color: bool) -> String { | ||
use ariadne::{Color, Config, Label, Report, ReportKind, Source}; | ||
|
||
use crate::text_index::TextRangeExtensions as _; | ||
|
||
let kind = match error.severity() { | ||
Severity::Error => ReportKind::Error, | ||
Severity::Warning => ReportKind::Warning, | ||
Severity::Information => ReportKind::Advice, | ||
Severity::Hint => ReportKind::Advice, | ||
}; | ||
|
||
let color = if with_color { Color::Red } else { Color::Unset }; | ||
|
||
let message = error.message(); | ||
|
||
if source.is_empty() { | ||
return format!("{kind}: {message}\n ─[{source_id}:0:0]"); | ||
} | ||
|
||
let range = error.range().char(); | ||
|
||
let report = Report::build(kind, source_id, range.start) | ||
.with_config(Config::default().with_color(with_color)) | ||
.with_message(message) | ||
.with_label( | ||
Label::new((source_id, range)) | ||
.with_color(color) | ||
.with_message("Error occurred here."), | ||
) | ||
.finish(); | ||
|
||
let mut result = vec![]; | ||
report | ||
.write((source_id, Source::from(&source)), &mut result) | ||
.expect("Failed to write report"); | ||
|
||
return String::from_utf8(result) | ||
.expect("Failed to convert report to utf8") | ||
.trim() | ||
.to_string(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
crates/codegen/runtime/cargo/src/runtime/napi_interface/diagnostic.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use napi_derive::napi; | ||
|
||
use crate::napi_interface::text_index::TextRange; | ||
|
||
/// Severity of the compiler diagnostic. | ||
/// | ||
/// Explicitly compatible with the LSP protocol. | ||
#[napi(namespace = "diagnostic")] | ||
pub enum Severity { | ||
Error = 1, | ||
Warning = 2, | ||
Information = 3, | ||
Hint = 4, | ||
} | ||
|
||
impl From<crate::diagnostic::Severity> for Severity { | ||
fn from(value: crate::diagnostic::Severity) -> Severity { | ||
match value { | ||
crate::diagnostic::Severity::Error => Self::Error, | ||
crate::diagnostic::Severity::Warning => Self::Warning, | ||
crate::diagnostic::Severity::Information => Self::Information, | ||
crate::diagnostic::Severity::Hint => Self::Hint, | ||
} | ||
} | ||
} | ||
|
||
#[napi(namespace = "diagnostic")] | ||
pub struct Diagnostic(pub(crate) Box<dyn crate::diagnostic::Diagnostic>); | ||
|
||
#[napi(namespace = "diagnostic")] | ||
impl Diagnostic { | ||
#[napi] | ||
pub fn severity(&self) -> Severity { | ||
self.0.severity().into() | ||
} | ||
|
||
#[napi(ts_return_type = "text_index.TextRange")] | ||
pub fn text_range(&self) -> TextRange { | ||
self.0.range().into() | ||
} | ||
|
||
#[napi] | ||
pub fn message(&self) -> String { | ||
self.0.message() | ||
} | ||
|
||
#[napi] | ||
pub fn code(&self) -> String { | ||
self.0.code().unwrap_or_default().into_owned() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod cst; | ||
pub mod cursor; | ||
pub mod diagnostic; | ||
pub mod parse_error; | ||
pub mod parse_output; | ||
pub mod query; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 20 additions & 1 deletion
21
crates/codegen/runtime/npm/src/runtime/napi-bindings/generated/index.d.ts
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
4 changes: 3 additions & 1 deletion
4
crates/codegen/runtime/npm/src/runtime/napi-bindings/generated/index.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.