Close inline assist when the associated transaction is undone

This commit is contained in:
Antonio Scandurra 2023-08-28 14:23:42 +02:00
parent c587cf66ce
commit 8c4d2ccf80
2 changed files with 32 additions and 8 deletions

View file

@ -249,7 +249,7 @@ impl AssistantPanel {
} }
fn new_inline_assist(&mut self, editor: &ViewHandle<Editor>, cx: &mut ViewContext<Self>) { fn new_inline_assist(&mut self, editor: &ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
let id = post_inc(&mut self.next_inline_assist_id); let inline_assist_id = post_inc(&mut self.next_inline_assist_id);
let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx); let snapshot = editor.read(cx).buffer().read(cx).snapshot(cx);
let selection = editor.read(cx).selections.newest_anchor().clone(); let selection = editor.read(cx).selections.newest_anchor().clone();
let range = selection.start.bias_left(&snapshot)..selection.end.bias_right(&snapshot); let range = selection.start.bias_left(&snapshot)..selection.end.bias_right(&snapshot);
@ -272,7 +272,7 @@ impl AssistantPanel {
}); });
let inline_assistant = cx.add_view(|cx| { let inline_assistant = cx.add_view(|cx| {
let assistant = InlineAssistant { let assistant = InlineAssistant {
id, id: inline_assist_id,
prompt_editor, prompt_editor,
confirmed: false, confirmed: false,
has_focus: false, has_focus: false,
@ -313,7 +313,7 @@ impl AssistantPanel {
}); });
self.pending_inline_assists.insert( self.pending_inline_assists.insert(
id, inline_assist_id,
PendingInlineAssist { PendingInlineAssist {
kind: assist_kind, kind: assist_kind,
editor: editor.downgrade(), editor: editor.downgrade(),
@ -326,12 +326,30 @@ impl AssistantPanel {
cx.subscribe(&inline_assistant, Self::handle_inline_assistant_event), cx.subscribe(&inline_assistant, Self::handle_inline_assistant_event),
cx.subscribe(editor, { cx.subscribe(editor, {
let inline_assistant = inline_assistant.downgrade(); let inline_assistant = inline_assistant.downgrade();
move |_, editor, event, cx| { move |this, editor, event, cx| {
if let Some(inline_assistant) = inline_assistant.upgrade(cx) { if let Some(inline_assistant) = inline_assistant.upgrade(cx) {
if let editor::Event::SelectionsChanged { local } = event { match event {
if *local && inline_assistant.read(cx).has_focus { editor::Event::SelectionsChanged { local } => {
cx.focus(&editor); if *local && inline_assistant.read(cx).has_focus {
cx.focus(&editor);
}
} }
editor::Event::TransactionUndone {
transaction_id: tx_id,
} => {
if let Some(pending_assist) =
this.pending_inline_assists.get(&inline_assist_id)
{
if pending_assist.transaction_id == Some(*tx_id) {
this.close_inline_assist(
inline_assist_id,
false,
cx,
);
}
}
}
_ => {}
} }
} }
} }
@ -342,7 +360,7 @@ impl AssistantPanel {
self.pending_inline_assist_ids_by_editor self.pending_inline_assist_ids_by_editor
.entry(editor.downgrade()) .entry(editor.downgrade())
.or_default() .or_default()
.push(id); .push(inline_assist_id);
self.update_highlights_for_editor(&editor, cx); self.update_highlights_for_editor(&editor, cx);
} }

View file

@ -4975,6 +4975,9 @@ impl Editor {
self.unmark_text(cx); self.unmark_text(cx);
self.refresh_copilot_suggestions(true, cx); self.refresh_copilot_suggestions(true, cx);
cx.emit(Event::Edited); cx.emit(Event::Edited);
cx.emit(Event::TransactionUndone {
transaction_id: tx_id,
});
} }
} }
@ -8404,6 +8407,9 @@ pub enum Event {
local: bool, local: bool,
autoscroll: bool, autoscroll: bool,
}, },
TransactionUndone {
transaction_id: TransactionId,
},
Closed, Closed,
} }