]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/diagnostics.rs
Merge #7172
[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         self.native.insert(file_id, diagnostics);
69         self.changes.insert(file_id);
70     }
71
72     pub(crate) fn diagnostics_for(
73         &self,
74         file_id: FileId,
75     ) -> impl Iterator<Item = &lsp_types::Diagnostic> {
76         let native = self.native.get(&file_id).into_iter().flatten();
77         let check = self.check.get(&file_id).into_iter().flatten();
78         native.chain(check)
79     }
80
81     pub(crate) fn take_changes(&mut self) -> Option<FxHashSet<FileId>> {
82         if self.changes.is_empty() {
83             return None;
84         }
85         Some(mem::take(&mut self.changes))
86     }
87 }
88
89 fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool {
90     left.source == right.source
91         && left.severity == right.severity
92         && left.range == right.range
93         && left.message == right.message
94 }