Skip to content

Commit cbe7702

Browse files
committed
Add support for handling deleted files in fast replace operations including tab cleanup and directory management
1 parent 96582cd commit cbe7702

4 files changed

Lines changed: 81 additions & 18 deletions

File tree

‎packages/vscode/src/commands/apply-chat-response-command/handlers/fast-replace-handler.ts‎

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,59 @@ export const handle_fast_replace = async (
9999

100100
if (!safe_path) continue
101101

102+
if (file.is_deleted) {
103+
// If the file was also renamed (moved then deleted), we need to ensure the source is deleted too.
104+
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),
115+
workspace_root
116+
})
117+
}
118+
}
119+
120+
if (fs.existsSync(safe_path)) {
121+
try {
122+
const document = await vscode.workspace.openTextDocument(safe_path)
123+
const original_content = document.getText()
124+
125+
await close_file_tabs(safe_path)
126+
127+
await vscode.workspace.fs.delete(vscode.Uri.file(safe_path))
128+
await remove_directory_if_empty({
129+
dir_path: path.dirname(safe_path),
130+
workspace_root
131+
})
132+
133+
original_states.push({
134+
file_path: file.file_path,
135+
content: original_content,
136+
workspace_name: file.workspace_name,
137+
file_state: 'deleted'
138+
})
139+
Logger.info({
140+
function_name: 'handle_fast_replace',
141+
message: 'File deleted',
142+
data: safe_path
143+
})
144+
} catch (error) {
145+
Logger.error({
146+
function_name: 'handle_fast_replace',
147+
message: 'Failed to delete file',
148+
data: { safe_path, error }
149+
})
150+
}
151+
}
152+
continue
153+
}
154+
102155
let rename_source_path: string | undefined
103156
let rename_source_content: string | undefined
104157

@@ -131,21 +184,7 @@ export const handle_fast_replace = async (
131184
new_content = rename_source_content
132185
}
133186

134-
const tabs_to_close: vscode.Tab[] = []
135-
for (const tab_group of vscode.window.tabGroups.all) {
136-
tabs_to_close.push(
137-
...tab_group.tabs.filter((tab) => {
138-
const tab_uri = (tab.input as any)?.uri as
139-
| vscode.Uri
140-
| undefined
141-
return tab_uri && tab_uri.fsPath === rename_source_path
142-
})
143-
)
144-
}
145-
146-
if (tabs_to_close.length > 0) {
147-
await vscode.window.tabGroups.close(tabs_to_close)
148-
}
187+
await close_file_tabs(rename_source_path)
149188

150189
await vscode.workspace.fs.delete(vscode.Uri.file(rename_source_path))
151190
await remove_directory_if_empty({
@@ -310,3 +349,18 @@ export const handle_fast_replace = async (
310349
return { success: false }
311350
}
312351
}
352+
353+
async function close_file_tabs(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+
}

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/clipboard-parser.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export type FileItem = {
1111
content: string
1212
workspace_name?: string
1313
renamed_from?: string
14+
is_deleted?: boolean
1415
}
1516

1617
export type DiffItem = {

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/parsers/multiple-files-parser/helpers/item-management.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const create_or_update_file_item = (params: {
99
results: (FileItem | TextItem | InlineFileItem)[]
1010
mode?: 'overwrite' | 'append'
1111
renamed_from?: string
12+
is_deleted?: boolean
1213
}): void => {
1314
const {
1415
file_name,
@@ -17,7 +18,8 @@ export const create_or_update_file_item = (params: {
1718
file_ref_map,
1819
results,
1920
mode = 'overwrite',
20-
renamed_from
21+
renamed_from,
22+
is_deleted
2123
} = params
2224

2325
if (!file_name) {
@@ -40,13 +42,18 @@ export const create_or_update_file_item = (params: {
4042
if (renamed_from) {
4143
existing_file.renamed_from = renamed_from
4244
}
45+
46+
if (is_deleted !== undefined) {
47+
existing_file.is_deleted = is_deleted
48+
}
4349
} else {
4450
const new_file: FileItem = {
4551
type: 'file' as const,
4652
file_path: file_name,
4753
content: processed_content,
4854
workspace_name: workspace_name,
49-
renamed_from
55+
renamed_from,
56+
is_deleted
5057
}
5158
file_ref_map.set(file_key, new_file)
5259
results.push(new_file)

‎packages/vscode/src/commands/apply-chat-response-command/utils/clipboard-parser/parsers/multiple-files-parser/multiple-files-parser.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ export const parse_multiple_files = (params: {
279279
content: '',
280280
workspace_name,
281281
file_ref_map,
282-
results
282+
results,
283+
is_deleted: true
283284
})
284285
continue
285286
}

0 commit comments

Comments
 (0)