Skip to content

Commit c4cd897

Browse files
committed
Preserve and restore open editors and their view columns when duplicating a workspace
1 parent 63b6534 commit c4cd897

3 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎packages/vscode/src/commands/duplicate-workspace-command.ts‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,27 @@ export function duplicate_workspace_command(
2424
vscode.workspace.workspaceFolders?.map((folder) => folder.uri.fsPath) ??
2525
[]
2626

27+
const open_editors: { path: string; view_column?: number }[] = []
28+
for (const tab_group of vscode.window.tabGroups.all) {
29+
for (const tab of tab_group.tabs) {
30+
if (
31+
tab.input instanceof vscode.TabInputText &&
32+
tab.input.uri.scheme == 'file'
33+
) {
34+
open_editors.push({
35+
path: tab.input.uri.fsPath,
36+
view_column: tab_group.viewColumn
37+
})
38+
}
39+
}
40+
}
41+
2742
const context_to_save: DuplicateWorkspaceContext = {
2843
checked_files,
2944
checked_websites,
3045
workspace_root_folders,
31-
timestamp: Date.now()
46+
timestamp: Date.now(),
47+
open_editors
3248
}
3349

3450
const api_edit_format = context.workspaceState.get(

‎packages/vscode/src/constants/state-keys.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ export type DuplicateWorkspaceContext = {
7777
checked_websites: string[]
7878
timestamp: number
7979
workspace_root_folders: string[]
80+
open_editors?: { path: string; view_column?: number }[]
8081
}
8182

8283
export type HistoryEntry = {

‎packages/vscode/src/context/context-initialization.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ const restore_duplicated_workspace_context = async (
6161
CONTEXT_CHECKED_URLS_STATE_KEY,
6262
duplicated_context.checked_websites
6363
)
64+
if (duplicated_context.open_editors) {
65+
for (const editor_info of duplicated_context.open_editors) {
66+
try {
67+
const uri = vscode.Uri.file(editor_info.path)
68+
const document = await vscode.workspace.openTextDocument(uri)
69+
await vscode.window.showTextDocument(document, {
70+
viewColumn: editor_info.view_column,
71+
preview: false
72+
})
73+
} catch (error) {
74+
console.error(
75+
`Failed to restore open editor for ${editor_info.path}:`,
76+
error
77+
)
78+
}
79+
}
80+
}
6481
}
6582
}
6683

0 commit comments

Comments
 (0)