Skip to content

Commit

Permalink
Update Rust crate ariadne to v0.5.0 (#5550)
Browse files Browse the repository at this point in the history
Co-authored-by: hash-worker[bot] <180894564+hash-worker[bot]@users.noreply.github.com>
Co-authored-by: Bilal Mahmoud <[email protected]>
  • Loading branch information
hash-worker[bot] and indietyp authored Nov 4, 2024
1 parent e2d44c7 commit fb84b51
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ error-stack = { path = "./libs/error-stack", default-features = false }

# Public dependencies
ahash = { version = "=0.8.11", default-features = false }
ariadne = { version = "=0.4.1", default-features = false }
ariadne = { version = "=0.5.0", default-features = false }
aws-types = { version = "=1.3.3", default-features = false }
axum = "0.7.5"
axum-core = "0.4.3"
Expand Down
37 changes: 15 additions & 22 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, TryReportIteratorExt as _, TryReportTupleExt as _};
use error_stack::{Report, TryReportIteratorExt as _};
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,28 +62,17 @@ 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
let labels: Vec<_> = self
.labels
.into_iter()
.map(|label| label.resolve(storage))
.try_collect_reports();

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

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

labels,
note: self.note,
help: self.help,
Expand All @@ -98,13 +85,19 @@ 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 span given to `Report::build` should be the span of the
// primary (first) label.
// 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

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fb84b51

Please sign in to comment.