Skip to content

Commit a4e89ee

Browse files
authored
Fix detection of duplicate file change events (#1564)
1 parent ecb13ef commit a4e89ee

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

src/utils/documentIndex.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
169169
// request to 50 concurrent calls to avoid hammering the server
170170
const restRateLimiter = new RateLimiter(50);
171171
// A cache of the last time each file was last changed
172-
const lastFileChangeTimes: Map<string, number> = new Map();
172+
const lastChangeMtimes: Map<string, number> = new Map();
173173
// Index classes and routines that currently exist
174174
vscode.workspace
175175
.findFiles(new vscode.RelativePattern(wsFolder, "{**/*.cls,**/*.mac,**/*.int,**/*.inc}"))
@@ -178,7 +178,7 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
178178
const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(wsFolder, "**/*"));
179179
const debouncedCompile = generateCompileFn();
180180
const debouncedDelete = generateDeleteFn(wsFolder.uri);
181-
const updateIndexAndSyncChanges = async (uri: vscode.Uri): Promise<void> => {
181+
const updateIndexAndSyncChanges = async (uri: vscode.Uri, created = false): Promise<void> => {
182182
if (uri.scheme != wsFolder.uri.scheme) {
183183
// We don't care about virtual files that might be
184184
// part of the workspace folder, like "git" files
@@ -194,8 +194,22 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
194194
return;
195195
}
196196
const uriString = uri.toString();
197-
const lastFileChangeTime = lastFileChangeTimes.get(uriString) ?? 0;
198-
lastFileChangeTimes.set(uriString, Date.now());
197+
if (!created) {
198+
const stat = await vscode.workspace.fs.stat(uri).then(undefined, () => {});
199+
if (!stat) {
200+
// If we couldn't get the file's metadata then something is very wrong
201+
touchedByVSCode.delete(uriString);
202+
return;
203+
}
204+
const lastChangeMtime = lastChangeMtimes.get(uriString) ?? 0;
205+
lastChangeMtimes.set(uriString, stat.mtime);
206+
if (stat.mtime == lastChangeMtime) {
207+
// This file change event was triggered on the same version
208+
// of the file as the last event, so ignore this one
209+
touchedByVSCode.delete(uriString);
210+
return;
211+
}
212+
}
199213
if (openLowCodeEditors.has(uriString)) {
200214
// This class is open in a low-code editor, so its name will not change
201215
// and any updates to the class will be handled by that editor
@@ -210,12 +224,6 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
210224
touchedByVSCode.delete(uriString);
211225
return;
212226
}
213-
if (lastFileChangeTimes.get(uriString) - lastFileChangeTime < 300) {
214-
// This file change event came too quickly after the last one to be a
215-
// meaningful change triggered by the user, so consider it a duplicate
216-
touchedByVSCode.delete(uriString);
217-
return;
218-
}
219227
const api = new AtelierAPI(uri);
220228
const conf = vscode.workspace.getConfiguration("objectscript", wsFolder);
221229
const syncLocalChanges: string = conf.get("syncLocalChanges");
@@ -244,7 +252,7 @@ export async function indexWorkspaceFolder(wsFolder: vscode.WorkspaceFolder): Pr
244252
}
245253
};
246254
watcher.onDidChange((uri) => restRateLimiter.call(() => updateIndexAndSyncChanges(uri)));
247-
watcher.onDidCreate((uri) => restRateLimiter.call(() => updateIndexAndSyncChanges(uri)));
255+
watcher.onDidCreate((uri) => restRateLimiter.call(() => updateIndexAndSyncChanges(uri, true)));
248256
watcher.onDidDelete((uri) => {
249257
if (uri.scheme != wsFolder.uri.scheme) {
250258
// We don't care about virtual files that might be

0 commit comments

Comments
 (0)