From 794446bf8bd43afee0a2fb9269910c9d0c36b5ab Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 2 May 2023 11:03:01 +0200 Subject: [PATCH] Move `debug_elements` to `AsyncAppContext` Previously, `debug_elements` was available on `WindowContext`. If that method was called while having a borrow out to a view, it would panic because the view would already have been borrowed. By moving it to an `AsyncAppContext` we ensure the method can't be called while a view is being used. --- crates/gpui/src/app.rs | 9 +++++++++ crates/gpui/src/app/window.rs | 19 +------------------ crates/gpui/src/elements.rs | 7 ++++++- crates/zed/src/zed.rs | 8 +++++++- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/crates/gpui/src/app.rs b/crates/gpui/src/app.rs index 589ec8f1af..3cc90dd4ce 100644 --- a/crates/gpui/src/app.rs +++ b/crates/gpui/src/app.rs @@ -43,6 +43,7 @@ use window_input_handler::WindowInputHandler; use crate::{ elements::{AnyElement, AnyRootElement, RootElement}, executor::{self, Task}, + json, keymap_matcher::{self, Binding, KeymapContext, KeymapMatcher, Keystroke, MatchResult}, platform::{ self, FontSystem, KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, MouseButton, @@ -309,6 +310,14 @@ impl AsyncAppContext { self.0.borrow_mut().update_window(window_id, callback) } + pub fn debug_elements(&self, window_id: usize) -> Option { + self.0.borrow().read_window(window_id, |cx| { + let root_view = cx.window.root_view(); + let root_element = cx.window.rendered_views.get(&root_view.id())?; + root_element.debug(cx).log_err() + })? + } + pub fn dispatch_action( &mut self, window_id: usize, diff --git a/crates/gpui/src/app/window.rs b/crates/gpui/src/app/window.rs index f54c18c755..49befafbec 100644 --- a/crates/gpui/src/app/window.rs +++ b/crates/gpui/src/app/window.rs @@ -1,7 +1,7 @@ use crate::{ elements::AnyRootElement, geometry::rect::RectF, - json::{self, ToJson}, + json::ToJson, keymap_matcher::{Binding, Keystroke, MatchResult}, platform::{ self, Appearance, CursorStyle, Event, KeyDownEvent, KeyUpEvent, ModifiersChangedEvent, @@ -975,17 +975,6 @@ impl<'a> WindowContext<'a> { .flatten() } - pub fn debug_elements(&self) -> Option { - let view = self.window.root_view(); - Some(json!({ - "root_view": view.debug_json(self), - "root_element": self.window.rendered_views.get(&view.id()) - .and_then(|root_element| { - root_element.debug(self).log_err() - }) - })) - } - pub fn set_window_title(&mut self, title: &str) { self.window.platform_window.set_title(title); } @@ -1454,13 +1443,7 @@ impl Element for ChildView { ) -> serde_json::Value { json!({ "type": "ChildView", - "view_id": self.view_id, "bounds": bounds.to_json(), - "view": if let Some(view) = cx.views.get(&(cx.window_id, self.view_id)) { - view.debug_json(cx) - } else { - json!(null) - }, "child": if let Some(element) = cx.window.rendered_views.get(&self.view_id) { element.debug(&cx.window_context).log_err().unwrap_or_else(|| json!(null)) } else { diff --git a/crates/gpui/src/elements.rs b/crates/gpui/src/elements.rs index dbeb9c218a..7de0bc10f5 100644 --- a/crates/gpui/src/elements.rs +++ b/crates/gpui/src/elements.rs @@ -708,7 +708,12 @@ impl AnyRootElement for RootElement { .ok_or_else(|| anyhow!("debug called on a root element for a dropped view"))?; let view = view.read(cx); let view_context = ViewContext::immutable(cx, self.view.id()); - Ok(self.element.debug(view, &view_context)) + Ok(serde_json::json!({ + "view_id": self.view.id(), + "view_name": V::ui_name(), + "view": view.debug_json(cx), + "element": self.element.debug(view, &view_context) + })) } fn name(&self) -> Option<&str> { diff --git a/crates/zed/src/zed.rs b/crates/zed/src/zed.rs index 9e0b55d423..5160426bdd 100644 --- a/crates/zed/src/zed.rs +++ b/crates/zed/src/zed.rs @@ -11,6 +11,7 @@ use collections::VecDeque; pub use editor; use editor::{Editor, MultiBuffer}; +use anyhow::anyhow; use feedback::{ feedback_info_text::FeedbackInfoText, submit_feedback_button::SubmitFeedbackButton, }; @@ -215,9 +216,14 @@ pub fn init(app_state: &Arc, cx: &mut gpui::AppContext) { move |workspace: &mut Workspace, _: &DebugElements, cx: &mut ViewContext| { let app_state = workspace.app_state().clone(); let markdown = app_state.languages.language_for_name("JSON"); - let content = to_string_pretty(&cx.debug_elements()).unwrap(); + let window_id = cx.window_id(); cx.spawn(|workspace, mut cx| async move { let markdown = markdown.await.log_err(); + let content = to_string_pretty( + &cx.debug_elements(window_id) + .ok_or_else(|| anyhow!("could not debug elements for {window_id}"))?, + ) + .unwrap(); workspace .update(&mut cx, |workspace, cx| { workspace.with_local_workspace(cx, move |workspace, cx| {