Cancel last inline assist when escaping from the editor

This commit is contained in:
Antonio Scandurra 2023-08-25 15:54:52 +02:00
parent fdbf4680bb
commit b101a7edff

View file

@ -7,7 +7,7 @@ use crate::{
}; };
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use chrono::{DateTime, Local}; use chrono::{DateTime, Local};
use collections::{HashMap, HashSet}; use collections::{hash_map, HashMap, HashSet};
use editor::{ use editor::{
display_map::{ display_map::{
BlockContext, BlockDisposition, BlockId, BlockProperties, BlockStyle, ToDisplayPoint, BlockContext, BlockDisposition, BlockId, BlockProperties, BlockStyle, ToDisplayPoint,
@ -93,6 +93,7 @@ pub fn init(cx: &mut AppContext) {
}, },
); );
cx.add_action(AssistantPanel::inline_assist); cx.add_action(AssistantPanel::inline_assist);
cx.add_action(AssistantPanel::cancel_last_inline_assist);
cx.add_action(InlineAssistant::confirm); cx.add_action(InlineAssistant::confirm);
cx.add_action(InlineAssistant::cancel); cx.add_action(InlineAssistant::cancel);
} }
@ -347,25 +348,64 @@ impl AssistantPanel {
self.confirm_inline_assist(assist_id, prompt, cx); self.confirm_inline_assist(assist_id, prompt, cx);
} }
InlineAssistantEvent::Canceled => { InlineAssistantEvent::Canceled => {
self.complete_inline_assist(assist_id, true, cx); self.close_inline_assist(assist_id, true, cx);
} }
InlineAssistantEvent::Dismissed => { InlineAssistantEvent::Dismissed => {
self.dismiss_inline_assist(assist_id, cx); self.hide_inline_assist(assist_id, cx);
} }
} }
} }
fn complete_inline_assist( fn cancel_last_inline_assist(
&mut self, workspace: &mut Workspace,
assist_id: usize, _: &editor::Cancel,
cancel: bool, cx: &mut ViewContext<Workspace>,
cx: &mut ViewContext<Self>,
) { ) {
self.dismiss_inline_assist(assist_id, cx); let panel = if let Some(panel) = workspace.panel::<AssistantPanel>(cx) {
panel
} else {
return;
};
let editor = if let Some(editor) = workspace
.active_item(cx)
.and_then(|item| item.downcast::<Editor>())
{
editor
} else {
return;
};
let handled = panel.update(cx, |panel, cx| {
if let Some(assist_id) = panel
.pending_inline_assist_ids_by_editor
.get(&editor.downgrade())
.and_then(|assist_ids| assist_ids.last().copied())
{
panel.close_inline_assist(assist_id, true, cx);
true
} else {
false
}
});
if !handled {
cx.propagate_action();
}
}
fn close_inline_assist(&mut self, assist_id: usize, cancel: bool, cx: &mut ViewContext<Self>) {
self.hide_inline_assist(assist_id, cx);
if let Some(pending_assist) = self.pending_inline_assists.remove(&assist_id) { if let Some(pending_assist) = self.pending_inline_assists.remove(&assist_id) {
self.pending_inline_assist_ids_by_editor if let hash_map::Entry::Occupied(mut entry) = self
.remove(&pending_assist.editor); .pending_inline_assist_ids_by_editor
.entry(pending_assist.editor)
{
entry.get_mut().retain(|id| *id != assist_id);
if entry.get().is_empty() {
entry.remove();
}
}
if let Some(editor) = pending_assist.editor.upgrade(cx) { if let Some(editor) = pending_assist.editor.upgrade(cx) {
editor.update(cx, |editor, cx| { editor.update(cx, |editor, cx| {
@ -386,7 +426,7 @@ impl AssistantPanel {
} }
} }
fn dismiss_inline_assist(&mut self, assist_id: usize, cx: &mut ViewContext<Self>) { fn hide_inline_assist(&mut self, assist_id: usize, cx: &mut ViewContext<Self>) {
if let Some(pending_assist) = self.pending_inline_assists.get_mut(&assist_id) { if let Some(pending_assist) = self.pending_inline_assists.get_mut(&assist_id) {
if let Some(editor) = pending_assist.editor.upgrade(cx) { if let Some(editor) = pending_assist.editor.upgrade(cx) {
if let Some(block_id) = pending_assist.inline_assistant_block_id.take() { if let Some(block_id) = pending_assist.inline_assistant_block_id.take() {
@ -568,7 +608,7 @@ impl AssistantPanel {
let this = this.clone(); let this = this.clone();
move || { move || {
let _ = this.update(&mut cx, |this, cx| { let _ = this.update(&mut cx, |this, cx| {
this.complete_inline_assist(inline_assist_id, false, cx) this.close_inline_assist(inline_assist_id, false, cx)
}); });
} }
}); });