@@ -23,13 +23,22 @@ import { ResponseHistoryItem } from '@shared/types/response-history-item'
2323let in_progress = false
2424let placeholder_created_at_for_update : number | undefined
2525
26- export const apply_chat_response_command = (
27- params : {
28- context : vscode . ExtensionContext
29- panel_provider : PanelProvider
30- workspace_provider : WorkspaceProvider
31- }
32- ) => {
26+ interface SavedEditorState {
27+ uri : string
28+ view_column : vscode . ViewColumn
29+ is_active : boolean
30+ }
31+
32+ interface SavedTabGroups {
33+ editors : SavedEditorState [ ]
34+ active_editor_uri ?: string
35+ }
36+
37+ export const apply_chat_response_command = ( params : {
38+ context : vscode . ExtensionContext
39+ panel_provider : PanelProvider
40+ workspace_provider : WorkspaceProvider
41+ } ) => {
3342 return vscode . commands . registerCommand (
3443 'codeWebChat.applyChatResponse' ,
3544 async ( args ?: CommandArgs ) => {
@@ -57,12 +66,10 @@ export const apply_chat_response_command = (
5766 }
5867
5968 if ( in_progress && ! response_preview_promise_resolve ) {
60- // Running intelligent update before response preview...
6169 return
6270 }
6371
6472 if ( response_preview_promise_resolve ) {
65- // Previewing response...
6673 const history = params . panel_provider . response_history
6774
6875 const new_item : ResponseHistoryItem = {
@@ -95,7 +102,28 @@ export const apply_chat_response_command = (
95102
96103 in_progress = true
97104 let before_checkpoint : Checkpoint | undefined
105+ let saved_tab_groups : SavedTabGroups | undefined
106+
98107 try {
108+ // Save current tab groups before entering preview
109+ saved_tab_groups = {
110+ editors : [ ] ,
111+ active_editor_uri :
112+ vscode . window . activeTextEditor ?. document . uri . toString ( )
113+ }
114+
115+ for ( const tab_group of vscode . window . tabGroups . all ) {
116+ for ( const tab of tab_group . tabs ) {
117+ if ( tab . input instanceof vscode . TabInputText ) {
118+ saved_tab_groups . editors . push ( {
119+ uri : tab . input . uri . toString ( ) ,
120+ view_column : tab_group . viewColumn ,
121+ is_active : tab . isActive
122+ } )
123+ }
124+ }
125+ }
126+
99127 before_checkpoint = await create_checkpoint (
100128 params . workspace_provider ,
101129 params . context ,
@@ -224,7 +252,9 @@ export const apply_chat_response_command = (
224252 } )
225253 }
226254
227- const history_for_checkpoint = [ ...params . panel_provider . response_history ]
255+ const history_for_checkpoint = [
256+ ...params . panel_provider . response_history
257+ ]
228258 const changes_accepted = await preview_handler ( {
229259 original_states : review_data . original_states ,
230260 chat_response : review_data . chat_response ,
@@ -260,6 +290,11 @@ export const apply_chat_response_command = (
260290
261291 before_checkpoint = undefined
262292 }
293+
294+ // Restore tab groups after preview ends
295+ if ( saved_tab_groups ) {
296+ await restore_tab_groups ( saved_tab_groups )
297+ }
263298 }
264299 } catch ( err : any ) {
265300 vscode . window . showErrorMessage (
@@ -278,3 +313,39 @@ export const apply_chat_response_command = (
278313 }
279314 )
280315}
316+
317+ async function restore_tab_groups ( saved_state : SavedTabGroups ) : Promise < void > {
318+ try {
319+ // Close all current tabs first
320+ await vscode . commands . executeCommand ( 'workbench.action.closeAllEditors' )
321+
322+ // Reopen saved editors in their original view columns
323+ for ( const editor of saved_state . editors ) {
324+ try {
325+ const uri = vscode . Uri . parse ( editor . uri )
326+ await vscode . window . showTextDocument ( uri , {
327+ viewColumn : editor . view_column ,
328+ preview : false ,
329+ preserveFocus : ! editor . is_active
330+ } )
331+ } catch ( error ) {
332+ // File might have been deleted or is no longer accessible
333+ console . error ( `Failed to restore editor for ${ editor . uri } :` , error )
334+ }
335+ }
336+
337+ // Restore active editor focus if it was saved
338+ if ( saved_state . active_editor_uri ) {
339+ try {
340+ const active_uri = vscode . Uri . parse ( saved_state . active_editor_uri )
341+ await vscode . window . showTextDocument ( active_uri , {
342+ preserveFocus : false
343+ } )
344+ } catch ( error ) {
345+ console . error ( 'Failed to restore active editor:' , error )
346+ }
347+ }
348+ } catch ( error ) {
349+ console . error ( 'Failed to restore tab groups:' , error )
350+ }
351+ }
0 commit comments