2020-01-31 12:23:25 -06:00
|
|
|
//! Book keeping for keeping diagnostics easily in sync with the client.
|
2020-05-14 18:51:48 -05:00
|
|
|
pub(crate) mod to_proto;
|
2020-02-18 05:25:26 -06:00
|
|
|
|
2020-06-29 10:07:52 -05:00
|
|
|
use std::{mem, sync::Arc};
|
2020-02-18 05:25:26 -06:00
|
|
|
|
2020-08-13 10:42:52 -05:00
|
|
|
use ide::FileId;
|
2022-08-25 13:31:02 -05:00
|
|
|
use ide_db::FxHashMap;
|
|
|
|
use stdx::hash::{NoHashHashMap, NoHashHashSet};
|
2020-01-31 12:23:25 -06:00
|
|
|
|
2020-05-17 17:11:40 -05:00
|
|
|
use crate::lsp_ext;
|
|
|
|
|
2022-08-25 13:31:02 -05:00
|
|
|
pub(crate) type CheckFixes = Arc<NoHashHashMap<usize, NoHashHashMap<FileId, Vec<Fix>>>>;
|
2020-01-31 12:23:25 -06:00
|
|
|
|
2020-06-16 15:26:33 -05:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2020-08-18 09:03:15 -05:00
|
|
|
pub struct DiagnosticsMapConfig {
|
2021-04-21 17:09:37 -05:00
|
|
|
pub remap_prefix: FxHashMap<String, String>,
|
2020-06-16 15:26:33 -05:00
|
|
|
pub warnings_as_info: Vec<String>,
|
|
|
|
pub warnings_as_hint: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:23:25 -06:00
|
|
|
#[derive(Debug, Default, Clone)]
|
2020-06-25 15:45:35 -05:00
|
|
|
pub(crate) struct DiagnosticCollection {
|
2022-08-25 13:31:02 -05:00
|
|
|
// FIXME: should be NoHashHashMap<FileId, Vec<ra_id::Diagnostic>>
|
|
|
|
pub(crate) native: NoHashHashMap<FileId, Vec<lsp_types::Diagnostic>>,
|
2020-07-09 08:41:13 -05:00
|
|
|
// FIXME: should be Vec<flycheck::Diagnostic>
|
2022-08-25 13:31:02 -05:00
|
|
|
pub(crate) check: NoHashHashMap<usize, NoHashHashMap<FileId, Vec<lsp_types::Diagnostic>>>,
|
2020-06-25 15:45:35 -05:00
|
|
|
pub(crate) check_fixes: CheckFixes,
|
2022-08-25 13:31:02 -05:00
|
|
|
changes: NoHashHashSet<FileId>,
|
2020-01-31 12:23:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
2020-06-25 15:45:35 -05:00
|
|
|
pub(crate) struct Fix {
|
2021-12-04 22:59:26 -06:00
|
|
|
// Fixes may be triggerable from multiple ranges.
|
|
|
|
pub(crate) ranges: Vec<lsp_types::Range>,
|
2020-06-25 15:45:35 -05:00
|
|
|
pub(crate) action: lsp_ext::CodeAction,
|
2020-01-31 12:23:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl DiagnosticCollection {
|
2022-07-20 04:49:36 -05:00
|
|
|
pub(crate) fn clear_check(&mut self, flycheck_id: usize) {
|
|
|
|
if let Some(it) = Arc::make_mut(&mut self.check_fixes).get_mut(&flycheck_id) {
|
|
|
|
it.clear();
|
|
|
|
}
|
|
|
|
if let Some(it) = self.check.get_mut(&flycheck_id) {
|
|
|
|
self.changes.extend(it.drain().map(|(key, _value)| key));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn clear_check_all(&mut self) {
|
2020-01-31 12:23:25 -06:00
|
|
|
Arc::make_mut(&mut self.check_fixes).clear();
|
2022-07-20 04:49:36 -05:00
|
|
|
self.changes
|
|
|
|
.extend(self.check.values_mut().flat_map(|it| it.drain().map(|(key, _value)| key)))
|
2020-01-31 12:23:25 -06:00
|
|
|
}
|
|
|
|
|
2022-05-25 08:47:41 -05:00
|
|
|
pub(crate) fn clear_native_for(&mut self, file_id: FileId) {
|
|
|
|
self.native.remove(&file_id);
|
|
|
|
self.changes.insert(file_id);
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:45:35 -05:00
|
|
|
pub(crate) fn add_check_diagnostic(
|
2020-01-31 12:23:25 -06:00
|
|
|
&mut self,
|
2022-07-20 04:49:36 -05:00
|
|
|
flycheck_id: usize,
|
2020-01-31 12:23:25 -06:00
|
|
|
file_id: FileId,
|
2020-06-26 05:02:59 -05:00
|
|
|
diagnostic: lsp_types::Diagnostic,
|
2021-12-04 22:59:26 -06:00
|
|
|
fix: Option<Fix>,
|
2020-01-31 12:23:25 -06:00
|
|
|
) {
|
2022-07-20 04:49:36 -05:00
|
|
|
let diagnostics = self.check.entry(flycheck_id).or_default().entry(file_id).or_default();
|
2020-01-31 12:23:25 -06:00
|
|
|
for existing_diagnostic in diagnostics.iter() {
|
2021-06-12 22:54:16 -05:00
|
|
|
if are_diagnostics_equal(existing_diagnostic, &diagnostic) {
|
2020-01-31 12:23:25 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let check_fixes = Arc::make_mut(&mut self.check_fixes);
|
2022-07-20 04:49:36 -05:00
|
|
|
check_fixes.entry(flycheck_id).or_default().entry(file_id).or_default().extend(fix);
|
2020-01-31 12:23:25 -06:00
|
|
|
diagnostics.push(diagnostic);
|
2020-06-25 15:45:35 -05:00
|
|
|
self.changes.insert(file_id);
|
2020-01-31 12:23:25 -06:00
|
|
|
}
|
|
|
|
|
2020-06-26 05:02:59 -05:00
|
|
|
pub(crate) fn set_native_diagnostics(
|
|
|
|
&mut self,
|
|
|
|
file_id: FileId,
|
|
|
|
diagnostics: Vec<lsp_types::Diagnostic>,
|
|
|
|
) {
|
2021-02-17 05:38:11 -06:00
|
|
|
if let Some(existing_diagnostics) = self.native.get(&file_id) {
|
|
|
|
if existing_diagnostics.len() == diagnostics.len()
|
|
|
|
&& diagnostics
|
|
|
|
.iter()
|
|
|
|
.zip(existing_diagnostics)
|
|
|
|
.all(|(new, existing)| are_diagnostics_equal(new, existing))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:23:25 -06:00
|
|
|
self.native.insert(file_id, diagnostics);
|
2020-06-25 15:45:35 -05:00
|
|
|
self.changes.insert(file_id);
|
2020-01-31 12:23:25 -06:00
|
|
|
}
|
|
|
|
|
2020-06-26 05:02:59 -05:00
|
|
|
pub(crate) fn diagnostics_for(
|
|
|
|
&self,
|
|
|
|
file_id: FileId,
|
|
|
|
) -> impl Iterator<Item = &lsp_types::Diagnostic> {
|
2020-01-31 12:23:25 -06:00
|
|
|
let native = self.native.get(&file_id).into_iter().flatten();
|
2022-12-30 02:42:44 -06:00
|
|
|
let check = self.check.values().filter_map(move |it| it.get(&file_id)).flatten();
|
2020-01-31 12:23:25 -06:00
|
|
|
native.chain(check)
|
|
|
|
}
|
|
|
|
|
2022-08-25 13:31:02 -05:00
|
|
|
pub(crate) fn take_changes(&mut self) -> Option<NoHashHashSet<FileId>> {
|
2020-06-25 15:45:35 -05:00
|
|
|
if self.changes.is_empty() {
|
|
|
|
return None;
|
2020-01-31 12:23:25 -06:00
|
|
|
}
|
2020-06-25 15:45:35 -05:00
|
|
|
Some(mem::take(&mut self.changes))
|
2020-01-31 12:23:25 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-26 05:02:59 -05:00
|
|
|
fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool {
|
2020-01-31 12:23:25 -06:00
|
|
|
left.source == right.source
|
|
|
|
&& left.severity == right.severity
|
|
|
|
&& left.range == right.range
|
|
|
|
&& left.message == right.message
|
|
|
|
}
|