Use createFileSystemWatcher to more reliably watch for file system changes

This commit is contained in:
Matt Rubens
2024-12-23 21:04:45 -08:00
parent bc7dee6097
commit 752b0cf352

View File

@@ -27,58 +27,36 @@ class WorkspaceTracker {
} }
private registerListeners() { private registerListeners() {
// Create a file system watcher for all files
const watcher = vscode.workspace.createFileSystemWatcher('**')
// Listen for file creation // Listen for file creation
// .bind(this) ensures the callback refers to class instance when using this, not necessary when using arrow function this.disposables.push(
this.disposables.push(vscode.workspace.onDidCreateFiles(this.onFilesCreated.bind(this))) watcher.onDidCreate(async (uri) => {
await this.addFilePath(uri.fsPath)
this.workspaceDidUpdate()
})
)
// Listen for file deletion // Listen for file deletion
this.disposables.push(vscode.workspace.onDidDeleteFiles(this.onFilesDeleted.bind(this))) this.disposables.push(
watcher.onDidDelete(async (uri) => {
// Listen for file renaming if (await this.removeFilePath(uri.fsPath)) {
this.disposables.push(vscode.workspace.onDidRenameFiles(this.onFilesRenamed.bind(this))) this.workspaceDidUpdate()
/*
An event that is emitted when a workspace folder is added or removed.
**Note:** this event will not fire if the first workspace folder is added, removed or changed,
because in that case the currently executing extensions (including the one that listens to this
event) will be terminated and restarted so that the (deprecated) `rootPath` property is updated
to point to the first workspace folder.
*/
// 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)
// this.disposables.push(vscode.workspace.onDidChangeWorkspaceFolders(this.onWorkspaceFoldersChanged.bind(this)))
}
private async onFilesCreated(event: vscode.FileCreateEvent) {
await Promise.all(
event.files.map(async (file) => {
await this.addFilePath(file.fsPath)
}),
)
this.workspaceDidUpdate()
}
private async onFilesDeleted(event: vscode.FileDeleteEvent) {
let updated = false
await Promise.all(
event.files.map(async (file) => {
if (await this.removeFilePath(file.fsPath)) {
updated = true
} }
}), })
) )
if (updated) {
this.workspaceDidUpdate()
}
}
private async onFilesRenamed(event: vscode.FileRenameEvent) { // Listen for file changes (which could include renames)
await Promise.all( this.disposables.push(
event.files.map(async (file) => { watcher.onDidChange(async (uri) => {
await this.removeFilePath(file.oldUri.fsPath) await this.addFilePath(uri.fsPath)
await this.addFilePath(file.newUri.fsPath) this.workspaceDidUpdate()
}), })
) )
this.workspaceDidUpdate()
// Add the watcher itself to disposables
this.disposables.push(watcher)
} }
private workspaceDidUpdate() { private workspaceDidUpdate() {