diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 10527b2a3d9157bc26b636841efe819a550bfd78..1928dab71731dfb48c14ad43cf9b4e60529642d7 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -725,6 +725,10 @@ impl Buffer { cx.notify(); } + pub fn all_diagnostics<'a>(&'a self) -> impl 'a + Iterator> { + self.diagnostics.iter() + } + pub fn update_diagnostics( &mut self, version: Option, @@ -1859,15 +1863,6 @@ impl BufferSnapshot { }) } - pub fn all_diagnostics<'a, O>(&'a self) -> impl 'a + Iterator> - where - O: 'a + FromAnchor, - { - self.diagnostics - .iter() - .map(|diagnostic| diagnostic.resolve(self)) - } - pub fn diagnostics_in_range<'a, T, O>( &'a self, search_range: Range, diff --git a/crates/project/src/project.rs b/crates/project/src/project.rs index ae2730e9cbcdf0c043c90f0f601c78268efccc95..d0adbff7932590cfa32fd9efa175a4e6f5112bdc 100644 --- a/crates/project/src/project.rs +++ b/crates/project/src/project.rs @@ -78,7 +78,7 @@ pub struct DiagnosticSummary { } impl DiagnosticSummary { - fn new(diagnostics: &[DiagnosticEntry]) -> Self { + fn new<'a, T: 'a>(diagnostics: impl IntoIterator>) -> Self { let mut this = Self { error_count: 0, warning_count: 0, diff --git a/crates/project/src/worktree.rs b/crates/project/src/worktree.rs index 53ea5f1bf84cc660b3c001e54bdc56d066fde00b..dcb1f401d64b2d7040990f2a3740377c4abf077d 100644 --- a/crates/project/src/worktree.rs +++ b/crates/project/src/worktree.rs @@ -1097,15 +1097,43 @@ impl LocalWorktree { buffer }); - this.update(&mut cx, |this, _| { + this.update(&mut cx, |this, cx| { let this = this.as_local_mut().unwrap(); this.open_buffers.insert(buffer.id(), buffer.downgrade()); + cx.subscribe(&buffer, |worktree, buffer, event, cx| { + worktree + .as_local_mut() + .unwrap() + .on_buffer_event(buffer, event, cx); + }) + .detach(); }); Ok(buffer) }) } + fn on_buffer_event( + &mut self, + buffer: ModelHandle, + event: &language::Event, + cx: &mut ModelContext, + ) { + match event { + language::Event::DiagnosticsUpdated => { + let buffer = buffer.read(cx); + if let Some(path) = buffer.file().map(|file| file.path().clone()) { + let diagnostics = buffer.all_diagnostics(); + self.diagnostic_summaries + .insert(path.clone(), DiagnosticSummary::new(diagnostics)); + cx.emit(Event::DiagnosticsUpdated(path)); + cx.notify(); + } + } + _ => {} + } + } + pub fn open_remote_buffer( &mut self, envelope: TypedEnvelope,