aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_lsp_server/src/diagnostics.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-02-18 11:37:45 +0000
committerAleksey Kladov <[email protected]>2020-02-18 11:37:45 +0000
commit865759925be6b72f7ef39124ed0e4c86c0412a69 (patch)
tree0fc36373073a66c2bbd6c7cfae6cb734527d847f /crates/ra_lsp_server/src/diagnostics.rs
parentc855e36696afa54260773a6bc8a358df67d60dea (diff)
Rename folder
Diffstat (limited to 'crates/ra_lsp_server/src/diagnostics.rs')
-rw-r--r--crates/ra_lsp_server/src/diagnostics.rs87
1 files changed, 0 insertions, 87 deletions
diff --git a/crates/ra_lsp_server/src/diagnostics.rs b/crates/ra_lsp_server/src/diagnostics.rs
deleted file mode 100644
index e7924f0a3..000000000
--- a/crates/ra_lsp_server/src/diagnostics.rs
+++ /dev/null
@@ -1,87 +0,0 @@
1//! Book keeping for keeping diagnostics easily in sync with the client.
2
3use std::{collections::HashMap, sync::Arc};
4
5use lsp_types::{CodeActionOrCommand, Diagnostic, Range};
6use ra_ide::FileId;
7
8pub type CheckFixes = Arc<HashMap<FileId, Vec<Fix>>>;
9
10#[derive(Debug, Default, Clone)]
11pub struct DiagnosticCollection {
12 pub native: HashMap<FileId, Vec<Diagnostic>>,
13 pub check: HashMap<FileId, Vec<Diagnostic>>,
14 pub check_fixes: CheckFixes,
15}
16
17#[derive(Debug, Clone)]
18pub struct Fix {
19 pub range: Range,
20 pub action: CodeActionOrCommand,
21}
22
23#[derive(Debug)]
24pub enum DiagnosticTask {
25 ClearCheck,
26 AddCheck(FileId, Diagnostic, Vec<CodeActionOrCommand>),
27 SetNative(FileId, Vec<Diagnostic>),
28}
29
30impl DiagnosticCollection {
31 pub fn clear_check(&mut self) -> Vec<FileId> {
32 Arc::make_mut(&mut self.check_fixes).clear();
33 self.check.drain().map(|(key, _value)| key).collect()
34 }
35
36 pub fn add_check_diagnostic(
37 &mut self,
38 file_id: FileId,
39 diagnostic: Diagnostic,
40 fixes: Vec<CodeActionOrCommand>,
41 ) {
42 let diagnostics = self.check.entry(file_id).or_default();
43 for existing_diagnostic in diagnostics.iter() {
44 if are_diagnostics_equal(&existing_diagnostic, &diagnostic) {
45 return;
46 }
47 }
48
49 let check_fixes = Arc::make_mut(&mut self.check_fixes);
50 check_fixes
51 .entry(file_id)
52 .or_default()
53 .extend(fixes.into_iter().map(|action| Fix { range: diagnostic.range, action }));
54 diagnostics.push(diagnostic);
55 }
56
57 pub fn set_native_diagnostics(&mut self, file_id: FileId, diagnostics: Vec<Diagnostic>) {
58 self.native.insert(file_id, diagnostics);
59 }
60
61 pub fn diagnostics_for(&self, file_id: FileId) -> impl Iterator<Item = &Diagnostic> {
62 let native = self.native.get(&file_id).into_iter().flatten();
63 let check = self.check.get(&file_id).into_iter().flatten();
64 native.chain(check)
65 }
66
67 pub fn handle_task(&mut self, task: DiagnosticTask) -> Vec<FileId> {
68 match task {
69 DiagnosticTask::ClearCheck => self.clear_check(),
70 DiagnosticTask::AddCheck(file_id, diagnostic, fixes) => {
71 self.add_check_diagnostic(file_id, diagnostic, fixes);
72 vec![file_id]
73 }
74 DiagnosticTask::SetNative(file_id, diagnostics) => {
75 self.set_native_diagnostics(file_id, diagnostics);
76 vec![file_id]
77 }
78 }
79 }
80}
81
82fn are_diagnostics_equal(left: &Diagnostic, right: &Diagnostic) -> bool {
83 left.source == right.source
84 && left.severity == right.severity
85 && left.range == right.range
86 && left.message == right.message
87}