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