]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/diagnostics.rs
Merge #10902
[rust.git] / 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<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<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) {
39         Arc::make_mut(&mut self.check_fixes).clear();
40         self.changes.extend(self.check.drain().map(|(key, _value)| key))
41     }
42
43     pub(crate) fn add_check_diagnostic(
44         &mut self,
45         file_id: FileId,
46         diagnostic: lsp_types::Diagnostic,
47         fix: Option<Fix>,
48     ) {
49         let diagnostics = self.check.entry(file_id).or_default();
50         for existing_diagnostic in diagnostics.iter() {
51             if are_diagnostics_equal(existing_diagnostic, &diagnostic) {
52                 return;
53             }
54         }
55
56         let check_fixes = Arc::make_mut(&mut self.check_fixes);
57         check_fixes.entry(file_id).or_default().extend(fix);
58         diagnostics.push(diagnostic);
59         self.changes.insert(file_id);
60     }
61
62     pub(crate) fn set_native_diagnostics(
63         &mut self,
64         file_id: FileId,
65         diagnostics: Vec<lsp_types::Diagnostic>,
66     ) {
67         if let Some(existing_diagnostics) = self.native.get(&file_id) {
68             if existing_diagnostics.len() == diagnostics.len()
69                 && diagnostics
70                     .iter()
71                     .zip(existing_diagnostics)
72                     .all(|(new, existing)| are_diagnostics_equal(new, existing))
73             {
74                 return;
75             }
76         }
77
78         self.native.insert(file_id, diagnostics);
79         self.changes.insert(file_id);
80     }
81
82     pub(crate) fn diagnostics_for(
83         &self,
84         file_id: FileId,
85     ) -> impl Iterator<Item = &lsp_types::Diagnostic> {
86         let native = self.native.get(&file_id).into_iter().flatten();
87         let check = self.check.get(&file_id).into_iter().flatten();
88         native.chain(check)
89     }
90
91     pub(crate) fn take_changes(&mut self) -> Option<FxHashSet<FileId>> {
92         if self.changes.is_empty() {
93             return None;
94         }
95         Some(mem::take(&mut self.changes))
96     }
97 }
98
99 fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool {
100     left.source == right.source
101         && left.severity == right.severity
102         && left.range == right.range
103         && left.message == right.message
104 }