diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index eedb77c9be1fae401b1dfdf829fec775c501aa49..62fc3bf321809b6074a59552b84ec290d3c8f0f7 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -193,7 +193,10 @@ impl ProjectDiagnosticsEditor { } } - workspace.update(cx, |workspace, cx| { + // We defer the pane interaction because we ourselves are a workspace item + // and activating a new item causes the pane to call a method on us reentrantly, + // which panics if we're on the stack. + workspace.defer(cx, |workspace, cx| { for (buffer, ranges) in new_selections_by_buffer { let buffer = BufferItemHandle(buffer); if !workspace.activate_pane_for_item(&buffer, cx) { diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 11a8760700ba4323ad6ffe2ddc6ba7edf8b7d7ea..31efb4991ea73b1b83cc03fbdd6546acb4d126e2 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -1060,6 +1060,10 @@ impl MutableAppContext { } } + fn defer(&mut self, callback: Box) { + self.pending_effects.push_back(Effect::Deferred(callback)) + } + pub(crate) fn notify_model(&mut self, model_id: usize) { if self.pending_notifications.insert(model_id) { self.pending_effects @@ -1443,6 +1447,7 @@ impl MutableAppContext { Effect::ViewNotification { window_id, view_id } => { self.notify_view_observers(window_id, view_id) } + Effect::Deferred(callback) => callback(self), Effect::Release { entity_id } => self.notify_release_observers(entity_id), Effect::Focus { window_id, view_id } => { self.focus(window_id, view_id); @@ -1876,6 +1881,7 @@ pub enum Effect { window_id: usize, view_id: usize, }, + Deferred(Box), Release { entity_id: usize, }, @@ -1905,6 +1911,7 @@ impl Debug for Effect { .field("window_id", window_id) .field("view_id", view_id) .finish(), + Effect::Deferred(_) => f.debug_struct("Effect::Deferred").finish(), Effect::Release { entity_id } => f .debug_struct("Effect::Release") .field("entity_id", entity_id) @@ -2410,6 +2417,15 @@ impl<'a, T: View> ViewContext<'a, T> { self.app.notify_view(self.window_id, self.view_id); } + pub fn defer(&mut self, callback: impl 'static + FnOnce(&mut T, &mut ViewContext)) { + let handle = self.handle(); + self.app.defer(Box::new(move |cx| { + handle.update(cx, |view, cx| { + callback(view, cx); + }) + })) + } + pub fn propagate_action(&mut self) { self.halt_action_dispatch = false; } @@ -2922,6 +2938,17 @@ impl ViewHandle { }) } + pub fn defer(&self, cx: &mut C, update: F) + where + C: AsMut, + F: 'static + FnOnce(&mut T, &mut ViewContext), + { + let this = self.clone(); + cx.as_mut().defer(Box::new(move |cx| { + this.update(cx, |view, cx| update(view, cx)); + })); + } + pub fn is_focused(&self, cx: &AppContext) -> bool { cx.focused_view_id(self.window_id) .map_or(false, |focused_id| focused_id == self.view_id)