Open workflow step editors as preview tabs (#15928)

This PR opens workflow step editors as preview tabs and closes them upon
exiting the step if they are still in preview mode and they weren't
already open before entering the step.

Making this work was tricky, because we often edit the buffer as part of
displaying the workflow step suggestions to create empty lines where we
can generate. We undo these edits if the transformation is not applied,
but they were causing the preview to be dismissed.

After trying a few approaches, I decided to give workspace `Item`s a
`preserve_preview` method that defaults to false. When the workspace
sees an edit event for the item, it checks if the item wants to preserve
its preview. For buffers, after editing, you can call `refresh_preview`,
which sets a preview version to the current version of the buffer. Any
edits after this version will cause preview to not be preserved.

One final issue is with async auto-indent. To ensure these async edits
don't dismiss the preview, I automatically refresh the preview version
if preview was preserved prior to performing the auto-indent. The
assumption is that these are edits created by other edits, and if we
didn't want to dismiss the preview with the originating edits, then the
auto-indent edits shouldn't dismiss it either.

Release Notes:

- N/A

---------

Co-authored-by: Jason <jason@zed.dev>
This commit is contained in:
Nathan Sobo 2024-08-07 19:33:58 -06:00 committed by GitHub
parent a5961c8d45
commit da8d1306af
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 497 additions and 324 deletions

View file

@ -97,6 +97,7 @@ pub struct Buffer {
/// The version vector when this buffer was last loaded from
/// or saved to disk.
saved_version: clock::Global,
preview_version: clock::Global,
transaction_depth: usize,
was_dirty_before_starting_transaction: Option<bool>,
reload_task: Option<Task<Result<()>>>,
@ -703,6 +704,7 @@ impl Buffer {
Self {
saved_mtime,
saved_version: buffer.version(),
preview_version: buffer.version(),
reload_task: None,
transaction_depth: 0,
was_dirty_before_starting_transaction: None,
@ -1351,7 +1353,11 @@ impl Buffer {
})
.collect();
let preserve_preview = self.preserve_preview();
self.edit(edits, None, cx);
if preserve_preview {
self.refresh_preview();
}
}
/// Create a minimal edit that will cause the given row to be indented
@ -2195,6 +2201,18 @@ impl Buffer {
pub fn completion_triggers(&self) -> &[String] {
&self.completion_triggers
}
/// Call this directly after performing edits to prevent the preview tab
/// from being dismissed by those edits. It causes `should_dismiss_preview`
/// to return false until there are additional edits.
pub fn refresh_preview(&mut self) {
self.preview_version = self.version.clone();
}
/// Whether we should preserve the preview status of a tab containing this buffer.
pub fn preserve_preview(&self) -> bool {
!self.has_edits_since(&self.preview_version)
}
}
#[doc(hidden)]

View file

@ -1822,6 +1822,63 @@ fn test_autoindent_query_with_outdent_captures(cx: &mut AppContext) {
});
}
#[gpui::test]
async fn test_async_autoindents_preserve_preview(cx: &mut TestAppContext) {
cx.update(|cx| init_settings(cx, |_| {}));
// First we insert some newlines to request an auto-indent (asynchronously).
// Then we request that a preview tab be preserved for the new version, even though it's edited.
let buffer = cx.new_model(|cx| {
let text = "fn a() {}";
let mut buffer = Buffer::local(text, cx).with_language(Arc::new(rust_lang()), cx);
// This causes autoindent to be async.
buffer.set_sync_parse_timeout(Duration::ZERO);
buffer.edit([(8..8, "\n\n")], Some(AutoindentMode::EachLine), cx);
buffer.refresh_preview();
// Synchronously, we haven't auto-indented and we're still preserving the preview.
assert_eq!(buffer.text(), "fn a() {\n\n}");
assert!(buffer.preserve_preview());
buffer
});
// Now let the autoindent finish
cx.executor().run_until_parked();
// The auto-indent applied, but didn't dismiss our preview
buffer.update(cx, |buffer, cx| {
assert_eq!(buffer.text(), "fn a() {\n \n}");
assert!(buffer.preserve_preview());
// Edit inserting another line. It will autoindent async.
// Then refresh the preview version.
buffer.edit(
[(Point::new(1, 4)..Point::new(1, 4), "\n")],
Some(AutoindentMode::EachLine),
cx,
);
buffer.refresh_preview();
assert_eq!(buffer.text(), "fn a() {\n \n\n}");
assert!(buffer.preserve_preview());
// Then perform another edit, this time without refreshing the preview version.
buffer.edit([(Point::new(1, 4)..Point::new(1, 4), "x")], None, cx);
// This causes the preview to not be preserved.
assert!(!buffer.preserve_preview());
});
// Let the async autoindent from the first edit finish.
cx.executor().run_until_parked();
// The autoindent applies, but it shouldn't restore the preview status because we had an edit in the meantime.
buffer.update(cx, |buffer, _| {
assert_eq!(buffer.text(), "fn a() {\n x\n \n}");
assert!(!buffer.preserve_preview());
});
}
#[gpui::test]
fn test_insert_empty_line(cx: &mut AppContext) {
init_settings(cx, |_| {});