1 //! Book keeping for keeping diagnostics easily in sync with the client.
2 pub(crate) mod to_proto;
3
4 use std::mem;
5
6 use ide::FileId;
7 use ide_db::FxHashMap;
8 use nohash_hasher::{IntMap, IntSet};
9 use triomphe::Arc;
10
11 use crate::lsp_ext;
12
13 pub(crate) type CheckFixes = Arc<IntMap<usize, IntMap<FileId, Vec<Fix>>>>;
14
15 #[derive(Debug, Default, Clone)]
16 pub struct DiagnosticsMapConfig {
17 pub remap_prefix: FxHashMap<String, String>,
18 pub warnings_as_info: Vec<String>,
19 pub warnings_as_hint: Vec<String>,
20 }
21
22 #[derive(Debug, Default, Clone)]
23 pub(crate) struct DiagnosticCollection {
24 // FIXME: should be IntMap<FileId, Vec<ra_id::Diagnostic>>
25 pub(crate) native: IntMap<FileId, Vec<lsp_types::Diagnostic>>,
26 // FIXME: should be Vec<flycheck::Diagnostic>
27 pub(crate) check: IntMap<usize, IntMap<FileId, Vec<lsp_types::Diagnostic>>>,
28 pub(crate) check_fixes: CheckFixes,
29 changes: IntSet<FileId>,
30 }
31
32 #[derive(Debug, Clone)]
33 pub(crate) struct Fix {
34 // Fixes may be triggerable from multiple ranges.
35 pub(crate) ranges: Vec<lsp_types::Range>,
36 pub(crate) action: lsp_ext::CodeAction,
37 }
38
39 impl DiagnosticCollection {
clear_check(&mut self, flycheck_id: usize)40 pub(crate) fn clear_check(&mut self, flycheck_id: usize) {
41 if let Some(it) = Arc::make_mut(&mut self.check_fixes).get_mut(&flycheck_id) {
42 it.clear();
43 }
44 if let Some(it) = self.check.get_mut(&flycheck_id) {
45 self.changes.extend(it.drain().map(|(key, _value)| key));
46 }
47 }
48
clear_check_all(&mut self)49 pub(crate) fn clear_check_all(&mut self) {
50 Arc::make_mut(&mut self.check_fixes).clear();
51 self.changes
52 .extend(self.check.values_mut().flat_map(|it| it.drain().map(|(key, _value)| key)))
53 }
54
clear_native_for(&mut self, file_id: FileId)55 pub(crate) fn clear_native_for(&mut self, file_id: FileId) {
56 self.native.remove(&file_id);
57 self.changes.insert(file_id);
58 }
59
add_check_diagnostic( &mut self, flycheck_id: usize, file_id: FileId, diagnostic: lsp_types::Diagnostic, fix: Option<Fix>, )60 pub(crate) fn add_check_diagnostic(
61 &mut self,
62 flycheck_id: usize,
63 file_id: FileId,
64 diagnostic: lsp_types::Diagnostic,
65 fix: Option<Fix>,
66 ) {
67 let diagnostics = self.check.entry(flycheck_id).or_default().entry(file_id).or_default();
68 for existing_diagnostic in diagnostics.iter() {
69 if are_diagnostics_equal(existing_diagnostic, &diagnostic) {
70 return;
71 }
72 }
73
74 let check_fixes = Arc::make_mut(&mut self.check_fixes);
75 check_fixes.entry(flycheck_id).or_default().entry(file_id).or_default().extend(fix);
76 diagnostics.push(diagnostic);
77 self.changes.insert(file_id);
78 }
79
set_native_diagnostics( &mut self, file_id: FileId, diagnostics: Vec<lsp_types::Diagnostic>, )80 pub(crate) fn set_native_diagnostics(
81 &mut self,
82 file_id: FileId,
83 diagnostics: Vec<lsp_types::Diagnostic>,
84 ) {
85 if let Some(existing_diagnostics) = self.native.get(&file_id) {
86 if existing_diagnostics.len() == diagnostics.len()
87 && diagnostics
88 .iter()
89 .zip(existing_diagnostics)
90 .all(|(new, existing)| are_diagnostics_equal(new, existing))
91 {
92 return;
93 }
94 }
95
96 self.native.insert(file_id, diagnostics);
97 self.changes.insert(file_id);
98 }
99
diagnostics_for( &self, file_id: FileId, ) -> impl Iterator<Item = &lsp_types::Diagnostic>100 pub(crate) fn diagnostics_for(
101 &self,
102 file_id: FileId,
103 ) -> impl Iterator<Item = &lsp_types::Diagnostic> {
104 let native = self.native.get(&file_id).into_iter().flatten();
105 let check = self.check.values().filter_map(move |it| it.get(&file_id)).flatten();
106 native.chain(check)
107 }
108
take_changes(&mut self) -> Option<IntSet<FileId>>109 pub(crate) fn take_changes(&mut self) -> Option<IntSet<FileId>> {
110 if self.changes.is_empty() {
111 return None;
112 }
113 Some(mem::take(&mut self.changes))
114 }
115 }
116
are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool117 fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool {
118 left.source == right.source
119 && left.severity == right.severity
120 && left.range == right.range
121 && left.message == right.message
122 }
123