Skip to content

Commit

Permalink
make setup questions modals and promt until zig.path is set
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Oct 9, 2023
1 parent 3ff67ea commit cab0945
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 25 deletions.
39 changes: 22 additions & 17 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ async function checkUpdate(context: ExtensionContext) {

async function getUpdatedVersion(context: ExtensionContext): Promise<ZigVersion | null> {
const configuration = workspace.getConfiguration("zig");
const zigPath = configuration.get<string | null>("path");
const zigPath = configuration.get<string>("path");
if (zigPath) {
const zigBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zig_install", "zig").fsPath;
if (!zigPath.startsWith(zigBinPath)) return null;
Expand Down Expand Up @@ -156,29 +156,31 @@ export async function setupZig(context: ExtensionContext) {
});

if (!context.globalState.get<boolean>("initialSetupDone")) {
await initialSetup(context);
await context.globalState.update("initialSetupDone", true);
await context.globalState.update("initialSetupDone",
await initialSetup(context)
);
}

const configuration = workspace.getConfiguration("zig", null);
if (configuration.get<string | null>("path") === null) return;
const configuration = workspace.getConfiguration("zig");
if (!configuration.get<string>("path")) return;
if (!configuration.get<boolean>("checkForUpdate")) return;
if (!shouldCheckUpdate(context, "zigUpdate")) return;
await checkUpdate(context);
}

async function initialSetup(context: ExtensionContext) {
const zigConfig = workspace.getConfiguration("zig", null);
async function initialSetup(context: ExtensionContext): Promise<boolean> {
const zigConfig = workspace.getConfiguration("zig");
const zigResponse = await window.showInformationMessage(
"Zig path hasn't been set, do you want to specify the path or install Zig?",
{ modal: true },
"Install", "Specify path", "Use Zig in PATH"
);

if (zigResponse === "Install") {
await selectVersionAndInstall(context);
const configuration = workspace.getConfiguration("zig", null);
const path = configuration.get<string | null>("path");
if (!path) return;
const configuration = workspace.getConfiguration("zig");
const path = configuration.get<string>("path");
if (!path) return false;
window.showInformationMessage(`Zig was installed at '${path}', add it to PATH to use it from the terminal`);
} else if (zigResponse === "Specify path") {
const uris = await window.showOpenDialog({
Expand All @@ -187,21 +189,22 @@ async function initialSetup(context: ExtensionContext) {
canSelectMany: false,
title: "Select Zig executable",
});
if (!uris) return;
if (!uris) return false;

const buffer = child_process.execFileSync(uris[0].path, ["version"]);
const version = semver.parse(buffer.toString("utf8"));
if (!version) return;
if (!version) return false;

await zigConfig.update("path", uris[0].path, true);
} else if (zigResponse === "Use Zig in PATH") {
await zigConfig.update("path", "", true);
} else return;
} else return false;

const zlsConfig = workspace.getConfiguration("zig.zls", null);
const zlsConfig = workspace.getConfiguration("zig.zls");
const zlsResponse = await window.showInformationMessage(
"We recommend enabling ZLS (the Zig Language Server) for a better editing experience. Would you like to install it?",
"Install", "Specify path", "Use ZLS in PATH", "Ignore"
{ modal: true },
"Install", "Specify path", "Use ZLS in PATH"
);

if (zlsResponse === "Install") {
Expand All @@ -213,14 +216,16 @@ async function initialSetup(context: ExtensionContext) {
canSelectMany: false,
title: "Select Zig Language Server (ZLS) executable",
});
if (!uris) return;
if (!uris) return true;

const buffer = child_process.execFileSync(uris[0].path, ["--version"]);
const version = semver.parse(buffer.toString("utf8"));
if (!version) return;
if (!version) return true;

await zlsConfig.update("path", uris[0].path, true);
} else if (zlsResponse === "Use ZLS in PATH") {
await zlsConfig.update("path", "", true);
}

return true;
}
2 changes: 1 addition & 1 deletion src/zigUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export function getExePath(exePath: string | null, exeName: string, optionName:

export function getZigPath(): string {
const configuration = workspace.getConfiguration("zig");
const zigPath = configuration.get<string | null>("path");
const zigPath = configuration.get<string>("path");
return getExePath(zigPath, "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 @@ -90,7 +90,7 @@ export async function stopClient() {
// returns the file system path to the zls executable
export function getZLSPath(): string {
const configuration = workspace.getConfiguration("zig.zls");
const zlsPath = configuration.get<string | null>("path");
const zlsPath = configuration.get<string>("path");
return getExePath(zlsPath, "zls", "zig.zls.path");
}

Expand Down Expand Up @@ -124,8 +124,8 @@ async function getVersionIndex(): Promise<VersionIndex> {
async function checkUpdate(context: ExtensionContext) {
const configuration = workspace.getConfiguration("zig.zls");
const zlsPath = configuration.get<string>("path");
const zigBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zls_install", "zls").fsPath;
if (!zlsPath.startsWith(zigBinPath)) return;
const zlsBinPath = vscode.Uri.joinPath(context.globalStorageUri, "zls_install", "zls").fsPath;
if (!zlsPath.startsWith(zlsBinPath)) return;

// get current version
const buffer = child_process.execFileSync(zlsPath, ["--version"]);
Expand Down Expand Up @@ -235,9 +235,9 @@ async function openConfig() {
}

function checkInstalled(): boolean {
const zlsPath = workspace.getConfiguration("zig.zls").get<string | null>("path");
if (zlsPath === null) window.showErrorMessage("This command cannot be run without setting 'zig.zls.path'.");
return zlsPath !== null;
const zlsPath = workspace.getConfiguration("zig.zls").get<string>("path");
if (!zlsPath) window.showErrorMessage("This command cannot be run without setting 'zig.zls.path'.", { modal: true });
return !!zlsPath;
}

export async function activate(context: ExtensionContext) {
Expand All @@ -246,7 +246,7 @@ export async function activate(context: ExtensionContext) {
vscode.commands.registerCommand("zig.zls.install", async () => {
const zigPath = workspace.getConfiguration("zig").get<string | null>("path");
if (zigPath === null) {
window.showErrorMessage("This command cannot be run without setting 'zig.path'.");
window.showErrorMessage("This command cannot be run without setting 'zig.path'.", { modal: true });
return;
}

Expand Down

0 comments on commit cab0945

Please sign in to comment.