Fix double lease panic in Quote Selection command (#12217)

Release Notes:

- Fixed a panic that occurred when using the `assistant: quote
selection` command while signed out.
This commit is contained in:
Max Brunsfeld 2024-05-23 12:14:34 -07:00 committed by GitHub
parent 9c35187aac
commit e15b902974
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 31 additions and 49 deletions

View file

@ -3271,52 +3271,26 @@ impl ConversationEditor {
} }
if let Some(text) = text { if let Some(text) = text {
panel.update(cx, |panel, cx| { panel.update(cx, |_, cx| {
if let Some(conversation) = panel // Wait to create a new conversation until the workspace is no longer
.active_conversation_editor() // being updated.
.cloned() cx.defer(move |panel, cx| {
.or_else(|| panel.new_conversation(cx)) if let Some(conversation) = panel
{ .active_conversation_editor()
conversation.update(cx, |conversation, cx| { .cloned()
conversation .or_else(|| panel.new_conversation(cx))
.editor {
.update(cx, |editor, cx| editor.insert(&text, 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<Workspace>,
// ) {
// let Some(panel) = workspace.panel::<AssistantPanel>(cx) else {
// return;
// };
// if !panel.focus_handle(cx).contains_focused(cx) {
// workspace.toggle_panel_focus::<AssistantPanel>(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<Self>) { fn copy(&mut self, _: &editor::actions::Copy, cx: &mut ViewContext<Self>) {
let editor = self.editor.read(cx); let editor = self.editor.read(cx);
let conversation = self.conversation.read(cx); let conversation = self.conversation.read(cx);

View file

@ -97,12 +97,11 @@ impl EntityMap {
#[track_caller] #[track_caller]
pub fn lease<'a, T>(&mut self, model: &'a Model<T>) -> Lease<'a, T> { pub fn lease<'a, T>(&mut self, model: &'a Model<T>) -> Lease<'a, T> {
self.assert_valid_context(model); self.assert_valid_context(model);
let entity = Some(self.entities.remove(model.entity_id).unwrap_or_else(|| { let entity = Some(
panic!( self.entities
"Circular entity lease of {}. Is it already being updated?", .remove(model.entity_id)
std::any::type_name::<T>() .unwrap_or_else(|| double_lease_panic::<T>("update")),
) );
}));
Lease { Lease {
model, model,
entity, entity,
@ -118,7 +117,9 @@ impl EntityMap {
pub fn read<T: 'static>(&self, model: &Model<T>) -> &T { pub fn read<T: 'static>(&self, model: &Model<T>) -> &T {
self.assert_valid_context(model); 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::<T>("read"))
} }
fn assert_valid_context(&self, model: &AnyModel) { fn assert_valid_context(&self, model: &AnyModel) {
@ -149,6 +150,13 @@ impl EntityMap {
} }
} }
fn double_lease_panic<T>(operation: &str) -> ! {
panic!(
"cannot {operation} {} while it is already being updated",
std::any::type_name::<T>()
)
}
pub(crate) struct Lease<'a, T> { pub(crate) struct Lease<'a, T> {
entity: Option<Box<dyn Any>>, entity: Option<Box<dyn Any>>,
pub model: &'a Model<T>, pub model: &'a Model<T>,