diff --git a/src/zigProvider.ts b/src/zigProvider.ts index 3f05c2a..af3388d 100644 --- a/src/zigProvider.ts +++ b/src/zigProvider.ts @@ -21,7 +21,6 @@ export class ZigProvider implements vscode.Disposable { if (change.affectsConfiguration("zig.path")) { const newValue = this.resolveZigPathConfigOption(); if (newValue) { - this.value = newValue; this.set(this.value); } } @@ -39,20 +38,36 @@ export class ZigProvider implements vscode.Disposable { return this.value?.exe ?? null; } - /** Override which zig executable should be used. The `zig.path` config option will be ignored */ + /** Set the path the Zig executable. The `zig.path` config option will be ignored */ public set(value: ExeWithVersion | null) { this.value = value; this.onChange.fire(value); } + /** + * Set the path the Zig executable. Will be saved in `zig.path` config option. + * + * @param zigPath The path to the zig executable. If `null`, the `zig.path` config option will be removed. + */ + public async setAndSave(zigPath: string | null) { + if (!zigPath) { + await vscode.workspace.getConfiguration("zig").update("path", undefined, true); + return; + } + const newValue = this.resolveZigPathConfigOption(zigPath); + if (!newValue) return; + await vscode.workspace.getConfiguration("zig").update("path", newValue.exe, true); + this.set(newValue); + } + /** Resolves the `zig.path` configuration option */ - private resolveZigPathConfigOption(): ExeWithVersion | null { - const zigPath = vscode.workspace.getConfiguration("zig").get("path", ""); + private resolveZigPathConfigOption(zigPath?: string): ExeWithVersion | null { + 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(`'zig.path' is not valid: ${result.message}`); + void vscode.window.showErrorMessage(result.message); return null; } return result; diff --git a/src/zigSetup.ts b/src/zigSetup.ts index ce47e8f..995d0fa 100644 --- a/src/zigSetup.ts +++ b/src/zigSetup.ts @@ -22,8 +22,7 @@ async function installZig(context: vscode.ExtensionContext) { Object.values(WantedZigVersionSource) as WantedZigVersionSource[], ); if (!wantedZig) { - await vscode.workspace.getConfiguration("zig").update("path", undefined, true); - zigProvider.set(null); + await zigProvider.setAndSave(null); return; } @@ -249,7 +248,7 @@ async function selectVersionAndInstall(context: vscode.ExtensionContext) { title: "Select Zig executable", }); if (!uris) return; - await vscode.workspace.getConfiguration("zig").update("path", uris[0].path, true); + await zigProvider.setAndSave(uris[0].fsPath); break; default: const version = new semver.SemVer(selection.detail ?? selection.label);