Skip to main content

blues_lsp/syntax/
diagnostic.rs

1use super::location::{FileSpan, ROOT_ORIGIN, Span};
2
3#[derive(Debug)]
4pub enum Severity {
5    Error = 1,
6    Warning = 2,
7    Information = 3,
8    Hint = 4,
9}
10
11impl Severity {
12    pub fn to_lsp(&self) -> lsp_types::DiagnosticSeverity {
13        match self {
14            Severity::Error => lsp_types::DiagnosticSeverity::ERROR,
15            Severity::Warning => lsp_types::DiagnosticSeverity::WARNING,
16            Severity::Information => lsp_types::DiagnosticSeverity::INFORMATION,
17            Severity::Hint => lsp_types::DiagnosticSeverity::HINT,
18        }
19    }
20}
21
22#[derive(Debug)]
23pub struct Diagnostic {
24    pub span: FileSpan,
25    pub severity: Severity,
26    pub source: &'static str,
27    pub message: String,
28}
29
30impl Diagnostic {
31    pub fn new(span: FileSpan, severity: Severity, source: &'static str, message: String) -> Self {
32        Self {
33            span,
34            severity,
35            source,
36            message,
37        }
38    }
39}
40
41#[derive(Debug, Default)]
42pub struct FileDiagnostics {
43    list: Vec<Diagnostic>,
44}
45
46impl FileDiagnostics {
47    pub fn push(&mut self, span: Span, severity: Severity, source: &'static str, message: String) {
48        // TODO: would be helpfull to emit additional diagnostics in header files?
49        if span.origin != ROOT_ORIGIN {
50            return;
51        }
52        // TODO: a limit to emited diagnostics is probably also a good idea...
53        self.list
54            .push(Diagnostic::new(span.text_span, severity, source, message));
55    }
56
57    pub fn get(&self) -> &[Diagnostic] {
58        &self.list
59    }
60}