diff --git a/crates/ai/src/assistant.rs b/crates/ai/src/assistant.rs index 3b444082c6..4dad12ad08 100644 --- a/crates/ai/src/assistant.rs +++ b/crates/ai/src/assistant.rs @@ -249,7 +249,7 @@ impl AssistantPanel { } fn new_inline_assist(&mut self, editor: &ViewHandle, cx: &mut ViewContext) { - 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 selection = editor.read(cx).selections.newest_anchor().clone(); 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 assistant = InlineAssistant { - id, + id: inline_assist_id, prompt_editor, confirmed: false, has_focus: false, @@ -313,7 +313,7 @@ impl AssistantPanel { }); self.pending_inline_assists.insert( - id, + inline_assist_id, PendingInlineAssist { kind: assist_kind, editor: editor.downgrade(), @@ -326,12 +326,30 @@ impl AssistantPanel { cx.subscribe(&inline_assistant, Self::handle_inline_assistant_event), cx.subscribe(editor, { 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 editor::Event::SelectionsChanged { local } = event { - if *local && inline_assistant.read(cx).has_focus { - cx.focus(&editor); + match event { + editor::Event::SelectionsChanged { local } => { + 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 .entry(editor.downgrade()) .or_default() - .push(id); + .push(inline_assist_id); self.update_highlights_for_editor(&editor, cx); } diff --git a/crates/editor/src/editor.rs b/crates/editor/src/editor.rs index 6396536b83..fde280f8fe 100644 --- a/crates/editor/src/editor.rs +++ b/crates/editor/src/editor.rs @@ -4975,6 +4975,9 @@ impl Editor { self.unmark_text(cx); self.refresh_copilot_suggestions(true, cx); cx.emit(Event::Edited); + cx.emit(Event::TransactionUndone { + transaction_id: tx_id, + }); } } @@ -8404,6 +8407,9 @@ pub enum Event { local: bool, autoscroll: bool, }, + TransactionUndone { + transaction_id: TransactionId, + }, Closed, }