@@ -157,105 +157,114 @@ export const apply_chat_response_command = (
157157 } )
158158
159159 if ( changes_accepted ) {
160- const workspace_map = new Map < string , string > ( )
161- vscode . workspace . workspaceFolders ! . forEach ( ( folder ) => {
162- workspace_map . set ( folder . name , folder . uri . fsPath )
163- } )
164- const default_workspace =
165- vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath
160+ panel_provider . send_message ( { command : 'SHOW_APPLYING_CHANGES' } )
161+ try {
162+ const workspace_map = new Map < string , string > ( )
163+ vscode . workspace . workspaceFolders ! . forEach ( ( folder ) => {
164+ workspace_map . set ( folder . name , folder . uri . fsPath )
165+ } )
166+ const default_workspace =
167+ vscode . workspace . workspaceFolders ! [ 0 ] . uri . fsPath
166168
167- const accepted_states : {
168- file_path : string
169- workspace_name ?: string
170- content : string
171- is_deleted : boolean
172- } [ ] = [ ]
169+ const accepted_states : {
170+ file_path : string
171+ workspace_name ?: string
172+ content : string
173+ is_deleted : boolean
174+ } [ ] = [ ]
173175
174- for ( const state of review_data . original_states ) {
175- let workspace_root = default_workspace
176- if (
177- state . workspace_name &&
178- workspace_map . has ( state . workspace_name )
179- ) {
180- workspace_root = workspace_map . get ( state . workspace_name ) !
181- }
176+ for ( const state of review_data . original_states ) {
177+ let workspace_root = default_workspace
178+ if (
179+ state . workspace_name &&
180+ workspace_map . has ( state . workspace_name )
181+ ) {
182+ workspace_root = workspace_map . get ( state . workspace_name ) !
183+ }
182184
183- const sanitized_file_path = create_safe_path (
184- workspace_root ,
185- state . file_path
186- )
187- if ( ! sanitized_file_path ) {
188- continue
185+ const sanitized_file_path = create_safe_path (
186+ workspace_root ,
187+ state . file_path
188+ )
189+ if ( ! sanitized_file_path ) {
190+ continue
191+ }
192+
193+ const exists = fs . existsSync ( sanitized_file_path )
194+ const content = exists
195+ ? fs . readFileSync ( sanitized_file_path , 'utf8' )
196+ : ''
197+
198+ accepted_states . push ( {
199+ file_path : state . file_path ,
200+ workspace_name : state . workspace_name ,
201+ content : content ,
202+ is_deleted : ! exists
203+ } )
189204 }
190205
191- const exists = fs . existsSync ( sanitized_file_path )
192- const content = exists
193- ? fs . readFileSync ( sanitized_file_path , 'utf8' )
194- : ''
206+ await undo_files ( { original_states : review_data . original_states } )
195207
196- accepted_states . push ( {
197- file_path : state . file_path ,
198- workspace_name : state . workspace_name ,
199- content : content ,
200- is_deleted : ! exists
201- } )
202- }
208+ await create_checkpoint (
209+ workspace_provider ,
210+ context ,
211+ 'Before changes approved' ,
212+ args ?. raw_instructions
213+ )
203214
204- await undo_files ( { original_states : review_data . original_states } )
215+ for ( const accepted_state of accepted_states ) {
216+ let workspace_root = default_workspace
217+ if (
218+ accepted_state . workspace_name &&
219+ workspace_map . has ( accepted_state . workspace_name )
220+ ) {
221+ workspace_root = workspace_map . get (
222+ accepted_state . workspace_name
223+ ) !
224+ }
225+ const sanitized_file_path = create_safe_path (
226+ workspace_root ,
227+ accepted_state . file_path
228+ )
229+ if ( ! sanitized_file_path ) continue
205230
206- await create_checkpoint (
207- workspace_provider ,
208- context ,
209- 'Before changes approved' ,
210- args ?. raw_instructions
211- )
231+ if ( accepted_state . is_deleted ) {
232+ if ( fs . existsSync ( sanitized_file_path ) ) {
233+ const tabs_to_close : vscode . Tab [ ] = [ ]
234+ for ( const tab_group of vscode . window . tabGroups . all ) {
235+ tabs_to_close . push (
236+ ...tab_group . tabs . filter ( ( tab ) => {
237+ const tab_uri = ( tab . input as any ) ?. uri as
238+ | vscode . Uri
239+ | undefined
240+ return tab_uri && tab_uri . fsPath == sanitized_file_path
241+ } )
242+ )
243+ }
212244
213- for ( const accepted_state of accepted_states ) {
214- let workspace_root = default_workspace
215- if (
216- accepted_state . workspace_name &&
217- workspace_map . has ( accepted_state . workspace_name )
218- ) {
219- workspace_root = workspace_map . get ( accepted_state . workspace_name ) !
220- }
221- const sanitized_file_path = create_safe_path (
222- workspace_root ,
223- accepted_state . file_path
224- )
225- if ( ! sanitized_file_path ) continue
245+ if ( tabs_to_close . length > 0 ) {
246+ await vscode . window . tabGroups . close ( tabs_to_close )
247+ }
226248
227- if ( accepted_state . is_deleted ) {
228- if ( fs . existsSync ( sanitized_file_path ) ) {
229- const tabs_to_close : vscode . Tab [ ] = [ ]
230- for ( const tab_group of vscode . window . tabGroups . all ) {
231- tabs_to_close . push (
232- ...tab_group . tabs . filter ( ( tab ) => {
233- const tab_uri = ( tab . input as any ) ?. uri as
234- | vscode . Uri
235- | undefined
236- return tab_uri && tab_uri . fsPath == sanitized_file_path
237- } )
249+ await vscode . workspace . fs . delete (
250+ vscode . Uri . file ( sanitized_file_path )
238251 )
239252 }
240-
241- if ( tabs_to_close . length > 0 ) {
242- await vscode . window . tabGroups . close ( tabs_to_close )
253+ } else {
254+ const dir = path . dirname ( sanitized_file_path )
255+ if ( ! fs . existsSync ( dir ) ) {
256+ await vscode . workspace . fs . createDirectory (
257+ vscode . Uri . file ( dir )
258+ )
243259 }
244-
245- await vscode . workspace . fs . delete (
246- vscode . Uri . file ( sanitized_file_path )
260+ await vscode . workspace . fs . writeFile (
261+ vscode . Uri . file ( sanitized_file_path ) ,
262+ Buffer . from ( accepted_state . content , 'utf8' )
247263 )
248264 }
249- } else {
250- const dir = path . dirname ( sanitized_file_path )
251- if ( ! fs . existsSync ( dir ) ) {
252- await vscode . workspace . fs . createDirectory ( vscode . Uri . file ( dir ) )
253- }
254- await vscode . workspace . fs . writeFile (
255- vscode . Uri . file ( sanitized_file_path ) ,
256- Buffer . from ( accepted_state . content , 'utf8' )
257- )
258265 }
266+ } finally {
267+ panel_provider . send_message ( { command : 'HIDE_APPLYING_CHANGES' } )
259268 }
260269 }
261270 }
0 commit comments