@@ -6,6 +6,7 @@ import { dictionary } from '@shared/constants/dictionary'
66import { create_safe_path , sanitize_file_name } from '@/utils/path-sanitizer'
77import { FileItem } from '../utils/clipboard-parser'
88import { OriginalFileState } from '@/commands/apply-chat-response-command/types/original-file-state'
9+ import { remove_directory_if_empty } from '../utils/file-operations'
910
1011export const handle_fast_replace = async (
1112 files : FileItem [ ]
@@ -102,46 +103,89 @@ export const handle_fast_replace = async (
102103
103104 try {
104105 if ( file_exists ) {
105- const file_uri = vscode . Uri . file ( safe_path )
106- const document = await vscode . workspace . openTextDocument ( file_uri )
107- const original_content = document . getText ( )
108- original_states . push ( {
109- file_path : file . file_path ,
110- content : original_content ,
111- is_new : false ,
112- workspace_name : file . workspace_name
113- } )
114- const editor = await vscode . window . showTextDocument ( document )
115- let final_content = file . content
116- if (
117- original_content . endsWith ( '\n' ) &&
118- ! final_content . endsWith ( '\n' )
119- ) {
120- final_content += '\n'
121- }
122- await editor . edit ( ( edit ) => {
123- edit . replace (
124- new vscode . Range (
125- document . positionAt ( 0 ) ,
126- document . positionAt ( document . getText ( ) . length )
127- ) ,
128- final_content
129- )
130- } )
106+ if ( file . content == '' ) {
107+ const file_uri = vscode . Uri . file ( safe_path )
108+ const document = await vscode . workspace . openTextDocument ( file_uri )
109+ const original_content = document . getText ( )
110+ original_states . push ( {
111+ file_path : file . file_path ,
112+ content : original_content ,
113+ is_new : false ,
114+ workspace_name : file . workspace_name ,
115+ is_deleted : true
116+ } )
131117
132- await document . save ( )
133- Logger . info ( {
134- function_name : 'handle_fast_replace' ,
135- message : 'Existing file replaced and saved' ,
136- data : safe_path
137- } )
118+ const tabs_to_close : vscode . Tab [ ] = [ ]
119+ for ( const tab_group of vscode . window . tabGroups . all ) {
120+ tabs_to_close . push (
121+ ...tab_group . tabs . filter ( ( tab ) => {
122+ const tab_uri = ( tab . input as any ) ?. uri as
123+ | vscode . Uri
124+ | undefined
125+ return tab_uri && tab_uri . fsPath === safe_path
126+ } )
127+ )
128+ }
129+
130+ if ( tabs_to_close . length > 0 ) {
131+ await vscode . window . tabGroups . close ( tabs_to_close )
132+ }
133+
134+ fs . unlinkSync ( safe_path )
135+ Logger . info ( {
136+ function_name : 'handle_fast_replace' ,
137+ message : 'File deleted due to empty content' ,
138+ data : safe_path
139+ } )
140+ await remove_directory_if_empty ( {
141+ dir_path : path . dirname ( safe_path ) ,
142+ workspace_root
143+ } )
144+ } else {
145+ const file_uri = vscode . Uri . file ( safe_path )
146+ const document = await vscode . workspace . openTextDocument ( file_uri )
147+ const original_content = document . getText ( )
148+ original_states . push ( {
149+ file_path : file . file_path ,
150+ content : original_content ,
151+ is_new : false ,
152+ workspace_name : file . workspace_name
153+ } )
154+ const editor = await vscode . window . showTextDocument ( document )
155+ let final_content = file . content
156+ if (
157+ original_content . endsWith ( '\n' ) &&
158+ ! final_content . endsWith ( '\n' )
159+ ) {
160+ final_content += '\n'
161+ }
162+ await editor . edit ( ( edit ) => {
163+ edit . replace (
164+ new vscode . Range (
165+ document . positionAt ( 0 ) ,
166+ document . positionAt ( document . getText ( ) . length )
167+ ) ,
168+ final_content
169+ )
170+ } )
171+
172+ await document . save ( )
173+ Logger . info ( {
174+ function_name : 'handle_fast_replace' ,
175+ message : 'Existing file replaced and saved' ,
176+ data : safe_path
177+ } )
178+ }
138179 } else {
139180 original_states . push ( {
140181 file_path : file . file_path ,
141182 content : '' ,
142183 is_new : true ,
143184 workspace_name : file . workspace_name
144185 } )
186+ if ( file . content == '' ) {
187+ continue
188+ }
145189 const directory = path . dirname ( safe_path )
146190 if ( ! fs . existsSync ( directory ) ) {
147191 try {
0 commit comments