]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/rust-analyzer/src/diagnostics.rs
Rollup merge of #100296 - BlackHoleFox:os-error-aliases, 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 rustc_hash::{FxHashMap, FxHashSet};
8
9 use crate::lsp_ext;
10
11 pub(crate) type CheckFixes = Arc<FxHashMap<usize, FxHashMap<FileId, Vec<Fix>>>>;
12
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>,
18 }
19
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<usize, FxHashMap<FileId, Vec<lsp_types::Diagnostic>>>,
26     pub(crate) check_fixes: CheckFixes,
27     changes: FxHashSet<FileId>,
28 }
29
30 #[derive(Debug, Clone)]
31 pub(crate) struct Fix {
32     // Fixes may be triggerable from multiple ranges.
33     pub(crate) ranges: Vec<lsp_types::Range>,
34     pub(crate) action: lsp_ext::CodeAction,
35 }
36
37 impl DiagnosticCollection {
38     pub(crate) fn clear_check(&mut self, flycheck_id: usize) {
39         if let Some(it) = Arc::make_mut(&mut self.check_fixes).get_mut(&flycheck_id) {
40             it.clear();
41         }
42         if let Some(it) = self.check.get_mut(&flycheck_id) {
43             self.changes.extend(it.drain().map(|(key, _value)| key));
44         }
45     }
46
47     pub(crate) fn clear_check_all(&mut self) {
48         Arc::make_mut(&mut self.check_fixes).clear();
49         self.changes
50             .extend(self.check.values_mut().flat_map(|it| it.drain().map(|(key, _value)| key)))
51     }
52
53     pub(crate) fn clear_native_for(&mut self, file_id: FileId) {
54         self.native.remove(&file_id);
55         self.changes.insert(file_id);
56     }
57
58     pub(crate) fn add_check_diagnostic(
59         &mut self,
60         flycheck_id: usize,
61         file_id: FileId,
62         diagnostic: lsp_types::Diagnostic,
63         fix: Option<Fix>,
64     ) {
65         let diagnostics = self.check.entry(flycheck_id).or_default().entry(file_id).or_default();
66         for existing_diagnostic in diagnostics.iter() {
67             if are_diagnostics_equal(existing_diagnostic, &diagnostic) {
68                 return;
69             }
70         }
71
72         let check_fixes = Arc::make_mut(&mut self.check_fixes);
73         check_fixes.entry(flycheck_id).or_default().entry(file_id).or_default().extend(fix);
74         diagnostics.push(diagnostic);
75         self.changes.insert(file_id);
76     }
77
78     pub(crate) fn set_native_diagnostics(
79         &mut self,
80         file_id: FileId,
81         diagnostics: Vec<lsp_types::Diagnostic>,
82     ) {
83         if let Some(existing_diagnostics) = self.native.get(&file_id) {
84             if existing_diagnostics.len() == diagnostics.len()
85                 && diagnostics
86                     .iter()
87                     .zip(existing_diagnostics)
88                     .all(|(new, existing)| are_diagnostics_equal(new, existing))
89             {
90                 return;
91             }
92         }
93
94         self.native.insert(file_id, diagnostics);
95         self.changes.insert(file_id);
96     }
97
98     pub(crate) fn diagnostics_for(
99         &self,
100         file_id: FileId,
101     ) -> impl Iterator<Item = &lsp_types::Diagnostic> {
102         let native = self.native.get(&file_id).into_iter().flatten();
103         let check =
104             self.check.values().filter_map(move |it| it.get(&file_id)).into_iter().flatten();
105         native.chain(check)
106     }
107
108     pub(crate) fn take_changes(&mut self) -> Option<FxHashSet<FileId>> {
109         if self.changes.is_empty() {
110             return None;
111         }
112         Some(mem::take(&mut self.changes))
113     }
114 }
115
116 fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool {
117     left.source == right.source
118         && left.severity == right.severity
119         && left.range == right.range
120         && left.message == right.message
121 }