Skip to content

Commit cf073e4

Browse files
committed
Optimize tab restoration by closing only excess tabs instead of all editors when possible
1 parent 6133ad2 commit cf073e4

1 file changed

Lines changed: 33 additions & 12 deletions

File tree

‎apps/editor/src/commands/apply-chat-response-command/apply-chat-response-command.ts‎

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -459,18 +459,39 @@ const restore_tab_groups = async (
459459
return
460460
}
461461

462-
await vscode.commands.executeCommand('workbench.action.closeAllEditors')
463-
464-
for (const editor of saved_state.editors) {
465-
try {
466-
const uri = vscode.Uri.parse(editor.uri)
467-
await vscode.window.showTextDocument(uri, {
468-
viewColumn: editor.view_column,
469-
preview: false,
470-
preserveFocus: !editor.is_active
471-
})
472-
} catch (error) {
473-
console.error(`Failed to restore editor for ${editor.uri}:`, error)
462+
if (current_editors.length > saved_state.editors.length) {
463+
const tabs_to_close: vscode.Tab[] = []
464+
for (const tab_group of vscode.window.tabGroups.all) {
465+
for (const tab of tab_group.tabs) {
466+
if (tab.input instanceof vscode.TabInputText) {
467+
const uri = tab.input.uri.toString()
468+
const is_saved = saved_state.editors.some(
469+
(saved) =>
470+
saved.uri == uri && saved.view_column == tab_group.viewColumn
471+
)
472+
if (!is_saved) {
473+
tabs_to_close.push(tab)
474+
}
475+
}
476+
}
477+
}
478+
if (tabs_to_close.length > 0) {
479+
await vscode.window.tabGroups.close(tabs_to_close)
480+
}
481+
} else {
482+
await vscode.commands.executeCommand('workbench.action.closeAllEditors')
483+
484+
for (const editor of saved_state.editors) {
485+
try {
486+
const uri = vscode.Uri.parse(editor.uri)
487+
await vscode.window.showTextDocument(uri, {
488+
viewColumn: editor.view_column,
489+
preview: false,
490+
preserveFocus: !editor.is_active
491+
})
492+
} catch (error) {
493+
console.error(`Failed to restore editor for ${editor.uri}:`, error)
494+
}
474495
}
475496
}
476497

0 commit comments

Comments
 (0)