Merge pull request #359 from zed-industries/defer

Fix panic in ProjectDiagnosticsEditor::open_excerpts by introducing new defer feature to GPUI
This commit is contained in:
Nathan Sobo 2022-01-22 13:44:29 -07:00 committed by GitHub
commit 2a6f06f18a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View file

@ -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) {

View file

@ -1060,6 +1060,10 @@ impl MutableAppContext {
}
}
fn defer(&mut self, callback: Box<dyn FnOnce(&mut MutableAppContext)>) {
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<dyn FnOnce(&mut MutableAppContext)>),
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<T>)) {
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<T: View> ViewHandle<T> {
})
}
pub fn defer<C, F>(&self, cx: &mut C, update: F)
where
C: AsMut<MutableAppContext>,
F: 'static + FnOnce(&mut T, &mut ViewContext<T>),
{
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)