1 //! Book keeping for keeping diagnostics easily in sync with the client.
2 pub(crate) mod to_proto;
4 use std::{mem, sync::Arc};
7 use rustc_hash::{FxHashMap, FxHashSet};
11 pub(crate) type CheckFixes = Arc<FxHashMap<FileId, Vec<Fix>>>;
13 #[derive(Debug, Default, Clone)]
14 pub struct DiagnosticsMapConfig {
15 pub warnings_as_info: Vec<String>,
16 pub warnings_as_hint: Vec<String>,
19 #[derive(Debug, Default, Clone)]
20 pub(crate) struct DiagnosticCollection {
21 // FIXME: should be FxHashMap<FileId, Vec<ra_id::Diagnostic>>
22 pub(crate) native: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
23 // FIXME: should be Vec<flycheck::Diagnostic>
24 pub(crate) check: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
25 pub(crate) check_fixes: CheckFixes,
26 changes: FxHashSet<FileId>,
29 #[derive(Debug, Clone)]
30 pub(crate) struct Fix {
31 pub(crate) range: lsp_types::Range,
32 pub(crate) action: lsp_ext::CodeAction,
35 impl DiagnosticCollection {
36 pub(crate) fn clear_check(&mut self) {
37 Arc::make_mut(&mut self.check_fixes).clear();
38 self.changes.extend(self.check.drain().map(|(key, _value)| key))
41 pub(crate) fn add_check_diagnostic(
44 diagnostic: lsp_types::Diagnostic,
45 fixes: Vec<lsp_ext::CodeAction>,
47 let diagnostics = self.check.entry(file_id).or_default();
48 for existing_diagnostic in diagnostics.iter() {
49 if are_diagnostics_equal(&existing_diagnostic, &diagnostic) {
54 let check_fixes = Arc::make_mut(&mut self.check_fixes);
58 .extend(fixes.into_iter().map(|action| Fix { range: diagnostic.range, action }));
59 diagnostics.push(diagnostic);
60 self.changes.insert(file_id);
63 pub(crate) fn set_native_diagnostics(
66 diagnostics: Vec<lsp_types::Diagnostic>,
68 if let Some(existing_diagnostics) = self.native.get(&file_id) {
69 if existing_diagnostics.len() == diagnostics.len()
72 .zip(existing_diagnostics)
73 .all(|(new, existing)| are_diagnostics_equal(new, existing))
79 self.native.insert(file_id, diagnostics);
80 self.changes.insert(file_id);
83 pub(crate) fn diagnostics_for(
86 ) -> impl Iterator<Item = &lsp_types::Diagnostic> {
87 let native = self.native.get(&file_id).into_iter().flatten();
88 let check = self.check.get(&file_id).into_iter().flatten();
92 pub(crate) fn take_changes(&mut self) -> Option<FxHashSet<FileId>> {
93 if self.changes.is_empty() {
96 Some(mem::take(&mut self.changes))
100 fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool {
101 left.source == right.source
102 && left.severity == right.severity
103 && left.range == right.range
104 && left.message == right.message