mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
more enterprisey diagnostics setup
This commit is contained in:
parent
7ee2887d1e
commit
c7ffd939f6
3 changed files with 75 additions and 59 deletions
|
@ -1,9 +1,9 @@
|
|||
use std::{fmt, any::Any};
|
||||
|
||||
use ra_syntax::{SyntaxNodePtr, AstPtr, ast};
|
||||
use relative_path::RelativePathBuf;
|
||||
|
||||
use crate::HirFileId;
|
||||
use relative_path::RelativePathBuf;
|
||||
|
||||
/// Diagnostic defines hir API for errors and warnings.
|
||||
///
|
||||
|
@ -21,7 +21,7 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static {
|
|||
fn message(&self) -> String;
|
||||
fn file(&self) -> HirFileId;
|
||||
fn syntax_node(&self) -> SyntaxNodePtr;
|
||||
fn as_any(&self) -> &(Any + Send + 'static);
|
||||
fn as_any(&self) -> &(dyn Any + Send + 'static);
|
||||
}
|
||||
|
||||
impl dyn Diagnostic {
|
||||
|
@ -30,18 +30,37 @@ impl dyn Diagnostic {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct DiagnosticSink {
|
||||
data: Vec<Box<dyn Diagnostic>>,
|
||||
pub struct DiagnosticSink<'a> {
|
||||
callbacks: Vec<Box<dyn FnMut(&dyn Diagnostic) -> Result<(), ()> + 'a>>,
|
||||
default_callback: Box<dyn FnMut(&dyn Diagnostic) + 'a>,
|
||||
}
|
||||
|
||||
impl DiagnosticSink {
|
||||
pub fn push(&mut self, d: impl Diagnostic) {
|
||||
self.data.push(Box::new(d))
|
||||
impl<'a> DiagnosticSink<'a> {
|
||||
pub fn new(cb: impl FnMut(&dyn Diagnostic) + 'a) -> DiagnosticSink<'a> {
|
||||
DiagnosticSink { callbacks: Vec::new(), default_callback: Box::new(cb) }
|
||||
}
|
||||
|
||||
pub fn into_diagnostics(self) -> Vec<Box<dyn Diagnostic>> {
|
||||
self.data
|
||||
pub fn on<D: Diagnostic, F: FnMut(&D) + 'a>(mut self, mut cb: F) -> DiagnosticSink<'a> {
|
||||
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(crate) fn push(&mut self, d: impl Diagnostic) {
|
||||
let d: &dyn Diagnostic = &d;
|
||||
for cb in self.callbacks.iter_mut() {
|
||||
match cb(d) {
|
||||
Ok(()) => return,
|
||||
Err(()) => (),
|
||||
}
|
||||
}
|
||||
(self.default_callback)(d)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue