From 76509b3d2dde6770aa98d798ba8f50512825f266 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Fri, 24 Jul 2020 17:38:18 +0200 Subject: Allow filtering diagnostics --- crates/ra_hir_expand/src/diagnostics.rs | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'crates') diff --git a/crates/ra_hir_expand/src/diagnostics.rs b/crates/ra_hir_expand/src/diagnostics.rs index 6a5844f31..84ba97b14 100644 --- a/crates/ra_hir_expand/src/diagnostics.rs +++ b/crates/ra_hir_expand/src/diagnostics.rs @@ -24,6 +24,9 @@ pub trait Diagnostic: Any + Send + Sync + fmt::Debug + 'static { fn message(&self) -> String; fn source(&self) -> InFile; fn as_any(&self) -> &(dyn Any + Send + 'static); + fn is_experimental(&self) -> bool { + false + } } pub trait AstDiagnostic { @@ -44,6 +47,7 @@ impl dyn Diagnostic { pub struct DiagnosticSink<'a> { callbacks: Vec Result<(), ()> + 'a>>, + filters: Vec bool + 'a>>, default_callback: Box, } @@ -54,7 +58,12 @@ impl<'a> DiagnosticSink<'a> { } fn _push(&mut self, d: &dyn Diagnostic) { - for cb in self.callbacks.iter_mut() { + for filter in &mut self.filters { + if !filter(d) { + return; + } + } + for cb in &mut self.callbacks { match cb(d) { Ok(()) => return, Err(()) => (), @@ -66,11 +75,17 @@ impl<'a> DiagnosticSink<'a> { pub struct DiagnosticSinkBuilder<'a> { callbacks: Vec Result<(), ()> + 'a>>, + filters: Vec bool + 'a>>, } impl<'a> DiagnosticSinkBuilder<'a> { pub fn new() -> Self { - Self { callbacks: Vec::new() } + Self { callbacks: Vec::new(), filters: Vec::new() } + } + + pub fn filter bool + 'a>(mut self, cb: F) -> Self { + self.filters.push(Box::new(cb)); + self } pub fn on(mut self, mut cb: F) -> Self { @@ -86,6 +101,10 @@ impl<'a> DiagnosticSinkBuilder<'a> { } pub fn build(self, default_callback: F) -> DiagnosticSink<'a> { - DiagnosticSink { callbacks: self.callbacks, default_callback: Box::new(default_callback) } + DiagnosticSink { + callbacks: self.callbacks, + filters: self.filters, + default_callback: Box::new(default_callback), + } } } -- cgit v1.2.3