Skip to content

Commit b6d9735

Browse files
committed
Correctly watch run settings without workspaces
Fixes #133
1 parent 848fbfa commit b6d9735

File tree

1 file changed

+60
-10
lines changed

1 file changed

+60
-10
lines changed

src/extension/utils/vscode.ts

Lines changed: 60 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,55 @@ export function resolveVariables(
446446

447447
const _runSettingsCache = new Map<string, Record<string, unknown>>();
448448
let _runSettingsWatcher: vscode.FileSystemWatcher | undefined = undefined;
449+
let _runSettingsWatcherKey: string | undefined = undefined;
450+
let _runSettingsWatcherLifecycle: vscode.Disposable | undefined = undefined;
449451
let _schemaDefaults: Record<string, string> | undefined = undefined;
450452
let _extensionContext: vscode.ExtensionContext | undefined = undefined;
451453

454+
function disposeRunSettingsWatcher() {
455+
_runSettingsWatcher?.dispose();
456+
_runSettingsWatcher = undefined;
457+
_runSettingsWatcherKey = undefined;
458+
}
459+
460+
function updateRunSettingsWatcher() {
461+
let pattern: vscode.GlobPattern | undefined = undefined;
462+
let watcherKey: string | undefined = undefined;
463+
464+
if ((vscode.workspace.workspaceFolders?.length ?? 0) > 0) {
465+
pattern = "**/runSettings.json";
466+
watcherKey = "workspace";
467+
} else {
468+
const activeUri = vscode.window.activeTextEditor?.document.uri;
469+
if (activeUri && activeUri.scheme === "file") {
470+
const directory = path.dirname(activeUri.fsPath);
471+
pattern = new vscode.RelativePattern(vscode.Uri.file(directory), "runSettings.json");
472+
watcherKey = `no-workspace:${directory}`;
473+
}
474+
}
475+
476+
if (!pattern || !watcherKey) {
477+
disposeRunSettingsWatcher();
478+
return;
479+
}
480+
481+
if (_runSettingsWatcher && _runSettingsWatcherKey === watcherKey) {
482+
return;
483+
}
484+
485+
disposeRunSettingsWatcher();
486+
487+
_runSettingsWatcher = vscode.workspace.createFileSystemWatcher(pattern);
488+
_runSettingsWatcherKey = watcherKey;
489+
490+
const clearCache = () => {
491+
_runSettingsCache.clear();
492+
};
493+
_runSettingsWatcher.onDidCreate(clearCache);
494+
_runSettingsWatcher.onDidChange(clearCache);
495+
_runSettingsWatcher.onDidDelete(clearCache);
496+
}
497+
452498
/**
453499
* Loads default values from the runSettings.json schema file.
454500
* Caches the result after first load.
@@ -501,21 +547,25 @@ export function initializeRunSettingsWatcher(context: vscode.ExtensionContext):
501547
logger = getLogger("vscode");
502548
_extensionContext = context;
503549

504-
if (_runSettingsWatcher) {
550+
if (_runSettingsWatcherLifecycle) {
505551
return;
506552
}
507553

508-
// Watch for runSettings.json files in all workspace folders
509-
_runSettingsWatcher = vscode.workspace.createFileSystemWatcher("**/runSettings.json");
554+
const activeEditorWatcher = vscode.window.onDidChangeActiveTextEditor(updateRunSettingsWatcher);
555+
const workspaceFoldersWatcher =
556+
vscode.workspace.onDidChangeWorkspaceFolders(updateRunSettingsWatcher);
557+
_runSettingsWatcherLifecycle = vscode.Disposable.from(
558+
activeEditorWatcher,
559+
workspaceFoldersWatcher,
560+
new vscode.Disposable(() => {
561+
disposeRunSettingsWatcher();
562+
_runSettingsWatcherLifecycle = undefined;
563+
})
564+
);
510565

511-
const clearCache = () => {
512-
_runSettingsCache.clear();
513-
};
514-
_runSettingsWatcher.onDidCreate(clearCache);
515-
_runSettingsWatcher.onDidChange(clearCache);
516-
_runSettingsWatcher.onDidDelete(clearCache);
566+
updateRunSettingsWatcher();
517567

518-
context.subscriptions.push(_runSettingsWatcher);
568+
context.subscriptions.push(_runSettingsWatcherLifecycle);
519569
}
520570

521571
export function deepMerge(

0 commit comments

Comments
 (0)