rust/crates/rust-analyzer/src/diagnostics.rs

95 lines
2.8 KiB
Rust
Raw Normal View History

//! Book keeping for keeping diagnostics easily in sync with the client.
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
use ra_ide::FileId;
2020-06-29 10:07:52 -05:00
use rustc_hash::{FxHashMap, FxHashSet};
2020-05-17 17:11:40 -05:00
use crate::lsp_ext;
2020-06-29 10:07:52 -05:00
pub(crate) type CheckFixes = Arc<FxHashMap<FileId, Vec<Fix>>>;
2020-06-16 15:26:33 -05:00
#[derive(Debug, Default, Clone)]
pub struct DiagnosticsConfig {
pub warnings_as_info: Vec<String>,
pub warnings_as_hint: Vec<String>,
}
#[derive(Debug, Default, Clone)]
2020-06-25 15:45:35 -05:00
pub(crate) struct DiagnosticCollection {
2020-07-09 08:41:13 -05:00
// FIXME: should be FxHashMap<FileId, Vec<ra_id::Diagnostic>>
2020-06-29 10:07:52 -05:00
pub(crate) native: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
2020-07-09 08:41:13 -05:00
// FIXME: should be Vec<flycheck::Diagnostic>
2020-06-29 10:07:52 -05:00
pub(crate) check: FxHashMap<FileId, Vec<lsp_types::Diagnostic>>,
2020-06-25 15:45:35 -05:00
pub(crate) check_fixes: CheckFixes,
changes: FxHashSet<FileId>,
}
#[derive(Debug, Clone)]
2020-06-25 15:45:35 -05:00
pub(crate) struct Fix {
2020-06-26 05:02:59 -05:00
pub(crate) range: lsp_types::Range,
2020-06-25 15:45:35 -05:00
pub(crate) action: lsp_ext::CodeAction,
}
impl DiagnosticCollection {
2020-06-25 15:45:35 -05:00
pub(crate) fn clear_check(&mut self) {
Arc::make_mut(&mut self.check_fixes).clear();
2020-06-25 15:45:35 -05:00
self.changes.extend(self.check.drain().map(|(key, _value)| key))
}
2020-06-25 15:45:35 -05:00
pub(crate) fn add_check_diagnostic(
&mut self,
file_id: FileId,
2020-06-26 05:02:59 -05:00
diagnostic: lsp_types::Diagnostic,
2020-05-17 17:11:40 -05:00
fixes: Vec<lsp_ext::CodeAction>,
) {
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);
2020-06-25 15:45:35 -05:00
self.changes.insert(file_id);
}
2020-06-26 05:02:59 -05:00
pub(crate) fn set_native_diagnostics(
&mut self,
file_id: FileId,
diagnostics: Vec<lsp_types::Diagnostic>,
) {
self.native.insert(file_id, diagnostics);
2020-06-25 15:45:35 -05:00
self.changes.insert(file_id);
}
2020-06-26 05:02:59 -05:00
pub(crate) fn diagnostics_for(
&self,
file_id: FileId,
) -> impl Iterator<Item = &lsp_types::Diagnostic> {
let native = self.native.get(&file_id).into_iter().flatten();
let check = self.check.get(&file_id).into_iter().flatten();
native.chain(check)
}
2020-06-25 15:45:35 -05:00
pub(crate) fn take_changes(&mut self) -> Option<FxHashSet<FileId>> {
if self.changes.is_empty() {
return None;
}
2020-06-25 15:45:35 -05:00
Some(mem::take(&mut self.changes))
}
}
2020-06-26 05:02:59 -05:00
fn are_diagnostics_equal(left: &lsp_types::Diagnostic, right: &lsp_types::Diagnostic) -> bool {
left.source == right.source
&& left.severity == right.severity
&& left.range == right.range
&& left.message == right.message
}