Skip to content

Commit 752b0cf

Browse files
committed
Use createFileSystemWatcher to more reliably watch for file system changes
1 parent bc7dee6 commit 752b0cf

File tree

1 file changed

+23
-45
lines changed

1 file changed

+23
-45
lines changed

src/integrations/workspace/WorkspaceTracker.ts

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -27,58 +27,36 @@ class WorkspaceTracker {
2727
}
2828

2929
private registerListeners() {
30-
// Listen for file creation
31-
// .bind(this) ensures the callback refers to class instance when using this, not necessary when using arrow function
32-
this.disposables.push(vscode.workspace.onDidCreateFiles(this.onFilesCreated.bind(this)))
33-
34-
// Listen for file deletion
35-
this.disposables.push(vscode.workspace.onDidDeleteFiles(this.onFilesDeleted.bind(this)))
30+
// Create a file system watcher for all files
31+
const watcher = vscode.workspace.createFileSystemWatcher('**')
3632

37-
// Listen for file renaming
38-
this.disposables.push(vscode.workspace.onDidRenameFiles(this.onFilesRenamed.bind(this)))
39-
40-
/*
41-
An event that is emitted when a workspace folder is added or removed.
42-
**Note:** this event will not fire if the first workspace folder is added, removed or changed,
43-
because in that case the currently executing extensions (including the one that listens to this
44-
event) will be terminated and restarted so that the (deprecated) `rootPath` property is updated
45-
to point to the first workspace folder.
46-
*/
47-
// In other words, we don't have to worry about the root workspace folder ([0]) changing since the extension will be restarted and our cwd will be updated to reflect the new workspace folder. (We don't care about non root workspace folders, since cline will only be working within the root folder cwd)
48-
// this.disposables.push(vscode.workspace.onDidChangeWorkspaceFolders(this.onWorkspaceFoldersChanged.bind(this)))
49-
}
50-
51-
private async onFilesCreated(event: vscode.FileCreateEvent) {
52-
await Promise.all(
53-
event.files.map(async (file) => {
54-
await this.addFilePath(file.fsPath)
55-
}),
33+
// Listen for file creation
34+
this.disposables.push(
35+
watcher.onDidCreate(async (uri) => {
36+
await this.addFilePath(uri.fsPath)
37+
this.workspaceDidUpdate()
38+
})
5639
)
57-
this.workspaceDidUpdate()
58-
}
5940

60-
private async onFilesDeleted(event: vscode.FileDeleteEvent) {
61-
let updated = false
62-
await Promise.all(
63-
event.files.map(async (file) => {
64-
if (await this.removeFilePath(file.fsPath)) {
65-
updated = true
41+
// Listen for file deletion
42+
this.disposables.push(
43+
watcher.onDidDelete(async (uri) => {
44+
if (await this.removeFilePath(uri.fsPath)) {
45+
this.workspaceDidUpdate()
6646
}
67-
}),
47+
})
6848
)
69-
if (updated) {
70-
this.workspaceDidUpdate()
71-
}
72-
}
7349

74-
private async onFilesRenamed(event: vscode.FileRenameEvent) {
75-
await Promise.all(
76-
event.files.map(async (file) => {
77-
await this.removeFilePath(file.oldUri.fsPath)
78-
await this.addFilePath(file.newUri.fsPath)
79-
}),
50+
// Listen for file changes (which could include renames)
51+
this.disposables.push(
52+
watcher.onDidChange(async (uri) => {
53+
await this.addFilePath(uri.fsPath)
54+
this.workspaceDidUpdate()
55+
})
8056
)
81-
this.workspaceDidUpdate()
57+
58+
// Add the watcher itself to disposables
59+
this.disposables.push(watcher)
8260
}
8361

8462
private workspaceDidUpdate() {

0 commit comments

Comments
 (0)