Skip to content

Commit 575f924

Browse files
committed
Use VSCode file system watcher
1 parent 4e0d562 commit 575f924

File tree

4 files changed

+13
-50
lines changed

4 files changed

+13
-50
lines changed

packages/common/src/ide/types/FileSystem.types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ export interface FileSystem {
99
* @param onDidChange A function to call on changes
1010
* @returns A disposable to cancel the watcher
1111
*/
12-
watchDir(path: string, onDidChange: PathChangeListener): Disposable;
12+
watch(path: string, onDidChange: PathChangeListener): Disposable;
1313
}

packages/cursorless-engine/src/ScopeVisualizer/ScopeInfoProvider.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,7 @@ export class ScopeInfoProvider {
4343

4444
private constructor(fileSystem: FileSystem) {
4545
this.disposables.push(
46-
// TODO: Watch just the path we care about
47-
fileSystem.watchDir(
48-
path.join(homedir(), ".cursorless"),
49-
this.debouncer.run,
50-
),
46+
fileSystem.watch(spokenFormsPath, this.debouncer.run),
5147
this.debouncer,
5248
);
5349

packages/cursorless-engine/src/languages/LanguageDefinitions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export class LanguageDefinitions {
5757

5858
if (ide().runMode === "development") {
5959
this.disposables.push(
60-
fileSystem.watchDir(this.queryDir, () => {
60+
fileSystem.watch(this.queryDir, () => {
6161
this.languageDefinitions.clear();
6262
this.notifier.notifyListeners();
6363
}),
Lines changed: 10 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,19 @@
11
import {
22
Disposable,
33
FileSystem,
4-
PathChangeListener,
5-
walkFiles,
4+
PathChangeListener
65
} from "@cursorless/common";
7-
import { stat } from "fs/promises";
8-
import { max } from "lodash";
6+
import { RelativePattern, workspace } from "vscode";
97

108
export class VscodeFileSystem implements FileSystem {
11-
watchDir(path: string, onDidChange: PathChangeListener): Disposable {
12-
// Just poll for now; we can take advantage of VSCode's sophisticated
13-
// watcher later. Note that we would need to do a version check, as VSCode
14-
// file watcher is only available in more recent versions of VSCode.
15-
return new PollingFileSystemWatcher(path, onDidChange);
9+
watch(path: string, onDidChange: PathChangeListener): Disposable {
10+
// TODO: What happens on VSCode before 1.64?
11+
// FIXME: Support globs?
12+
const watcher = workspace.createFileSystemWatcher(new RelativePattern(path, "**"));
13+
watcher.onDidChange(onDidChange);
14+
watcher.onDidCreate(onDidChange);
15+
watcher.onDidDelete(onDidChange);
16+
return watcher;
1617
}
1718
}
1819

19-
const CHECK_INTERVAL_MS = 1000;
20-
21-
class PollingFileSystemWatcher implements Disposable {
22-
private maxMtimeMs: number = -1;
23-
private timer: NodeJS.Timer;
24-
25-
constructor(
26-
private readonly path: string,
27-
private readonly onDidChange: PathChangeListener,
28-
) {
29-
this.checkForChanges = this.checkForChanges.bind(this);
30-
this.timer = setInterval(this.checkForChanges, CHECK_INTERVAL_MS);
31-
}
32-
33-
private async checkForChanges() {
34-
const paths = await walkFiles(this.path);
35-
36-
const maxMtime =
37-
max(
38-
(await Promise.all(paths.map((file) => stat(file)))).map(
39-
(stat) => stat.mtimeMs,
40-
),
41-
) ?? 0;
42-
43-
if (maxMtime > this.maxMtimeMs) {
44-
this.maxMtimeMs = maxMtime;
45-
this.onDidChange();
46-
}
47-
}
48-
49-
dispose() {
50-
clearInterval(this.timer);
51-
}
52-
}

0 commit comments

Comments
 (0)