use super::document::{Document, DocumentStore};
use super::message::PublishDiagnosticsParams;
use super::types::*;
use std::sync::Arc;
use crate::diagnostics::check::check_buffer;
use crate::lexer::SourceFile;
const LEXER_DIAGNOSTIC_SOURCE: &str = "buildlang/lexer";
const PARSER_DIAGNOSTIC_SOURCE: &str = "buildlang/parser";
const TYPE_CHECKER_DIAGNOSTIC_SOURCE: &str = "buildlang/type-checker";
pub struct DiagnosticsProvider {
documents: Arc<DocumentStore>,
}
impl DiagnosticsProvider {
pub fn new(documents: Arc<DocumentStore>) -> Self {
Self { documents }
}
pub fn compute(&self, doc: &Document) -> PublishDiagnosticsParams {
let mut diagnostics = Vec::new();
self.check_types(&doc.content, &mut diagnostics);
PublishDiagnosticsParams {
uri: doc.uri.clone(),
version: Some(doc.version),
diagnostics,
}
}
fn check_types(&self, content: &str, diagnostics: &mut Vec<Diagnostic>) {
let source_file = SourceFile::new("buffer", content.to_string());
for d in check_buffer(&source_file) {
diagnostics.push(to_lsp(d));
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn compiler_type_error_diagnostic_uses_type_checker_source() {
let documents = Arc::new(DocumentStore::new());
let provider = DiagnosticsProvider::new(documents.clone());
let doc = documents.open(TextDocumentItem {
uri: "file:///workspace/type_error.bld".to_string(),
language_id: "build".to_string(),
version: 1,
text: "const BAD: i32 = \"oops\";\nfn main() {}\n".to_string(),
});
let published = provider.compute(&doc);
assert!(
published.diagnostics.iter().any(|d| {
d.source.as_deref() == Some("buildlang/type-checker")
&& d.message.contains("type mismatch")
}),
"expected type-checker diagnostic in {:#?}",
published.diagnostics
);
}
}
pub(crate) fn to_lsp(d: crate::diagnostics::Diagnostic) -> Diagnostic {
let range = match &d.span {
Some(span) => Range {
start: Position {
line: span.line.saturating_sub(1) as u32,
character: span.column.saturating_sub(1) as u32,
},
end: Position {
line: span.end_line.saturating_sub(1) as u32,
character: span.end_column.saturating_sub(1) as u32,
},
},
None => Range {
start: Position {
line: 0,
character: 0,
},
end: Position {
line: 0,
character: 1,
},
},
};
let source = match &d.code[..4] {
"BL01" => LEXER_DIAGNOSTIC_SOURCE,
"BL02" => PARSER_DIAGNOSTIC_SOURCE,
_ => TYPE_CHECKER_DIAGNOSTIC_SOURCE,
};
let mut message = d.message;
for help in &d.help {
message.push_str("\n help: ");
message.push_str(help);
}
for note in &d.notes {
message.push_str("\n note: ");
message.push_str(note);
}
Diagnostic {
range,
severity: Some(DiagnosticSeverity::Error),
code: Some(DiagnosticCode::String(d.code.to_string())),
source: Some(source.to_string()),
message,
tags: Vec::new(),
related_information: Vec::new(),
}
}