diff --git a/package-lock.json b/package-lock.json index a67ced5..cfbdfe5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -33,7 +33,7 @@ "vscode-test": "^1.4.0" }, "engines": { - "vscode": "^1.76.0" + "vscode": "^1.80.0" } }, "node_modules/@aashutoshrathi/word-wrap": { diff --git a/package.json b/package.json index 646022d..2a833b7 100644 --- a/package.json +++ b/package.json @@ -104,9 +104,12 @@ }, "zig.path": { "scope": "machine-overridable", - "type": "string", - "default": "", - "description": "Set a custom path to the Zig binary. Empty string will lookup zig in PATH." + "type": [ + "string", + "null" + ], + "default": null, + "description": "Set a custom path to the Zig binary. The string \"zig\" means lookup zig in PATH." }, "zig.checkForUpdate": { "scope": "resource", @@ -164,9 +167,12 @@ }, "zig.zls.path": { "scope": "machine-overridable", - "type": "string", - "default": "", - "description": "Path to `zls` executable. Example: `C:/zls/zig-cache/bin/zls.exe`. Empty string will lookup ZLS in PATH.", + "type": [ + "string", + "null" + ], + "default": null, + "description": "Path to `zls` executable. Example: `C:/zls/zig-cache/bin/zls.exe`. The string \"zls\" means lookup ZLS in PATH.", "format": "path" }, "zig.zls.enableSnippets": { diff --git a/src/zigSetup.ts b/src/zigSetup.ts index 951aa40..0c92699 100644 --- a/src/zigSetup.ts +++ b/src/zigSetup.ts @@ -191,13 +191,9 @@ async function checkUpdate(context: vscode.ExtensionContext) { async function getUpdatedVersion(context: vscode.ExtensionContext): Promise { const configuration = vscode.workspace.getConfiguration("zig"); - const zigPath = configuration.get("path"); - if (zigPath) { - const zigBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zig_install", "zig").fsPath; - if (!zigPath.startsWith(zigBinPath)) return null; - } else { - return null; - } + const zigPath = configuration.get("path", null); + const zigBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zig_install", "zig").fsPath; + if (!zigPath?.startsWith(zigBinPath)) return null; const curVersion = getVersion(zigPath, "version"); if (!curVersion) return null; @@ -240,6 +236,24 @@ export async function setupZig(context: vscode.ExtensionContext) { await checkUpdate(context); }); + { + // convert an empty string for `zig.path` and `zig.zls.path` to `zig` and `zls` respectively. + // This check can be removed once enough time has passed so that most users switched to the new value + + const zigConfig = vscode.workspace.getConfiguration("zig"); + const initialSetupDone = zigConfig.get("initialSetupDone", false); + const zigPath = zigConfig.get("path"); + if (zigPath === "" || (initialSetupDone && zigPath === null)) { + await zigConfig.update("path", "zig", true); + } + + const zlsConfig = vscode.workspace.getConfiguration("zig.zls"); + const zlsPath = zlsConfig.get("path"); + if (zlsPath === "" || (initialSetupDone && zlsPath === null)) { + await zlsConfig.update("path", "zls", true); + } + } + context.environmentVariableCollection.description = "Add Zig to PATH"; updateZigEnvironmentVariableCollection(context); vscode.workspace.onDidChangeConfiguration((change) => { @@ -253,7 +267,6 @@ export async function setupZig(context: vscode.ExtensionContext) { await configuration.update("initialSetupDone", await initialSetup(context), true); } - if (!configuration.get("path")) return; if (!configuration.get("checkForUpdate")) return; if (!(await shouldCheckUpdate(context, "zigUpdate"))) return; await checkUpdate(context); @@ -262,7 +275,7 @@ export async function setupZig(context: vscode.ExtensionContext) { async function initialSetup(context: vscode.ExtensionContext): Promise { const zigConfig = vscode.workspace.getConfiguration("zig"); - if (zigConfig.get("path") === "") { + if (zigConfig.get("path", null) === null) { const zigResponse = await vscode.window.showInformationMessage( "Zig path hasn't been set, do you want to specify the path or install Zig?", { modal: true }, @@ -273,7 +286,7 @@ async function initialSetup(context: vscode.ExtensionContext): Promise switch (zigResponse) { case "Install": await selectVersionAndInstall(context); - const zigPath = zigConfig.get("path"); + const zigPath = vscode.workspace.getConfiguration("zig").get("path", null); if (!zigPath) return false; break; case "Specify path": @@ -291,16 +304,17 @@ async function initialSetup(context: vscode.ExtensionContext): Promise await zigConfig.update("path", uris[0].path, true); break; case "Use Zig in PATH": - await zigConfig.update("path", "", true); + await zigConfig.update("path", "zig", true); break; case undefined: + await zigConfig.update("path", undefined, true); return false; } } const zlsConfig = vscode.workspace.getConfiguration("zig.zls"); - if (zlsConfig.get("path") === "") { + if (zlsConfig.get("path", null) === null) { const zlsResponse = await vscode.window.showInformationMessage( "We recommend enabling ZLS (the Zig Language Server) for a better editing experience. Would you like to install it?", { modal: true }, @@ -324,9 +338,11 @@ async function initialSetup(context: vscode.ExtensionContext): Promise await zlsConfig.update("path", uris[0].path, true); case "Use ZLS in PATH": - await zlsConfig.update("path", "", true); + await zlsConfig.update("path", "zls", true); break; case undefined: + // explicitly set `zig.zls.path` to null so it is visible in the `settings.json` + await zlsConfig.update("path", null, true); break; } } diff --git a/src/zigUtil.ts b/src/zigUtil.ts index 77beea1..ee2a7cb 100644 --- a/src/zigUtil.ts +++ b/src/zigUtil.ts @@ -21,7 +21,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName: } } - if (!exePath) { + if (exePath === null) { exePath = which.sync(exeName, { nothrow: true }); } else if (exePath.startsWith("~")) { exePath = path.join(os.homedir(), exePath.substring(1)); @@ -30,7 +30,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName: } let message; - if (!exePath) { + if (exePath === null) { message = `Could not find ${exeName} in PATH`; } else if (!fs.existsSync(exePath)) { message = `\`${optionName}\` ${exePath} does not exist`; @@ -48,8 +48,9 @@ export function getExePath(exePath: string | null, exeName: string, optionName: export function getZigPath(): string { const configuration = vscode.workspace.getConfiguration("zig"); - const zigPath = configuration.get("path") ?? null; - return getExePath(zigPath, "zig", "zig.path"); + const zigPath = configuration.get("path", null); + const exePath = zigPath !== "zig" ? zigPath : null; // the string "zig" means lookup in PATH + return getExePath(exePath, "zig", "zig.path"); } // Check timestamp `key` to avoid automatically checking for updates diff --git a/src/zls.ts b/src/zls.ts index 0e66756..de2ebfb 100644 --- a/src/zls.ts +++ b/src/zls.ts @@ -72,8 +72,9 @@ export async function stopClient() { // returns the file system path to the zls executable export function getZLSPath(): string { const configuration = vscode.workspace.getConfiguration("zig.zls"); - const zlsPath = configuration.get("path") ?? null; - return getExePath(zlsPath, "zls", "zig.zls.path"); + const zlsPath = configuration.get("path", null); + const exePath = zlsPath !== "zls" ? zlsPath : null; // the string "zls" means lookup in PATH + return getExePath(exePath, "zls", "zig.zls.path"); } async function configurationMiddleware( @@ -182,7 +183,7 @@ async function getVersionIndex(): Promise { // checks whether there is newer version on master async function checkUpdate(context: vscode.ExtensionContext) { const configuration = vscode.workspace.getConfiguration("zig.zls"); - const zlsPath = configuration.get("path"); + const zlsPath = configuration.get("path", null); const zlsBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zls_install", "zls").fsPath; if (!zlsPath) return; if (!zlsPath.startsWith(zlsBinPath)) return; @@ -325,13 +326,14 @@ async function installVersion(context: vscode.ExtensionContext, version: semver. } function checkInstalled(): boolean { - const zlsPath = vscode.workspace.getConfiguration("zig.zls").get("path"); - if (!zlsPath) { + const zlsPath = vscode.workspace.getConfiguration("zig.zls").get("path", null); + if (zlsPath === null) { void vscode.window.showErrorMessage("This command cannot be run without setting 'zig.zls.path'.", { modal: true, }); + return false; } - return !!zlsPath; + return true; } export async function activate(context: vscode.ExtensionContext) { @@ -378,14 +380,14 @@ export async function activate(context: vscode.ExtensionContext) { ) { await stopClient(); const zlsConfig = vscode.workspace.getConfiguration("zig.zls"); - if (!!zlsConfig.get("path")) { + if (zlsConfig.get("path", null) !== null) { await startClient(); } } }, context.subscriptions); const zlsConfig = vscode.workspace.getConfiguration("zig.zls"); - if (zlsConfig.get("path") === undefined) return; + if (zlsConfig.get("path", null) === null) return; if (zlsConfig.get("checkForUpdate") && (await shouldCheckUpdate(context, "zlsUpdate"))) { await checkUpdate(context); }