|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import * as path from "path"; |
| 3 | + |
| 4 | +export class TlogFileWatcher { |
| 5 | + private fileWatcher: vscode.FileSystemWatcher | null = null; |
| 6 | + private saveListener: vscode.Disposable | null = null; |
| 7 | + private refreshTimeout: NodeJS.Timeout | null = null; |
| 8 | + private onRefreshCallback: () => void; |
| 9 | + |
| 10 | + constructor(onRefresh: () => void) { |
| 11 | + this.onRefreshCallback = onRefresh; |
| 12 | + } |
| 13 | + |
| 14 | + start(workspacePath: string): void { |
| 15 | + this.dispose(); |
| 16 | + |
| 17 | + this.saveListener = vscode.workspace.onDidSaveTextDocument((document) => { |
| 18 | + if (this.shouldProcessFile(document.fileName)) { |
| 19 | + this.debouncedRefresh(); |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + this.fileWatcher = vscode.workspace.createFileSystemWatcher( |
| 24 | + new vscode.RelativePattern( |
| 25 | + workspacePath, |
| 26 | + "**/*.{js,ts,jsx,tsx,vue,py,java,c,cpp,cs,php,rb,go,rs,swift,kt,dart}" |
| 27 | + ), |
| 28 | + false, // onCreate |
| 29 | + true, // onChange |
| 30 | + false // onDelete |
| 31 | + ); |
| 32 | + |
| 33 | + this.fileWatcher.onDidCreate((uri) => { |
| 34 | + if (this.shouldProcessFile(uri.fsPath)) { |
| 35 | + this.debouncedRefresh(); |
| 36 | + } |
| 37 | + }); |
| 38 | + |
| 39 | + this.fileWatcher.onDidDelete((uri) => { |
| 40 | + if (this.shouldProcessFile(uri.fsPath)) { |
| 41 | + this.debouncedRefresh(); |
| 42 | + } |
| 43 | + }); |
| 44 | + } |
| 45 | + |
| 46 | + private shouldProcessFile(filePath: string): boolean { |
| 47 | + const excludePatterns = [ |
| 48 | + "node_modules", |
| 49 | + ".git", |
| 50 | + ".vscode", |
| 51 | + "dist", |
| 52 | + "build", |
| 53 | + "out", |
| 54 | + "coverage", |
| 55 | + ".next", |
| 56 | + ".nuxt", |
| 57 | + ".cache", |
| 58 | + "tmp", |
| 59 | + "temp", |
| 60 | + ]; |
| 61 | + |
| 62 | + return !excludePatterns.some( |
| 63 | + (pattern) => |
| 64 | + filePath.includes(path.sep + pattern + path.sep) || |
| 65 | + filePath.endsWith(path.sep + pattern) |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + private debouncedRefresh(): void { |
| 70 | + if (this.refreshTimeout) { |
| 71 | + clearTimeout(this.refreshTimeout); |
| 72 | + } |
| 73 | + |
| 74 | + this.refreshTimeout = setTimeout(() => { |
| 75 | + this.onRefreshCallback(); |
| 76 | + this.refreshTimeout = null; |
| 77 | + }, 300); |
| 78 | + } |
| 79 | + |
| 80 | + dispose(): void { |
| 81 | + if (this.fileWatcher) { |
| 82 | + this.fileWatcher.dispose(); |
| 83 | + this.fileWatcher = null; |
| 84 | + } |
| 85 | + |
| 86 | + if (this.saveListener) { |
| 87 | + this.saveListener.dispose(); |
| 88 | + this.saveListener = null; |
| 89 | + } |
| 90 | + |
| 91 | + if (this.refreshTimeout) { |
| 92 | + clearTimeout(this.refreshTimeout); |
| 93 | + this.refreshTimeout = null; |
| 94 | + } |
| 95 | + } |
| 96 | +} |
0 commit comments