Skip to content

Visual Studio Code, rebuilt in Rust

Rust

fix(custom-editors): close the text editor a preview reopen replaces

rokartura255a9ab1eb8
diff --git a/native/crates/app/src/workbench/custom_editors.rs b/native/crates/app/src/workbench/custom_editors.rs
index 6e3cc4c22a4..413e18377e8 100644
--- a/native/crates/app/src/workbench/custom_editors.rs
+++ b/native/crates/app/src/workbench/custom_editors.rs
@@ -413,8 +413,8 @@ impl Workbench {
}
}
- /// `group.replaceEditors`: a text model's editor stays and gains or loses its webview; a custom document's or a
- /// saved notebook's editor closes first and the replacement takes its place, pinned (editorGroupView.ts
+ /// `group.replaceEditors`: a text model's editor stays and gains or loses a custom text editor's webview; a custom
+ /// document's, a saved notebook's or a clean text editor reopened in a tab of its own closes first and the replacement takes its place, pinned (editorGroupView.ts
/// `replaceEditors`).
pub(super) fn reopen_with(&mut self, resource: &Value, current: &str, editor_id: &str, preserve_focus: bool, window: &mut Window, cx: &mut Context<Self>) -> Outcome {
// binaryFileEditor.ts `openInternal`: the text editor picked for a binary file opens it as text.
@@ -429,9 +429,14 @@ impl Workbench {
return Outcome::done();
}
let notebook_dirty = self.active_notebook_editor().and_then(|editor| self.host.notebooks.model(&editor.notebook)).is_some_and(|model| model.dirty);
- let replaced = |scheme: &str| scheme == WEBVIEW_PANEL_SCHEME || (scheme == crate::notebook::NOTEBOOK_EDITOR_SCHEME && !notebook_dirty);
+ let own_tab = media_kind(editor_id).is_some() || self.is_notebook_type(editor_id) || self.webview_panels.custom_editors.providers.get(editor_id).is_some_and(|provider| !provider.text);
+ let replaced = |editor: &OpenEditor| match editor.document().scheme.as_str() {
+ WEBVIEW_PANEL_SCHEME => true,
+ crate::notebook::NOTEBOOK_EDITOR_SCHEME => !notebook_dirty,
+ _ => own_tab && !editor.is_dirty(),
+ };
let group = self.editors.id;
- let replaced_at = self.editors.active_index().filter(|&index| replaced(&self.editors.editors[index].document().scheme));
+ let replaced_at = self.editors.active_index().filter(|&index| replaced(&self.editors.editors[index]));
if let Some(index) = replaced_at {
self.editors.close(index);
self.editors_changed(cx);
diff --git a/native/crates/app/src/workbench/media_preview.rs b/native/crates/app/src/workbench/media_preview.rs
index 608f02f294f..f7d2f543be9 100644
--- a/native/crates/app/src/workbench/media_preview.rs
+++ b/native/crates/app/src/workbench/media_preview.rs
@@ -631,6 +631,25 @@ mod tests {
assert_eq!(tabs, [("file".to_owned(), image), ("file".to_owned(), text)]);
}
+ /// editorGroupView.ts `replaceEditors` closes the text editor an image preview replaces, as Reopen as Preview does.
+ #[gpui::test]
+ fn reopening_as_text_and_back_keeps_one_tab(cx: &mut gpui::TestAppContext) {
+ let app_root = Path::new(env!("VSCODE_SRC")).to_path_buf();
+ let themes = exthost::scan::scan_extension(&app_root.join("extensions/theme-defaults"), true).unwrap();
+ let theme = crate::theme::load_theme(&[themes], "Dark Modern").unwrap();
+ let (workbench, cx) = cx.add_window_view(|_, cx| Workbench::new(theme, app_root.clone(), None, crate::languages::Languages::new(&[]), crate::default_settings(), crate::host_sync::HostSync::new(std::sync::mpsc::channel().0), cx));
+ let image = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../../port/fixtures/webviews/image.png");
+ workbench.update_in(cx, |workbench, window, cx| workbench.open_file(image.clone(), true, window, cx));
+ cx.run_until_parked();
+ let schemes = |cx: &mut gpui::VisualTestContext| workbench.read_with(cx, |workbench, _| workbench.editors.editors.iter().map(|editor| editor.document().scheme.clone()).collect::<Vec<_>>());
+ workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("imagePreview.reopenAsText", Vec::new(), window, cx)));
+ cx.run_until_parked();
+ assert_eq!(schemes(cx), ["file"]);
+ workbench.update_in(cx, |workbench, window, cx| drop(workbench.execute_command("imagePreview.reopenAsPreview", Vec::new(), window, cx)));
+ cx.run_until_parked();
+ assert_eq!(schemes(cx), [WEBVIEW_PANEL_SCHEME]);
+ }
+
/// mediaPreview.contribution.ts registers without `singlePerResource`: opening an image previewed in another
/// group gives the active group a preview of its own.
#[gpui::test]