Skip to content

Commit

Permalink
automatically manage ZLS versions
Browse files Browse the repository at this point in the history
These changes make ZLS an "invisible" component of the extension after the initial setup.

This also removes the `zig.zls.checkForUpdate` command. I am not sure if it even needs to be brought back. I've made sure that no error is reported if no internet connection is available. The last installed ZLS version should be reused.

closes #136 because `zig.zls.startRestart` will just enable ZLS instead of complaining
  • Loading branch information
Techatrix committed Sep 24, 2024
1 parent ff6fc57 commit 41f9443
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 261 deletions.
21 changes: 8 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@
],
"default": "off"
},
"zig.zls.checkForUpdate": {
"zig.zls.enabled": {
"scope": "resource",
"type": "boolean",
"description": "Whether to automatically check for new updates",
"default": true
"description": "Whether to enable the optional ZLS Language Server",
"default": null
},
"zig.zls.path": {
"scope": "machine-overridable",
"type": "string",
"description": "Path to `zls` executable. Example: `C:/zls/zig-cache/bin/zls.exe`. The string \"zls\" means lookup ZLS in PATH.",
"description": "Set a custom path to the `zls` executable. Example: `C:/zls/zig-cache/bin/zls.exe`. The string \"zls\" means lookup ZLS in PATH.",
"format": "path"
},
"zig.zls.enableSnippets": {
Expand Down Expand Up @@ -329,23 +329,18 @@
"category": "Zig Setup"
},
{
"command": "zig.zls.install",
"title": "Install Server",
"command": "zig.zls.enable",
"title": "Enable Language Server",
"category": "Zig Language Server"
},
{
"command": "zig.zls.startRestart",
"title": "Start / Restart Server",
"title": "Start / Restart Language Server",
"category": "Zig Language Server"
},
{
"command": "zig.zls.stop",
"title": "Stop Server",
"category": "Zig Language Server"
},
{
"command": "zig.zls.update",
"title": "Check for Server Updates",
"title": "Stop Language Server",
"category": "Zig Language Server"
}
],
Expand Down
17 changes: 17 additions & 0 deletions src/zigFormat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import vscode from "vscode";
import childProcess from "child_process";
import util from "util";

import { DocumentRangeFormattingRequest, TextDocumentIdentifier } from "vscode-languageclient";

import * as zls from "./zls";
import { getZigPath } from "./zigUtil";

const execFile = util.promisify(childProcess.execFile);
Expand Down Expand Up @@ -61,6 +64,20 @@ async function provideDocumentRangeFormattingEdits(
options: vscode.FormattingOptions,
token: vscode.CancellationToken,
): Promise<vscode.TextEdit[] | null> {
if (vscode.workspace.getConfiguration("zig").get<string>("formattingProvider") === "zls") {
if (zls.client !== null) {
return await (zls.client.sendRequest(
DocumentRangeFormattingRequest.type,
{
textDocument: TextDocumentIdentifier.create(document.uri.toString()),
range: range,
options: options,
},
token,
) as Promise<vscode.TextEdit[] | null>);
}
}

const zigPath = getZigPath();

const abortController = new AbortController();
Expand Down
112 changes: 40 additions & 72 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import semver from "semver";
import vscode from "vscode";

import { downloadAndExtractArtifact, getHostZigName, getVersion, getZigPath, shouldCheckUpdate } from "./zigUtil";
import { installZLS } from "./zls";
import { restartClient } from "./zls";

const DOWNLOAD_INDEX = "https://ziglang.org/download/index.json";

Expand Down Expand Up @@ -40,6 +40,8 @@ export async function installZig(context: vscode.ExtensionContext, version: ZigV
void vscode.window.showInformationMessage(
`Zig has been installed successfully. Relaunch your integrated terminal to make it available.`,
);

void restartClient(context);
}
}

Expand Down Expand Up @@ -180,9 +182,11 @@ export async function setupZig(context: vscode.ExtensionContext) {
}

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

Expand All @@ -192,7 +196,6 @@ export async function setupZig(context: vscode.ExtensionContext) {
context.subscriptions.push(
vscode.commands.registerCommand("zig.install", async () => {
await selectVersionAndInstall(context);
await installZLS(context, true);
}),
vscode.commands.registerCommand("zig.update", async () => {
await checkUpdate(context);
Expand All @@ -216,75 +219,40 @@ 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>("path")) return true;

const zigResponse = await vscode.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",
);
switch (zigResponse) {
case "Install":
await selectVersionAndInstall(context);
const zigPath = vscode.workspace.getConfiguration("zig").get<string>("path");
if (!zigPath) return false;
break;
case "Specify path":
const uris = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
title: "Select Zig executable",
});
if (!uris) return false;

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 },
"Install",
"Specify path",
"Use Zig in PATH",
);
switch (zigResponse) {
case "Install":
await selectVersionAndInstall(context);
const zigPath = vscode.workspace.getConfiguration("zig").get<string>("path");
if (!zigPath) return false;
break;
case "Specify path":
const uris = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
title: "Select Zig executable",
});
if (!uris) return false;

const version = getVersion(uris[0].path, "version");
if (!version) return false;

await zigConfig.update("path", uris[0].path, true);
break;
case "Use Zig in PATH":
await zigConfig.update("path", "zig", true);
break;
case undefined:
return false;
}
}

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

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 },
"Install",
"Specify path",
"Use ZLS in PATH",
);

switch (zlsResponse) {
case "Install":
await installZLS(context, false);
break;
case "Specify path":
const uris = await vscode.window.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
title: "Select Zig Language Server (ZLS) executable",
});
if (!uris) return true;
const version = getVersion(uris[0].path, "version");
if (!version) return false;

await zlsConfig.update("path", uris[0].path, true);
break;
case "Use ZLS in PATH":
await zlsConfig.update("path", "zls", true);
break;
case undefined:
break;
}
await zigConfig.update("path", uris[0].path, true);
break;
case "Use Zig in PATH":
await zigConfig.update("path", "zig", true);
break;
case undefined:
return false;
}

return true;
Expand Down
Loading

0 comments on commit 41f9443

Please sign in to comment.