Skip to content

Commit

Permalink
feat: update to ariadne 0.5
Browse files Browse the repository at this point in the history
  • Loading branch information
indietyp committed Nov 4, 2024
1 parent 5c27bb9 commit 1ed1565
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 23 deletions.
34 changes: 13 additions & 21 deletions libs/@local/hql/diagnostics/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{
};

use ariadne::ColorGenerator;
use error_stack::{Report, Result, TryReportIteratorExt, TryReportTupleExt};
use error_stack::{Result, TryReportIteratorExt};
use hql_span::{Span, SpanId, storage::SpanStorage, tree::SpanNode};

use crate::{
Expand All @@ -16,7 +16,7 @@ use crate::{
note::Note,
rob::RefOrBox,
severity::Severity,
span::{AbsoluteDiagnosticSpan, TransformSpan, absolute_span},
span::{AbsoluteDiagnosticSpan, TransformSpan},
};

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
Expand All @@ -26,7 +26,6 @@ pub struct Diagnostic<'a, S> {
pub severity: RefOrBox<'a, Severity<'a>>,

pub message: Option<Box<str>>,
pub span: Option<S>,

pub labels: Vec<Label<S>>,
pub note: Option<Note>,
Expand All @@ -43,7 +42,6 @@ impl<'a, S> Diagnostic<'a, S> {
category: category.into(),
severity: severity.into(),
message: None,
span: None,
labels: Vec::new(),
note: None,
help: None,
Expand All @@ -64,29 +62,18 @@ impl<'a> Diagnostic<'a, SpanId> {
where
S: Span + Clone,
{
let span = self
.span
.map(|id| {
storage
.resolve(id)
.ok_or_else(|| Report::new(ResolveError::UnknownSpan { id }))
})
.transpose();

let labels: Result<Vec<_>, _> = self
.labels
.into_iter()
.map(|label| label.resolve(storage))
.try_collect_reports();

let (span, labels) = (span, labels).try_collect()?;

Ok(Diagnostic {
category: self.category,
severity: self.severity,
message: self.message,
span,
labels,

labels: labels?,
note: self.note,
help: self.help,
})
Expand All @@ -98,13 +85,18 @@ impl<S> Diagnostic<'_, SpanNode<S>> {
&self,
mut config: ReportConfig<impl TransformSpan<S>>,
) -> ariadne::Report<AbsoluteDiagnosticSpan> {
let start = self.span.as_ref().map_or(0, |span| {
u32::from(absolute_span(span, &mut config.transform_span).start())
});
// According to the examples, the first range supplied should be the one that is absolute.
// See: https://github.com/zesterer/ariadne/blob/74c2a7f8881e95629f9fb8d70140c133972d81d3/examples/simple.rs#L14
let span = self
.labels
.first()
.map_or_else(AbsoluteDiagnosticSpan::full, |label| {
label.absolute_span(&mut config.transform_span)
});

let mut generator = ColorGenerator::new();

let mut builder = ariadne::Report::build(self.severity.as_ref().kind(), (), start as usize)
let mut builder = ariadne::Report::build(self.severity.as_ref().kind(), span)
.with_code(self.category.as_ref().canonical_id());

builder.set_message(
Expand Down
11 changes: 9 additions & 2 deletions libs/@local/hql/diagnostics/src/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,21 @@ impl Label<SpanId> {
}

impl<S> Label<SpanNode<S>> {
pub(crate) fn absolute_span(
&self,
transform: &mut impl TransformSpan<S>,
) -> AbsoluteDiagnosticSpan {
AbsoluteDiagnosticSpan::new(&self.span, transform)
}

pub(crate) fn ariadne(
&self,
enable_color: bool,
generator: &mut ColorGenerator,
transform: &mut impl TransformSpan<S>,
) -> ariadne::Label<AbsoluteDiagnosticSpan> {
let mut label = ariadne::Label::new(AbsoluteDiagnosticSpan::new(&self.span, transform))
.with_message(&self.message);
let mut label =
ariadne::Label::new(self.absolute_span(transform)).with_message(&self.message);

let color = self
.color
Expand Down
6 changes: 6 additions & 0 deletions libs/@local/hql/diagnostics/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ impl AbsoluteDiagnosticSpan {

Self { range }
}

pub(crate) const fn full() -> Self {
Self {
range: TextRange::new(TextSize::new(0), TextSize::new(u32::MAX)),
}
}
}

impl ariadne::Span for AbsoluteDiagnosticSpan {
Expand Down

0 comments on commit 1ed1565

Please sign in to comment.