Skip to content

Commit 30d8ff9

Browse files
committed
Add recursive directory handling to workspace delete listeners
- a file deleted during the preview (additonally, bu the user) is not correctly restored upon cancellation unless placed in root └── apps └── editor └── src └── commands └── apply-response-command ├── apply-response-command.ts ├── response-processor.ts └── utils ├── file-checks.ts ├── file-operations.ts ├── preview │ ├── diff-utils.ts │ ├── file-preparer.ts │ ├── index.ts │ ├── preview.ts │ ├── temp-file-manager.ts │ ├── types.ts │ ├── virtual-document-provider.ts │ ├── vscode-ui.ts │ └── workspace-listener.ts ├── preview-handler.ts └── state-manager.ts
1 parent 5173014 commit 30d8ff9

1 file changed

Lines changed: 44 additions & 5 deletions

File tree

‎apps/editor/src/commands/apply-response-command/utils/preview/workspace-listener.ts‎

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,28 @@ export const setup_workspace_listeners = (params: {
8585
const file_will_delete_listener = vscode.workspace.onWillDeleteFiles(
8686
(event) => {
8787
const promise = (async () => {
88-
for (const uri of event.files) {
89-
if (uri.scheme != 'file') continue
88+
const process_uri = async (uri: vscode.Uri) => {
9089
try {
91-
const content = (await vscode.workspace.fs.readFile(uri)).toString()
92-
deleted_files_content_cache.set(uri.fsPath, content)
90+
const stat = await vscode.workspace.fs.stat(uri)
91+
if (stat.type === vscode.FileType.Directory) {
92+
const entries = await vscode.workspace.fs.readDirectory(uri)
93+
for (const [name] of entries) {
94+
await process_uri(vscode.Uri.joinPath(uri, name))
95+
}
96+
} else {
97+
const contentBytes = await vscode.workspace.fs.readFile(uri)
98+
const content = Buffer.from(contentBytes).toString('utf8')
99+
deleted_files_content_cache.set(uri.fsPath, content)
100+
}
93101
} catch (e) {
94-
// Ignore, e.g. for directories
102+
// Ignore
95103
}
96104
}
105+
106+
for (const uri of event.files) {
107+
if (uri.scheme != 'file') continue
108+
await process_uri(uri)
109+
}
97110
})()
98111
event.waitUntil(promise)
99112
}
@@ -227,9 +240,35 @@ export const setup_workspace_listeners = (params: {
227240
})
228241

229242
const file_delete_listener = vscode.workspace.onDidDeleteFiles((event) => {
243+
const uris_to_process: vscode.Uri[] = []
244+
230245
for (const uri of event.files) {
231246
if (uri.scheme != 'file') continue
232247

248+
let found_in_cache = false
249+
if (deleted_files_content_cache.has(uri.fsPath)) {
250+
uris_to_process.push(uri)
251+
found_in_cache = true
252+
}
253+
254+
const prefix = uri.fsPath + path.sep
255+
for (const cached_path of deleted_files_content_cache.keys()) {
256+
if (cached_path.startsWith(prefix)) {
257+
uris_to_process.push(vscode.Uri.file(cached_path))
258+
found_in_cache = true
259+
}
260+
}
261+
262+
if (!found_in_cache) {
263+
uris_to_process.push(uri)
264+
}
265+
}
266+
267+
const unique_uris = Array.from(
268+
new Set(uris_to_process.map((u) => u.fsPath))
269+
).map((p) => vscode.Uri.file(p))
270+
271+
for (const uri of unique_uris) {
233272
const deleted_file_path = uri.fsPath
234273
const deleted_file_in_preview = params.prepared_files.find(
235274
(pf) => pf.sanitized_path == deleted_file_path

0 commit comments

Comments
 (0)