Skip to content

Commit 07f1692

Browse files
committed
diagnostic: allow strings for codes
1 parent 456aade commit 07f1692

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

src/ast/diagnostic.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use std::{
1010
};
1111

1212
use ariadne::{Cache, Report, ReportBuilder, ReportKind, Source};
13-
use lsp_types::{DiagnosticTag, TextDocumentIdentifier, VersionedTextDocumentIdentifier};
13+
use lsp_types::{
14+
DiagnosticTag, NumberOrString, TextDocumentIdentifier, VersionedTextDocumentIdentifier,
15+
};
1416
use pathdiff::diff_paths;
1517

1618
use crate::pretty::{Doc, SimplePretty};
@@ -421,7 +423,7 @@ pub struct Diagnostic(Box<DiagnosticInner>);
421423
#[derive(Debug)]
422424
struct DiagnosticInner {
423425
kind: ReportKind<'static>,
424-
code: Option<u32>,
426+
code: Option<NumberOrString>,
425427
msg: Option<String>,
426428
note: Option<String>,
427429
location: Span,
@@ -449,8 +451,8 @@ impl Diagnostic {
449451
self
450452
}
451453

452-
/// Give this diagnostic a numerical code that may be used to more precisely look up the error in documentation.
453-
pub fn with_code(mut self, code: u32) -> Self {
454+
/// Give this diagnostic a code.
455+
pub fn with_code(mut self, code: NumberOrString) -> Self {
454456
self.0.code = Some(code);
455457
self
456458
}
@@ -499,6 +501,10 @@ impl Diagnostic {
499501
let span = files.char_span(self.0.location);
500502
let mut builder = Report::build(self.0.kind, span);
501503
if let Some(code) = self.0.code {
504+
let code = match code {
505+
NumberOrString::Number(code) => code.to_string(),
506+
NumberOrString::String(code) => code,
507+
};
502508
builder = builder.with_code(code);
503509
}
504510
if let Some(msg) = self.0.msg {
@@ -522,13 +528,10 @@ impl Diagnostic {
522528
let severity = match self.0.kind {
523529
ReportKind::Error => lsp_types::DiagnosticSeverity::ERROR,
524530
ReportKind::Warning => lsp_types::DiagnosticSeverity::WARNING,
525-
ReportKind::Advice => lsp_types::DiagnosticSeverity::HINT,
531+
ReportKind::Advice => lsp_types::DiagnosticSeverity::INFORMATION,
526532
_ => lsp_types::DiagnosticSeverity::ERROR,
527533
};
528-
let code = self
529-
.0
530-
.code
531-
.map(|code| lsp_types::NumberOrString::Number(code as i32));
534+
let code = self.0.code.clone();
532535
let message = self
533536
.0
534537
.msg
@@ -597,7 +600,11 @@ impl Diagnostic {
597600
/// exists to make debugging easier.
598601
impl Display for Diagnostic {
599602
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
600-
if let Some(code) = self.0.code {
603+
if let Some(code) = &self.0.code {
604+
let code = match code {
605+
NumberOrString::Number(code) => &code.to_string(),
606+
NumberOrString::String(code) => code,
607+
};
601608
write!(f, "[{}] ", code)?;
602609
}
603610
write!(f, "{}: ", self.0.kind)?;

src/front/resolve.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ impl ResolveError {
8585
.with_message("Expression must be an identifier")
8686
.with_label(Label::new(span).with_message("found expression instead")),
8787
}
88+
.with_code(lsp_types::NumberOrString::String("resolve".to_owned()))
8889
}
8990
}
9091

src/front/tycheck.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ impl TycheckError {
384384
"Procedures must only be called on as the immediate right-hand side expression in an assignment. This makes execution order of assignments with side-effects explicit."
385385
),
386386
}
387+
.with_code(lsp_types::NumberOrString::String("tycheck".to_owned()))
387388
}
388389
}
389390

0 commit comments

Comments
 (0)