diff --git a/crates/assistant/src/assistant_panel.rs b/crates/assistant/src/assistant_panel.rs index 24dc91ddd7..a5f185c03a 100644 --- a/crates/assistant/src/assistant_panel.rs +++ b/crates/assistant/src/assistant_panel.rs @@ -3271,52 +3271,26 @@ impl ConversationEditor { } if let Some(text) = text { - panel.update(cx, |panel, cx| { - if let Some(conversation) = panel - .active_conversation_editor() - .cloned() - .or_else(|| panel.new_conversation(cx)) - { - conversation.update(cx, |conversation, cx| { - conversation - .editor - .update(cx, |editor, cx| editor.insert(&text, cx)) - }); - }; + panel.update(cx, |_, cx| { + // Wait to create a new conversation until the workspace is no longer + // being updated. + cx.defer(move |panel, cx| { + if let Some(conversation) = panel + .active_conversation_editor() + .cloned() + .or_else(|| panel.new_conversation(cx)) + { + conversation.update(cx, |conversation, cx| { + conversation + .editor + .update(cx, |editor, cx| editor.insert(&text, cx)) + }); + }; + }); }); } } - // fn insert_active_prompt( - // workspace: &mut Workspace, - // _: &InsertActivePrompt, - // cx: &mut ViewContext, - // ) { - // let Some(panel) = workspace.panel::(cx) else { - // return; - // }; - - // if !panel.focus_handle(cx).contains_focused(cx) { - // workspace.toggle_panel_focus::(cx); - // } - - // if let Some(default_prompt) = panel.read(cx).prompt_library.clone().default_prompt() { - // panel.update(cx, |panel, cx| { - // if let Some(conversation) = panel - // .active_conversation_editor() - // .cloned() - // .or_else(|| panel.new_conversation(cx)) - // { - // conversation.update(cx, |conversation, cx| { - // conversation - // .editor - // .update(cx, |editor, cx| editor.insert(&default_prompt, cx)) - // }); - // }; - // }); - // }; - // } - fn copy(&mut self, _: &editor::actions::Copy, cx: &mut ViewContext) { let editor = self.editor.read(cx); let conversation = self.conversation.read(cx); diff --git a/crates/gpui/src/app/entity_map.rs b/crates/gpui/src/app/entity_map.rs index b5ef39eada..7655e4b6be 100644 --- a/crates/gpui/src/app/entity_map.rs +++ b/crates/gpui/src/app/entity_map.rs @@ -97,12 +97,11 @@ impl EntityMap { #[track_caller] pub fn lease<'a, T>(&mut self, model: &'a Model) -> Lease<'a, T> { self.assert_valid_context(model); - let entity = Some(self.entities.remove(model.entity_id).unwrap_or_else(|| { - panic!( - "Circular entity lease of {}. Is it already being updated?", - std::any::type_name::() - ) - })); + let entity = Some( + self.entities + .remove(model.entity_id) + .unwrap_or_else(|| double_lease_panic::("update")), + ); Lease { model, entity, @@ -118,7 +117,9 @@ impl EntityMap { pub fn read(&self, model: &Model) -> &T { self.assert_valid_context(model); - self.entities[model.entity_id].downcast_ref().unwrap() + self.entities[model.entity_id] + .downcast_ref() + .unwrap_or_else(|| double_lease_panic::("read")) } fn assert_valid_context(&self, model: &AnyModel) { @@ -149,6 +150,13 @@ impl EntityMap { } } +fn double_lease_panic(operation: &str) -> ! { + panic!( + "cannot {operation} {} while it is already being updated", + std::any::type_name::() + ) +} + pub(crate) struct Lease<'a, T> { entity: Option>, pub model: &'a Model,