Skip to content

Visual Studio Code, rebuilt in Rust

Rust

fix(custom-editor): resolve in the group's column and copy to new windows

rokartur36a958416f81
diff --git a/native/crates/app/src/workbench/auxiliary_windows.rs b/native/crates/app/src/workbench/auxiliary_windows.rs
index 25f8df86f1a..85361d2b3be 100644
--- a/native/crates/app/src/workbench/auxiliary_windows.rs
+++ b/native/crates/app/src/workbench/auxiliary_windows.rs
@@ -209,7 +209,7 @@ impl Workbench {
return;
}
let copies: Vec<_> = match transfer {
- Transfer::CopyEditor => self.editors.copy_active().into_iter().collect(),
+ Transfer::CopyEditor => self.copy_active_editor().into_iter().collect(),
Transfer::CopyGroup => self.copy_group_editors(),
_ => Vec::new(),
};
@@ -221,9 +221,13 @@ impl Workbench {
let active = self.editors.active_index();
let group = self.create_auxiliary_part(bounds, cx);
self.activate_group(group);
- for editor in copies.into_iter().chain(moved) {
+ let copied: Vec<_> = copies.iter().map(|(source, copy)| (*source, copy.model_id)).collect();
+ for editor in copies.into_iter().map(|(_, copy)| copy).chain(moved) {
self.insert_moved_editor(editor);
}
+ for (source, copy) in copied {
+ self.copy_custom_text_editor(source, copy);
+ }
if let (Transfer::CopyGroup | Transfer::MoveGroup, Some(active)) = (transfer, active) {
self.editors.activate(active.min(self.editors.editors.len().saturating_sub(1)));
}
@@ -236,6 +240,7 @@ impl Workbench {
self.auxiliary_windows.windows.last_mut().expect("created above").focus_scrolled = empty;
self.close_group_if_empty(source);
self.editors_changed(cx);
+ self.resolve_custom_editors(cx);
}
/// How far the part's groups draw up (`focus_scrolled`), which ends once the part is more than one empty group.
@@ -252,18 +257,23 @@ impl Workbench {
}
}
- /// Copies of the active group's editors in tab order, its active editor's copy first.
- fn copy_group_editors(&mut self) -> Vec<crate::editor::OpenEditor> {
+ /// Copies of the active group's editors in tab order, each with the model id of the tab it copies.
+ fn copy_group_editors(&mut self) -> Vec<(u32, crate::editor::OpenEditor)> {
let Some(active) = self.editors.active_index() else { return Vec::new() };
let mut copies = Vec::new();
for index in 0..self.editors.editors.len() {
self.editors.activate(index);
- copies.extend(self.editors.copy_active());
+ copies.extend(self.copy_active_editor());
}
self.editors.activate(active);
copies
}
+ fn copy_active_editor(&mut self) -> Option<(u32, crate::editor::OpenEditor)> {
+ let source = self.editors.active_tab()?.model_id;
+ self.editors.copy_active().map(|copy| (source, copy))
+ }
+
/// editorParts.ts `createAuxiliaryEditorPart`: the part and its one group now, its window after this update
/// (the window's first frame reads the workbench).
fn create_auxiliary_part(&mut self, bounds: Bounds<Pixels>, cx: &mut Context<Self>) -> u32 {
diff --git a/native/crates/app/src/workbench/custom_documents/tests.rs b/native/crates/app/src/workbench/custom_documents/tests.rs
index 0e536b7f8ac..ff2ff250ec4 100644
--- a/native/crates/app/src/workbench/custom_documents/tests.rs
+++ b/native/crates/app/src/workbench/custom_documents/tests.rs
@@ -319,6 +319,35 @@ fn each_group_s_custom_text_editor_has_its_own_webview(cx: &mut TestAppContext)
let _ = std::fs::remove_dir_all(dir);
}
+/// mainThreadCustomEditors.ts `resolveWebview` passes `editorGroupToColumn` of the editor's group; a copy into a new
+/// window (`copyEditorToNewWindow`) gets a webview of its own, like a split.
+#[gpui::test]
+fn custom_text_editors_resolve_in_their_group_s_column(cx: &mut TestAppContext) {
+ let dir = scratch();
+ let (workbench, receiver, cx) = open(cx, &dir);
+ let extension = json!({ "id": "port.test", "location": { "scheme": "file", "path": "/ext" } });
+ let args = [extension, json!("test.text"), json!({}), json!({ "supportsMove": false }), json!(false)];
+ workbench.update_in(cx, |workbench, window, cx| {
+ drop(workbench.handle_custom_editors_request("$registerTextEditorProvider", &args, window, cx));
+ drop(workbench.open_with_resolver(&resource(&dir), Some("test.text"), json!({ "pinned": true }), window, cx));
+ });
+ cx.run_until_parked();
+ let mut resolved = Vec::new();
+ let mut resolve = |cx: &mut VisualTestContext| {
+ let message = next(&receiver, "$resolveCustomEditor");
+ resolved.push((message.args[1].clone(), message.args[4].clone()));
+ answer(message, Value::Null, cx);
+ };
+ resolve(cx);
+ run(&workbench, "workbench.action.splitEditorRight", cx);
+ resolve(cx);
+ run(&workbench, "workbench.action.copyEditorToNewWindow", cx);
+ resolve(cx);
+ let expected = [(json!("custom-editor-1"), json!(0)), (json!("custom-editor-2"), json!(1)), (json!("custom-editor-3"), json!(2))];
+ assert_eq!(resolved, expected);
+ let _ = std::fs::remove_dir_all(dir);
+}
+
/// editorResolverService.ts `doHandleConflictingDefaults`: two default editors for a.bin and no association warn once;
/// closing the warning keeps the first and stores the editors for `*.bin`, so the next open does not ask.
#[gpui::test]
diff --git a/native/crates/app/src/workbench/custom_editors.rs b/native/crates/app/src/workbench/custom_editors.rs
index 7044bbf05d9..c27d69e3ecb 100644
--- a/native/crates/app/src/workbench/custom_editors.rs
+++ b/native/crates/app/src/workbench/custom_editors.rs
@@ -793,8 +793,10 @@ impl Workbench {
});
let create = (!provider.text && !document_open).then(|| vec![custom.resource.clone(), json!(panel.view_type), json!(custom.backup_id), Value::Null]);
let init = json!({ "title": panel.title, "contentOptions": {}, "options": provider.options, "active": active == Some(index) });
- // EditorGroupColumn 0, the one group.
- let resolve = vec![custom.resource.clone(), json!(panel.handle), json!(panel.view_type), init, json!(0)];
+ // `editorGroupToColumn`: the index in grid order of the group that shows the editor.
+ let group = self.webview_editor_group(&panel.handle).map(|(group, _)| group);
+ let column = group.and_then(|group| self.groups_in_grid_order().iter().position(|id| *id == group)).unwrap_or(0);
+ let resolve = vec![custom.resource.clone(), json!(panel.handle), json!(panel.view_type), init, json!(column)];
let navigate = (provider.text && provider.supports_navigation).then(|| custom.resource.clone());
resolves.push((panel.handle.clone(), panel.view_type.clone(), create, resolve, navigate));
self.webview_panels.panels[index].custom.as_mut().expect("checked above").resolved = true;