@@ -6,7 +6,12 @@ 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'
9+ import {
10+ remove_directory_if_empty ,
11+ close_file_tabs ,
12+ read_rename_source_file ,
13+ cleanup_rename_source
14+ } from '../utils/file-operations'
1015
1116export const handle_fast_replace = async (
1217 files : FileItem [ ]
@@ -102,16 +107,13 @@ export const handle_fast_replace = async (
102107 if ( file . is_deleted ) {
103108 // If the file was also renamed (moved then deleted), we need to ensure the source is deleted too.
104109 if ( file . renamed_from ) {
105- const sanitized_rename_path = sanitize_file_name ( file . renamed_from )
106- const safe_rename_path = create_safe_path (
107- workspace_root ,
108- sanitized_rename_path
109- )
110- if ( safe_rename_path && fs . existsSync ( safe_rename_path ) ) {
111- await close_file_tabs ( safe_rename_path )
112- await vscode . workspace . fs . delete ( vscode . Uri . file ( safe_rename_path ) )
113- await remove_directory_if_empty ( {
114- dir_path : path . dirname ( safe_rename_path ) ,
110+ const source_info = await read_rename_source_file ( {
111+ renamed_from : file . renamed_from ,
112+ workspace_root
113+ } )
114+ if ( source_info ) {
115+ await cleanup_rename_source ( {
116+ source_path : source_info . path ,
115117 workspace_root
116118 } )
117119 }
@@ -156,24 +158,13 @@ export const handle_fast_replace = async (
156158 let rename_source_content : string | undefined
157159
158160 if ( file . renamed_from ) {
159- const sanitized_rename_path = sanitize_file_name ( file . renamed_from )
160- const safe_rename_path = create_safe_path (
161- workspace_root ,
162- sanitized_rename_path
163- )
164- if ( safe_rename_path && fs . existsSync ( safe_rename_path ) ) {
165- rename_source_path = safe_rename_path
166- try {
167- const document =
168- await vscode . workspace . openTextDocument ( safe_rename_path )
169- rename_source_content = document . getText ( )
170- } catch ( e ) {
171- Logger . warn ( {
172- function_name : 'handle_fast_replace' ,
173- message : 'Failed to read rename source file' ,
174- data : safe_rename_path
175- } )
176- }
161+ const source_info = await read_rename_source_file ( {
162+ renamed_from : file . renamed_from ,
163+ workspace_root
164+ } )
165+ if ( source_info ) {
166+ rename_source_path = source_info . path
167+ rename_source_content = source_info . content
177168 }
178169 }
179170
@@ -184,11 +175,8 @@ export const handle_fast_replace = async (
184175 new_content = rename_source_content
185176 }
186177
187- await close_file_tabs ( rename_source_path )
188-
189- await vscode . workspace . fs . delete ( vscode . Uri . file ( rename_source_path ) )
190- await remove_directory_if_empty ( {
191- dir_path : path . dirname ( rename_source_path ) ,
178+ await cleanup_rename_source ( {
179+ source_path : rename_source_path ,
192180 workspace_root
193181 } )
194182
@@ -349,18 +337,3 @@ export const handle_fast_replace = async (
349337 return { success : false }
350338 }
351339}
352-
353- const close_file_tabs = async ( file_path : string ) => {
354- const tabs_to_close : vscode . Tab [ ] = [ ]
355- for ( const tab_group of vscode . window . tabGroups . all ) {
356- tabs_to_close . push (
357- ...tab_group . tabs . filter ( ( tab ) => {
358- const tab_uri = ( tab . input as any ) ?. uri as vscode . Uri | undefined
359- return tab_uri && tab_uri . fsPath === file_path
360- } )
361- )
362- }
363- if ( tabs_to_close . length > 0 ) {
364- await vscode . window . tabGroups . close ( tabs_to_close )
365- }
366- }
0 commit comments