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 remap_prefix: FxHashMap<String, String>,
16 pub warnings_as_info: Vec<String>,
17 pub warnings_as_hint: Vec<String>,
20 #[derive(Debug, Default, Clone)]
21 pub(crate) struct DiagnosticCollection {
22 // FIXME: should be FxHashMap<FileId, Vec<ra_id::Diagnostic>>
23 pub(crate) native: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
24 // FIXME: should be Vec<flycheck::Diagnostic>
25 pub(crate) check: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
26 pub(crate) check_fixes: CheckFixes,
27 changes: FxHashSet<FileId>,
30 #[derive(Debug, Clone)]
31 pub(crate) struct Fix {
32 pub(crate) range: lsp_types::Range,
33 pub(crate) action: lsp_ext::CodeAction,
36 impl DiagnosticCollection {
37 pub(crate) fn clear_check(&mut self) {
38 Arc::make_mut(&mut self.check_fixes).clear();
39 self.changes.extend(self.check.drain().map(|(key, _value)| key))
42 pub(crate) fn add_check_diagnostic(
45 diagnostic: lsp_types::Diagnostic,
46 fixes: Vec<lsp_ext::CodeAction>,
48 let diagnostics = self.check.entry(file_id).or_default();
49 for existing_diagnostic in diagnostics.iter() {
50 if are_diagnostics_equal(&existing_diagnostic, &diagnostic) {
55 let check_fixes = Arc::make_mut(&mut self.check_fixes);
59 .extend(fixes.into_iter().map(|action| Fix { range: diagnostic.range, action }));
60 diagnostics.push(diagnostic);
61 self.changes.insert(file_id);
64 pub(crate) fn set_native_diagnostics(
67 diagnostics: Vec<lsp_types::Diagnostic>,
69 if let Some(existing_diagnostics) = self.native.get(&file_id) {
70 if existing_diagnostics.len() == diagnostics.len()
73 .zip(existing_diagnostics)
74 .all(|(new, existing)| are_diagnostics_equal(new, existing))
80 self.native.insert(file_id, diagnostics);
81 self.changes.insert(file_id);
84 pub(crate) fn diagnostics_for(
87 ) -> impl Iterator<Item = &lsp_types::Diagnostic> {
88 let native = self.native.get(&file_id).into_iter().flatten();
89 let check = self.check.get(&file_id).into_iter().flatten();
93 pub(crate) fn take_changes(&mut self) -> Option<FxHashSet<FileId>> {
94 if self.changes.is_empty() {
97 Some(mem::take(&mut self.changes))
101 fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool {
102 left.source == right.source
103 && left.severity == right.severity
104 && left.range == right.range
105 && left.message == right.message