]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/diagnostics.rs
Merge #7777
[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 warnings_as_info: Vec<String>,
16     pub warnings_as_hint: Vec<String>,
17 }
18
19 #[derive(Debug, Default, Clone)]
20 pub(crate) struct DiagnosticCollection {
21     // FIXME: should be FxHashMap<FileId, Vec<ra_id::Diagnostic>>
22     pub(crate) native: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
23     // FIXME: should be Vec<flycheck::Diagnostic>
24     pub(crate) check: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
25     pub(crate) check_fixes: CheckFixes,
26     changes: FxHashSet<FileId>,
27 }
28
29 #[derive(Debug, Clone)]
30 pub(crate) struct Fix {
31     pub(crate) range: lsp_types::Range,
32     pub(crate) action: lsp_ext::CodeAction,
33 }
34
35 impl DiagnosticCollection {
36     pub(crate) fn clear_check(&mut self) {
37         Arc::make_mut(&mut self.check_fixes).clear();
38         self.changes.extend(self.check.drain().map(|(key, _value)| key))
39     }
40
41     pub(crate) fn add_check_diagnostic(
42         &mut self,
43         file_id: FileId,
44         diagnostic: lsp_types::Diagnostic,
45         fixes: Vec<lsp_ext::CodeAction>,
46     ) {
47         let diagnostics = self.check.entry(file_id).or_default();
48         for existing_diagnostic in diagnostics.iter() {
49             if are_diagnostics_equal(&existing_diagnostic, &diagnostic) {
50                 return;
51             }
52         }
53
54         let check_fixes = Arc::make_mut(&mut self.check_fixes);
55         check_fixes
56             .entry(file_id)
57             .or_default()
58             .extend(fixes.into_iter().map(|action| Fix { range: diagnostic.range, action }));
59         diagnostics.push(diagnostic);
60         self.changes.insert(file_id);
61     }
62
63     pub(crate) fn set_native_diagnostics(
64         &mut self,
65         file_id: FileId,
66         diagnostics: Vec<lsp_types::Diagnostic>,
67     ) {
68         if let Some(existing_diagnostics) = self.native.get(&file_id) {
69             if existing_diagnostics.len() == diagnostics.len()
70                 && diagnostics
71                     .iter()
72                     .zip(existing_diagnostics)
73                     .all(|(new, existing)| are_diagnostics_equal(new, existing))
74             {
75                 return;
76             }
77         }
78
79         self.native.insert(file_id, diagnostics);
80         self.changes.insert(file_id);
81     }
82
83     pub(crate) fn diagnostics_for(
84         &self,
85         file_id: FileId,
86     ) -> impl Iterator<Item = &lsp_types::Diagnostic> {
87         let native = self.native.get(&file_id).into_iter().flatten();
88         let check = self.check.get(&file_id).into_iter().flatten();
89         native.chain(check)
90     }
91
92     pub(crate) fn take_changes(&mut self) -> Option<FxHashSet<FileId>> {
93         if self.changes.is_empty() {
94             return None;
95         }
96         Some(mem::take(&mut self.changes))
97     }
98 }
99
100 fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool {
101     left.source == right.source
102         && left.severity == right.severity
103         && left.range == right.range
104         && left.message == right.message
105 }