diff --git a/crates/assistant/src/assistant_panel.rs b/crates/assistant/src/assistant_panel.rs index 8e6a7d364e..aa1044c7ee 100644 --- a/crates/assistant/src/assistant_panel.rs +++ b/crates/assistant/src/assistant_panel.rs @@ -2479,6 +2479,18 @@ impl ContextEditor { }; let resolved_step = step.read(cx).resolution.clone(); + + if let Some(Ok(resolution)) = resolved_step.as_ref() { + for (buffer, _) in resolution.suggestion_groups.iter() { + let step_range = step_range.clone(); + cx.subscribe(buffer, move |this, _, event, cx| match event { + language::Event::Discarded => this.undo_workflow_step(step_range.clone(), cx), + _ => {} + }) + .detach(); + } + } + if let Some(existing_step) = self.workflow_steps.get_mut(&step_range) { existing_step.resolved_step = resolved_step; } else { diff --git a/crates/editor/src/items.rs b/crates/editor/src/items.rs index 696e4b1fe3..64a94581e3 100644 --- a/crates/editor/src/items.rs +++ b/crates/editor/src/items.rs @@ -680,6 +680,12 @@ impl Item for Editor { self.nav_history = Some(history); } + fn discarded(&self, _project: Model, cx: &mut ViewContext) { + for buffer in self.buffer().clone().read(cx).all_buffers() { + buffer.update(cx, |buffer, cx| buffer.discarded(cx)) + } + } + fn deactivated(&mut self, cx: &mut ViewContext) { let selection = self.selections.newest_anchor(); self.push_to_nav_history(selection.head(), None, cx); diff --git a/crates/language/src/buffer.rs b/crates/language/src/buffer.rs index 3ee2d6ca55..2aac8c3c39 100644 --- a/crates/language/src/buffer.rs +++ b/crates/language/src/buffer.rs @@ -332,6 +332,8 @@ pub enum Event { CapabilityChanged, /// The buffer was explicitly requested to close. Closed, + /// The buffer was discarded when closing. + Discarded, } /// The file associated with a buffer. @@ -827,6 +829,12 @@ impl Buffer { cx.notify(); } + /// This method is called to signal that the buffer has been discarded. + pub fn discarded(&mut self, cx: &mut ModelContext) { + cx.emit(Event::Discarded); + cx.notify(); + } + /// Reloads the contents of the buffer from disk. pub fn reload( &mut self, diff --git a/crates/multi_buffer/src/multi_buffer.rs b/crates/multi_buffer/src/multi_buffer.rs index a734f5c862..eb0bc0546c 100644 --- a/crates/multi_buffer/src/multi_buffer.rs +++ b/crates/multi_buffer/src/multi_buffer.rs @@ -106,6 +106,7 @@ pub enum Event { Saved, FileHandleChanged, Closed, + Discarded, DirtyChanged, DiagnosticsUpdated, } @@ -1691,6 +1692,7 @@ impl MultiBuffer { language::Event::Reparsed => Event::Reparsed(buffer.read(cx).remote_id()), language::Event::DiagnosticsUpdated => Event::DiagnosticsUpdated, language::Event::Closed => Event::Closed, + language::Event::Discarded => Event::Discarded, language::Event::CapabilityChanged => { self.capability = buffer.read(cx).capability(); Event::CapabilityChanged diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index 14535a72fb..b06d8c435f 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -184,6 +184,7 @@ pub trait Item: FocusableView + EventEmitter { fn to_item_events(_event: &Self::Event, _f: impl FnMut(ItemEvent)) {} fn deactivated(&mut self, _: &mut ViewContext) {} + fn discarded(&self, _project: Model, _cx: &mut ViewContext) {} fn workspace_deactivated(&mut self, _: &mut ViewContext) {} fn navigate(&mut self, _: Box, _: &mut ViewContext) -> bool { false @@ -394,6 +395,7 @@ pub trait ItemHandle: 'static + Send { cx: &mut ViewContext, ); fn deactivated(&self, cx: &mut WindowContext); + fn discarded(&self, project: Model, cx: &mut WindowContext); fn workspace_deactivated(&self, cx: &mut WindowContext); fn navigate(&self, data: Box, cx: &mut WindowContext) -> bool; fn item_id(&self) -> EntityId; @@ -735,6 +737,10 @@ impl ItemHandle for View { }); } + fn discarded(&self, project: Model, cx: &mut WindowContext) { + self.update(cx, |this, cx| this.discarded(project, cx)); + } + fn deactivated(&self, cx: &mut WindowContext) { self.update(cx, |this, cx| this.deactivated(cx)); } diff --git a/crates/workspace/src/pane.rs b/crates/workspace/src/pane.rs index 393a47d134..c6d1241a4b 100644 --- a/crates/workspace/src/pane.rs +++ b/crates/workspace/src/pane.rs @@ -1500,8 +1500,13 @@ impl Pane { })?; match answer { Ok(0) => {} - Ok(1) => return Ok(true), // Don't save this file - _ => return Ok(false), // Cancel + Ok(1) => { + // Don't save this file + pane.update(cx, |_, cx| item.discarded(project, cx)) + .log_err(); + return Ok(true); + } + _ => return Ok(false), // Cancel } } else { return Ok(false);