From bf2151e4d46e8d058cfa383a8a1da48f5bdec82e Mon Sep 17 00:00:00 2001 From: Cole Miller Date: Mon, 5 Jan 2026 09:19:20 -0500 Subject: [PATCH] buffer_diff: Synchrously emit `DiffChanged` event from `recalculate_diff_sync` (#46077) Release Notes: - N/A --- crates/buffer_diff/src/buffer_diff.rs | 53 ++++++++++++++++++++------- 1 file changed, 40 insertions(+), 13 deletions(-) diff --git a/crates/buffer_diff/src/buffer_diff.rs b/crates/buffer_diff/src/buffer_diff.rs index 3bf1adae49d67630469ebdbd3a625e10c1ead432..d06d9a4f3eabac27e13f5b48314afbffe2928500 100644 --- a/crates/buffer_diff/src/buffer_diff.rs +++ b/crates/buffer_diff/src/buffer_diff.rs @@ -1310,23 +1310,14 @@ impl BufferDiff { .detach(); } - pub fn set_snapshot( - &mut self, - new_state: BufferDiffUpdate, - buffer: &text::BufferSnapshot, - cx: &mut Context, - ) -> Task>> { - self.set_snapshot_with_secondary(new_state, buffer, None, false, cx) - } - - pub fn set_snapshot_with_secondary( + fn set_snapshot_with_secondary_inner( &mut self, update: BufferDiffUpdate, buffer: &text::BufferSnapshot, secondary_diff_change: Option>, clear_pending_hunks: bool, cx: &mut Context, - ) -> Task>> { + ) -> impl Future>, Option>)> + use<> { log::debug!("set snapshot with secondary {secondary_diff_change:?}"); let old_snapshot = self.snapshot(cx); @@ -1398,10 +1389,41 @@ impl BufferDiff { state.pending_hunks = SumTree::new(buffer); } - cx.spawn(async move |this, cx| { + async move { if let Some(parsing_idle) = parsing_idle { parsing_idle.await; } + (changed_range, base_text_changed_range) + } + } + + pub fn set_snapshot( + &mut self, + new_state: BufferDiffUpdate, + buffer: &text::BufferSnapshot, + cx: &mut Context, + ) -> Task>> { + self.set_snapshot_with_secondary(new_state, buffer, None, false, cx) + } + + pub fn set_snapshot_with_secondary( + &mut self, + update: BufferDiffUpdate, + buffer: &text::BufferSnapshot, + secondary_diff_change: Option>, + clear_pending_hunks: bool, + cx: &mut Context, + ) -> Task>> { + let fut = self.set_snapshot_with_secondary_inner( + update, + buffer, + secondary_diff_change, + clear_pending_hunks, + cx, + ); + + cx.spawn(async move |this, cx| { + let (changed_range, base_text_changed_range) = fut.await; this.update(cx, |_, cx| { cx.emit(BufferDiffEvent::DiffChanged { changed_range: changed_range.clone(), @@ -1483,7 +1505,12 @@ impl BufferDiff { let fut = self.update_diff(buffer.clone(), base_text, false, language, cx); let executor = cx.background_executor().clone(); let snapshot = executor.block(fut); - self.set_snapshot(snapshot, &buffer, cx).detach(); + let fut = self.set_snapshot_with_secondary_inner(snapshot, buffer, None, false, cx); + let (changed_range, base_text_changed_range) = executor.block(fut); + cx.emit(BufferDiffEvent::DiffChanged { + changed_range, + base_text_changed_range, + }) } pub fn base_text_buffer(&self) -> Entity {