From 192ebbefa503e2417960dc094432f2729e0c31f5 Mon Sep 17 00:00:00 2001 From: Techatrix Date: Sun, 9 Feb 2025 07:20:31 +0100 Subject: [PATCH 1/2] avoid unnecessarily triggering zig provider on change events The difference this change makes can be easily observed by editing a build.zig.zon while using the builtin version manager. ZLS would be restarted every time the file is edited even if it is has no `minimum_zig_version`. --- src/zigProvider.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/zigProvider.ts b/src/zigProvider.ts index 136e648..9360af6 100644 --- a/src/zigProvider.ts +++ b/src/zigProvider.ts @@ -40,6 +40,8 @@ export class ZigProvider implements vscode.Disposable { /** Set the path the Zig executable. The `zig.path` config option will be ignored */ public set(value: ExeWithVersion | null) { + if (value === null && this.value === null) return; + if (value !== null && this.value !== null && value.version.compare(this.value.version) === 0) return; this.value = value; this.onChange.fire(value); } From bb9e901f52d8a7dc87b16ef76302093ad22a738b Mon Sep 17 00:00:00 2001 From: Techatrix Date: Wed, 12 Feb 2025 07:38:56 +0100 Subject: [PATCH 2/2] install Zig if the `zig.path` config option has been unset The ZigProvider does not update the zig version when the `zig.path` has been removed because it isn't aware of the version managment system. To resolve this, the logic is moved into zigSetup.ts --- src/zigProvider.ts | 27 +++++---------------------- src/zigSetup.ts | 10 ++++++++-- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/zigProvider.ts b/src/zigProvider.ts index 9360af6..673cffa 100644 --- a/src/zigProvider.ts +++ b/src/zigProvider.ts @@ -9,23 +9,12 @@ interface ExeWithVersion { version: semver.SemVer; } -export class ZigProvider implements vscode.Disposable { +export class ZigProvider { onChange: vscode.EventEmitter = new vscode.EventEmitter(); private value: ExeWithVersion | null; - private disposables: vscode.Disposable[]; constructor() { - this.value = this.resolveZigPathConfigOption(); - this.disposables = [ - vscode.workspace.onDidChangeConfiguration((change) => { - if (change.affectsConfiguration("zig.path")) { - const newValue = this.resolveZigPathConfigOption(); - if (newValue) { - this.set(newValue); - } - } - }), - ]; + this.value = this.resolveZigPathConfigOption() ?? null; } /** Returns the version of the Zig executable that is currently being used. */ @@ -63,22 +52,16 @@ export class ZigProvider implements vscode.Disposable { this.set(newValue); } - /** Resolves the `zig.path` configuration option */ - private resolveZigPathConfigOption(zigPath?: string): ExeWithVersion | null { + /** Resolves the `zig.path` configuration option. */ + public resolveZigPathConfigOption(zigPath?: string): ExeWithVersion | null | undefined { zigPath ??= vscode.workspace.getConfiguration("zig").get("path", ""); if (!zigPath) return null; const exePath = zigPath !== "zig" ? zigPath : null; // the string "zig" means lookup in PATH const result = resolveExePathAndVersion(exePath, "zig", "zig.path", "version"); if ("message" in result) { void vscode.window.showErrorMessage(result.message); - return null; + return undefined; } return result; } - - dispose() { - for (const disposable of this.disposables) { - disposable.dispose(); - } - } } diff --git a/src/zigSetup.ts b/src/zigSetup.ts index de96237..66bdc6d 100644 --- a/src/zigSetup.ts +++ b/src/zigSetup.ts @@ -641,17 +641,23 @@ export async function setupZig(context: vscode.ExtensionContext) { onDidChangeActiveTextEditor(vscode.window.activeTextEditor); context.subscriptions.push( - zigProvider, statusItem, languageStatusItem, vscode.commands.registerCommand("zig.install", async () => { await selectVersionAndInstall(context); }), vscode.workspace.onDidChangeConfiguration((change) => { - // The `zig.path` config option is handled by `zigProvider.onChange`. if (change.affectsConfiguration("zig.version")) { void refreshZigInstallation(); } + if (change.affectsConfiguration("zig.path")) { + const result = zigProvider.resolveZigPathConfigOption(); + if (result === undefined) return; // error message already reported + if (result !== null) { + zigProvider.set(result); + } + void refreshZigInstallation(); + } }), vscode.window.onDidChangeActiveTextEditor(onDidChangeActiveTextEditor), zigProvider.onChange.event(() => {