Skip to content

Commit

Permalink
do the javascript
Browse files Browse the repository at this point in the history
Adding types doesn't actually make `undefined`, `null` and `""` different things.
  • Loading branch information
Vexu committed Apr 16, 2024
1 parent ca47c7e commit 0759eb8
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 21 deletions.
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
"zig.path": {
"scope": "machine-overridable",
"type": "string",
"default": null,
"description": "Set a custom path to the Zig binary. The string \"zig\" means lookup zig in PATH."
},
"zig.checkForUpdate": {
Expand Down Expand Up @@ -160,7 +159,6 @@
"zig.zls.path": {
"scope": "machine-overridable",
"type": "string",
"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"
},
Expand Down
16 changes: 8 additions & 8 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ async function checkUpdate(context: vscode.ExtensionContext) {

async function getUpdatedVersion(context: vscode.ExtensionContext): Promise<ZigVersion | null> {
const configuration = vscode.workspace.getConfiguration("zig");
const zigPath = configuration.get<string | null>("path", null);
const zigPath = configuration.get<string>("path");
const zigBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zig_install", "zig").fsPath;
if (!zigPath?.startsWith(zigBinPath)) return null;

Expand Down Expand Up @@ -233,14 +233,14 @@ export async function setupZig(context: vscode.ExtensionContext) {

const zigConfig = vscode.workspace.getConfiguration("zig");
const initialSetupDone = zigConfig.get<boolean>("initialSetupDone", false);
const zigPath = zigConfig.get<string | null>("path");
if (zigPath === "" || (initialSetupDone && zigPath === null)) {
const zigPath = zigConfig.get<string>("path");
if (zigPath === "" && initialSetupDone) {
await zigConfig.update("path", "zig", true);
}

const zlsConfig = vscode.workspace.getConfiguration("zig.zls");
const zlsPath = zlsConfig.get<string | null>("path");
if (zlsPath === "" || (initialSetupDone && zlsPath === null)) {
const zlsPath = zlsConfig.get<string>("path");
if (zlsPath === "" && initialSetupDone) {
await zlsConfig.update("path", "zls", true);
}
}
Expand Down Expand Up @@ -276,7 +276,7 @@ export async function setupZig(context: vscode.ExtensionContext) {
async function initialSetup(context: vscode.ExtensionContext): Promise<boolean> {
const zigConfig = vscode.workspace.getConfiguration("zig");

if (zigConfig.get<string | null>("path", null) === null) {
if (!zigConfig.get<string>("path")) {
const zigResponse = await vscode.window.showInformationMessage(
"Zig path hasn't been set, do you want to specify the path or install Zig?",
{ modal: true },
Expand All @@ -287,7 +287,7 @@ async function initialSetup(context: vscode.ExtensionContext): Promise<boolean>
switch (zigResponse) {
case "Install":
await selectVersionAndInstall(context);
const zigPath = vscode.workspace.getConfiguration("zig").get<string | null>("path", null);
const zigPath = vscode.workspace.getConfiguration("zig").get<string>("path");
if (!zigPath) return false;
break;
case "Specify path":
Expand All @@ -314,7 +314,7 @@ async function initialSetup(context: vscode.ExtensionContext): Promise<boolean>

const zlsConfig = vscode.workspace.getConfiguration("zig.zls");

if (zlsConfig.get<string | null>("path", null) === null) {
if (!zlsConfig.get<string>("path")) {
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 },
Expand Down
8 changes: 4 additions & 4 deletions src/zigUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ export function handleConfigOption(input: string): string {
return input;
}

export function getExePath(exePath: string | null, exeName: string, optionName: string): string {
if (exePath === null) {
export function getExePath(exePath: string | null | undefined, exeName: string, optionName: string): string {
if (!exePath) {
exePath = which.sync(exeName, { nothrow: true });
} else {
// allow passing predefined variables
Expand All @@ -64,7 +64,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName:
}

let message;
if (exePath === null) {
if (!exePath) {
message = `Could not find ${exeName} in PATH`;
} else if (!fs.existsSync(exePath)) {
message = `\`${optionName}\` ${exePath} does not exist`;
Expand All @@ -82,7 +82,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName:

export function getZigPath(): string {
const configuration = vscode.workspace.getConfiguration("zig");
const zigPath = configuration.get<string | null>("path", null);
const zigPath = configuration.get<string>("path");
const exePath = zigPath !== "zig" ? zigPath : null; // the string "zig" means lookup in PATH
return getExePath(exePath, "zig", "zig.path");
}
Expand Down
14 changes: 7 additions & 7 deletions src/zls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ 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<string | null>("path", null);
const zlsPath = configuration.get<string>("path");
const exePath = zlsPath !== "zls" ? zlsPath : null; // the string "zls" means lookup in PATH
return getExePath(exePath, "zls", "zig.zls.path");
}
Expand Down Expand Up @@ -201,7 +201,7 @@ async function getVersionIndex(): Promise<VersionIndex> {
// 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<string | null>("path", null);
const zlsPath = configuration.get<string>("path");
const zlsBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zls_install", "zls").fsPath;
if (!zlsPath?.startsWith(zlsBinPath)) return;

Expand Down Expand Up @@ -238,7 +238,7 @@ export async function install(context: vscode.ExtensionContext, ask: boolean) {
}
// Zig 0.9.0 was the first version to have a tagged zls release
if (semver.lt(zigVersion, "0.9.0")) {
if (zlsConfiguration.get<string | null>("path", null) !== null) {
if (zlsConfiguration.get<string>("path")) {
void vscode.window.showErrorMessage(`ZLS is not available for Zig version ${zigVersion.version}`);
}
await zlsConfiguration.update("path", undefined, true);
Expand Down Expand Up @@ -343,8 +343,8 @@ async function installVersion(context: vscode.ExtensionContext, version: semver.
}

function checkInstalled(): boolean {
const zlsPath = vscode.workspace.getConfiguration("zig.zls").get<string | null>("path", null);
if (zlsPath === null) {
const zlsPath = vscode.workspace.getConfiguration("zig.zls").get<string>("path");
if (!zlsPath) {
void vscode.window.showErrorMessage("This command cannot be run without setting 'zig.zls.path'.", {
modal: true,
});
Expand Down Expand Up @@ -395,15 +395,15 @@ export async function activate(context: vscode.ExtensionContext) {
) {
await stopClient();
const zlsConfig = vscode.workspace.getConfiguration("zig.zls");
if (zlsConfig.get<string | null>("path", null) !== null) {
if (zlsConfig.get<string>("path")) {
await startClient();
}
}
}),
);

const zlsConfig = vscode.workspace.getConfiguration("zig.zls");
if (zlsConfig.get<string | null>("path", null) === null) return;
if (!zlsConfig.get<string>("path")) return;
if (zlsConfig.get<boolean>("checkForUpdate") && (await shouldCheckUpdate(context, "zlsUpdate"))) {
await checkUpdate(context);
}
Expand Down

0 comments on commit 0759eb8

Please sign in to comment.