rust/crates/ra_hir_expand/src/diagnostics.rs

125 lines
3.9 KiB
Rust
Raw Normal View History

2019-11-02 15:42:38 -05:00
//! Semantic errors and warnings.
//!
//! The `Diagnostic` trait defines a trait object which can represent any
//! diagnostic.
//!
//! `DiagnosticSink` struct is used as an emitter for diagnostic. When creating
//! a `DiagnosticSink`, you supply a callback which can react to a `dyn
//! Diagnostic` or to any concrete diagnostic (downcasting is sued internally).
//!
//! Because diagnostics store file offsets, it's a bad idea to store them
//! directly in salsa. For this reason, every hir subsytem defines it's own
//! strongly-typed closed set of diagnostics which use hir ids internally, are
//! stored in salsa and do *not* implement the `Diagnostic` trait. Instead, a
//! subsystem provides a separate, non-query-based API which can walk all stored
//! values and transform them into instances of `Diagnostic`.
use std::{any::Any, collections::HashSet, fmt};
2019-11-02 15:42:38 -05:00
use ra_syntax::{SyntaxNode, SyntaxNodePtr};
2019-11-02 15:42:38 -05:00
2019-11-28 03:50:26 -06:00
use crate::{db::AstDatabase, InFile};
2019-11-02 15:42:38 -05:00
pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
fn name(&self) -> String;
2019-11-02 15:42:38 -05:00
fn message(&self) -> String;
2019-11-28 03:50:26 -06:00
fn source(&self) -> InFile<SyntaxNodePtr>;
2019-11-02 15:42:38 -05:00
fn as_any(&self) -> &(dyn Any + Send + 'static);
2020-07-24 10:38:18 -05:00
fn is_experimental(&self) -> bool {
false
}
2019-11-02 15:42:38 -05:00
}
pub trait AstDiagnostic {
type AST;
2020-07-01 02:46:27 -05:00
fn ast(&self, db: &dyn AstDatabase) -> Self::AST;
2019-11-02 15:42:38 -05:00
}
impl dyn Diagnostic {
pub fn syntax_node(&self, db: &impl AstDatabase) -> SyntaxNode {
let node = db.parse_or_expand(self.source().file_id).unwrap();
2019-11-20 00:40:36 -06:00
self.source().value.to_node(&node)
2019-11-02 15:42:38 -05:00
}
pub fn downcast_ref<D: Diagnostic>(&self) -> Option<&D> {
self.as_any().downcast_ref()
}
}
pub struct DiagnosticSink<'a> {
callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
2020-07-24 10:38:18 -05:00
filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>,
2019-11-02 15:42:38 -05:00
default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>,
disabled_diagnostics: HashSet<String>,
2019-11-02 15:42:38 -05:00
}
impl<'a> DiagnosticSink<'a> {
pub fn push(&mut self, d: impl Diagnostic) {
if self.disabled_diagnostics.contains(&d.name()) {
// This diagnostic is disabled, ignore it completely.
return;
}
2019-11-02 15:42:38 -05:00
let d: &dyn Diagnostic = &d;
self._push(d);
}
fn _push(&mut self, d: &dyn Diagnostic) {
2020-07-24 10:38:18 -05:00
for filter in &mut self.filters {
if !filter(d) {
return;
}
}
for cb in &mut self.callbacks {
2019-11-02 15:42:38 -05:00
match cb(d) {
Ok(()) => return,
Err(()) => (),
}
}
(self.default_callback)(d)
}
}
2020-07-24 09:30:12 -05:00
pub struct DiagnosticSinkBuilder<'a> {
callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
2020-07-24 10:38:18 -05:00
filters: Vec<Box<dyn FnMut(&dyn Diagnostic) -> bool + 'a>>,
disabled_diagnostics: HashSet<String>,
2020-07-24 09:30:12 -05:00
}
impl<'a> DiagnosticSinkBuilder<'a> {
pub fn new() -> Self {
Self { callbacks: Vec::new(), filters: Vec::new(), disabled_diagnostics: HashSet::new() }
2020-07-24 10:38:18 -05:00
}
pub fn filter<F: FnMut(&dyn Diagnostic) -> bool + 'a>(mut self, cb: F) -> Self {
self.filters.push(Box::new(cb));
self
2020-07-24 09:30:12 -05:00
}
pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> Self {
let cb = move |diag: &dyn Diagnostic| match diag.downcast_ref::<D>() {
Some(d) => {
cb(d);
Ok(())
}
None => Err(()),
};
self.callbacks.push(Box::new(cb));
self
}
pub fn disable_diagnostic(mut self, diagnostic: impl Into<String>) -> Self {
self.disabled_diagnostics.insert(diagnostic.into());
self
}
2020-07-24 09:30:12 -05:00
pub fn build<F: FnMut(&dyn Diagnostic) + 'a>(self, default_callback: F) -> DiagnosticSink<'a> {
2020-07-24 10:38:18 -05:00
DiagnosticSink {
callbacks: self.callbacks,
filters: self.filters,
default_callback: Box::new(default_callback),
disabled_diagnostics: self.disabled_diagnostics,
2020-07-24 10:38:18 -05:00
}
2020-07-24 09:30:12 -05:00
}
}