From 790788d5f4013d8d92f110bc12a581d18cf4b6ae Mon Sep 17 00:00:00 2001 From: Emil Lauridsen Date: Fri, 31 Jan 2020 19:23:25 +0100 Subject: Rework how we send diagnostics to client. The previous way of sending from the thread pool suffered from stale diagnostics due to being canceled before we could clear the old ones. The key change is moving to sending diagnostics from the main loop thread, but doing all the hard work in the thread pool. This should provide the best of both worlds, with little to no of the downsides. This should hopefully fix a lot of issues, but we'll need testing in each individual issue to be sure. --- crates/ra_lsp_server/src/diagnostics.rs | 85 +++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 crates/ra_lsp_server/src/diagnostics.rs (limited to 'crates/ra_lsp_server/src/diagnostics.rs') diff --git a/crates/ra_lsp_server/src/diagnostics.rs b/crates/ra_lsp_server/src/diagnostics.rs new file mode 100644 index 000000000..ea08bce24 --- /dev/null +++ b/crates/ra_lsp_server/src/diagnostics.rs @@ -0,0 +1,85 @@ +//! Book keeping for keeping diagnostics easily in sync with the client. +use lsp_types::{CodeActionOrCommand, Diagnostic, Range}; +use ra_ide::FileId; +use std::{collections::HashMap, sync::Arc}; + +pub type CheckFixes = Arc>>; + +#[derive(Debug, Default, Clone)] +pub struct DiagnosticCollection { + pub native: HashMap>, + pub check: HashMap>, + pub check_fixes: CheckFixes, +} + +#[derive(Debug, Clone)] +pub struct Fix { + pub range: Range, + pub action: CodeActionOrCommand, +} + +#[derive(Debug)] +pub enum DiagnosticTask { + ClearCheck, + AddCheck(FileId, Diagnostic, Vec), + SetNative(FileId, Vec), +} + +impl DiagnosticCollection { + pub fn clear_check(&mut self) -> Vec { + Arc::make_mut(&mut self.check_fixes).clear(); + self.check.drain().map(|(key, _value)| key).collect() + } + + pub fn add_check_diagnostic( + &mut self, + file_id: FileId, + diagnostic: Diagnostic, + fixes: Vec, + ) { + let diagnostics = self.check.entry(file_id).or_default(); + for existing_diagnostic in diagnostics.iter() { + if are_diagnostics_equal(&existing_diagnostic, &diagnostic) { + return; + } + } + + let check_fixes = Arc::make_mut(&mut self.check_fixes); + check_fixes + .entry(file_id) + .or_default() + .extend(fixes.into_iter().map(|action| Fix { range: diagnostic.range, action })); + diagnostics.push(diagnostic); + } + + pub fn set_native_diagnostics(&mut self, file_id: FileId, diagnostics: Vec) { + self.native.insert(file_id, diagnostics); + } + + pub fn diagnostics_for(&self, file_id: FileId) -> impl Iterator { + let native = self.native.get(&file_id).into_iter().flatten(); + let check = self.check.get(&file_id).into_iter().flatten(); + native.chain(check) + } + + pub fn handle_task(&mut self, task: DiagnosticTask) -> Vec { + match task { + DiagnosticTask::ClearCheck => self.clear_check(), + DiagnosticTask::AddCheck(file_id, diagnostic, fixes) => { + self.add_check_diagnostic(file_id, diagnostic, fixes); + vec![file_id] + } + DiagnosticTask::SetNative(file_id, diagnostics) => { + self.set_native_diagnostics(file_id, diagnostics); + vec![file_id] + } + } + } +} + +fn are_diagnostics_equal(left: &Diagnostic, right: &Diagnostic) -> bool { + left.source == right.source + && left.severity == right.severity + && left.range == right.range + && left.message == right.message +} -- cgit v1.2.3