2020-01-31 12:23:25 -06:00
|
|
|
//! Book keeping for keeping diagnostics easily in sync with the client.
|
2020-05-14 18:51:48 -05:00
|
|
|
pub(crate) mod to_proto;
|
2020-02-18 05:25:26 -06:00
|
|
|
|
|
|
|
use std::{collections::HashMap, sync::Arc};
|
|
|
|
|
2020-05-17 17:11:40 -05:00
|
|
|
use lsp_types::{Diagnostic, Range};
|
2020-01-31 12:23:25 -06:00
|
|
|
use ra_ide::FileId;
|
|
|
|
|
2020-05-17 17:11:40 -05:00
|
|
|
use crate::lsp_ext;
|
|
|
|
|
2020-01-31 12:23:25 -06:00
|
|
|
pub type CheckFixes = Arc<HashMap<FileId, Vec<Fix>>>;
|
|
|
|
|
2020-06-16 15:26:33 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
pub struct DiagnosticsConfig {
|
|
|
|
pub warnings_as_info: Vec<String>,
|
|
|
|
pub warnings_as_hint: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:23:25 -06:00
|
|
|
#[derive(Debug, Default, Clone)]
|
|
|
|
pub struct DiagnosticCollection {
|
|
|
|
pub native: HashMap<FileId, Vec<Diagnostic>>,
|
|
|
|
pub check: HashMap<FileId, Vec<Diagnostic>>,
|
|
|
|
pub check_fixes: CheckFixes,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct Fix {
|
|
|
|
pub range: Range,
|
2020-05-17 17:11:40 -05:00
|
|
|
pub action: lsp_ext::CodeAction,
|
2020-01-31 12:23:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum DiagnosticTask {
|
|
|
|
ClearCheck,
|
2020-05-17 17:11:40 -05:00
|
|
|
AddCheck(FileId, Diagnostic, Vec<lsp_ext::CodeAction>),
|
2020-01-31 12:23:25 -06:00
|
|
|
SetNative(FileId, Vec<Diagnostic>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticCollection {
|
|
|
|
pub fn clear_check(&mut self) -> Vec<FileId> {
|
|
|
|
Arc::make_mut(&mut self.check_fixes).clear();
|
|
|
|
self.check.drain().map(|(key, _value)| key).collect()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_check_diagnostic(
|
|
|
|
&mut self,
|
|
|
|
file_id: FileId,
|
|
|
|
diagnostic: Diagnostic,
|
2020-05-17 17:11:40 -05:00
|
|
|
fixes: Vec<lsp_ext::CodeAction>,
|
2020-01-31 12:23:25 -06:00
|
|
|
) {
|
|
|
|
let diagnostics = self.check.entry(file_id).or_default();
|
|
|
|
for existing_diagnostic in diagnostics.iter() {
|
|
|
|
if are_diagnostics_equal(&existing_diagnostic, &diagnostic) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let check_fixes = Arc::make_mut(&mut self.check_fixes);
|
|
|
|
check_fixes
|
|
|
|
.entry(file_id)
|
|
|
|
.or_default()
|
|
|
|
.extend(fixes.into_iter().map(|action| Fix { range: diagnostic.range, action }));
|
|
|
|
diagnostics.push(diagnostic);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn set_native_diagnostics(&mut self, file_id: FileId, diagnostics: Vec<Diagnostic>) {
|
|
|
|
self.native.insert(file_id, diagnostics);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn diagnostics_for(&self, file_id: FileId) -> impl Iterator<Item = &Diagnostic> {
|
|
|
|
let native = self.native.get(&file_id).into_iter().flatten();
|
|
|
|
let check = self.check.get(&file_id).into_iter().flatten();
|
|
|
|
native.chain(check)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_task(&mut self, task: DiagnosticTask) -> Vec<FileId> {
|
|
|
|
match task {
|
|
|
|
DiagnosticTask::ClearCheck => self.clear_check(),
|
|
|
|
DiagnosticTask::AddCheck(file_id, diagnostic, fixes) => {
|
|
|
|
self.add_check_diagnostic(file_id, diagnostic, fixes);
|
|
|
|
vec![file_id]
|
|
|
|
}
|
|
|
|
DiagnosticTask::SetNative(file_id, diagnostics) => {
|
|
|
|
self.set_native_diagnostics(file_id, diagnostics);
|
|
|
|
vec![file_id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn are_diagnostics_equal(left: &Diagnostic, right: &Diagnostic) -> bool {
|
|
|
|
left.source == right.source
|
|
|
|
&& left.severity == right.severity
|
|
|
|
&& left.range == right.range
|
|
|
|
&& left.message == right.message
|
|
|
|
}
|