Skip to content

Commit 425cf98

Browse files
committed
Fixed the treeview to update in real time by detecting file save.
Issue #4
1 parent 3ea9e7c commit 425cf98

4 files changed

Lines changed: 112 additions & 1 deletion

File tree

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "tlog-vscode",
33
"displayName": "tlog",
44
"description": "Transient Log - Quick console.log with [TLOG] prefix for easy identification and removal",
5-
"version": "0.1.12",
5+
"version": "0.1.13",
66
"publisher": "KubrickCode",
77
"license": "MIT",
88
"repository": {

src/src/file-watcher.ts

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

src/src/tlog-tree-provider.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from "vscode";
22
import * as cp from "child_process";
33
import * as path from "path";
44
import { rgPath } from "@vscode/ripgrep";
5+
import { TlogFileWatcher } from "./file-watcher";
56

67
const RIPGREP_SEARCH_PATTERN = "console.log.*[TLOG]";
78
const NODE_MODULES_EXCLUDE_PATTERN = "!**/node_modules/**";
@@ -37,9 +38,12 @@ export class TlogTreeDataProvider
3738

3839
private rootNode: TlogDirectoryNode | null = null;
3940
private workspacePath: string = "";
41+
private fileWatcher: TlogFileWatcher;
4042

4143
constructor() {
44+
this.fileWatcher = new TlogFileWatcher(() => this.refresh());
4245
this.refresh();
46+
this.setupAutoRefresh();
4347
}
4448

4549
refresh(): void {
@@ -49,6 +53,17 @@ export class TlogTreeDataProvider
4953
});
5054
}
5155

56+
private setupAutoRefresh(): void {
57+
const workspacePath = this.getWorkspacePath();
58+
if (workspacePath) {
59+
this.fileWatcher.start(workspacePath);
60+
}
61+
}
62+
63+
dispose(): void {
64+
this.fileWatcher.dispose();
65+
}
66+
5267
getTreeItem(element: TlogTreeItem): vscode.TreeItem {
5368
return element;
5469
}
2.1 MB
Binary file not shown.

0 commit comments

Comments
 (0)