assistant: Undo workflow step when buffer is discarded (#16465)

This fixes a weird bug:

1. Use `/workflow` in assistant
2. Have it generate a step that modifies a file
3. Either (a) select the step in the assistant and have it auto-insert
newlines (b) select "Transform" to have the step applied
4. Close the modified file in the editor ("Discard")
5. Re-open the file
6. BUG: the changes made by assistant are still there!

The reason for the bug is that the assistant keeps references to buffers
and they're not closed/reloaded when closed/reopened.

To fix the bug we now rollback the applied workflow steps when
discarding a buffer.

(This does *not* yet fix the issue where a workflow step inserts a new
buffer into the project/worktree that does not show up on the file
system yet but in `/file` and hangs around until Zed is closed.)


Release Notes:

- N/A

Co-authored-by: Bennet <bennet@zed.dev>
This commit is contained in:
Thorsten Ball 2024-08-19 18:42:49 +02:00 committed by GitHub
parent 69aae2037d
commit 037cf1393c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 41 additions and 2 deletions

View file

@ -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 {