Skip to content

Commit

Permalink
add option to always search for zig in PATH
Browse files Browse the repository at this point in the history
  • Loading branch information
Vexu committed Jun 26, 2023
1 parent 6f766f1 commit a6f239a
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"zig.zigPath": {
"type": "string",
"default": null,
"description": "Set a custom path to the Zig binary."
"description": "Set a custom path to the Zig binary. Empty string will lookup zig in PATH."
},
"zig.zigVersion": {
"type": "string",
Expand Down
3 changes: 2 additions & 1 deletion src/zigBuild.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { buildDiagnosticCollection, logChannel } from './extension';
import * as cp from 'child_process';
import * as vscode from 'vscode';
import { getZigPath } from './zigUtil';

export function zigBuild(): void {
const editor = vscode.window.activeTextEditor;
Expand Down Expand Up @@ -28,7 +29,7 @@ export function zigBuild(): void {
});

const cwd = vscode.workspace.getWorkspaceFolder(editor.document.uri).uri.fsPath;
const buildPath = config.get<string>("zigPath");
const buildPath = getZigPath();

logChannel.appendLine(`Starting building the current workspace at ${cwd}`);

Expand Down
6 changes: 3 additions & 3 deletions src/zigCompilerProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as cp from "child_process";
import * as vscode from "vscode";
// This will be treeshaked to only the debounce function
import { throttle } from "lodash-es";
import { getZigPath } from "./zigUtil";

export default class ZigCompilerProvider implements vscode.CodeActionProvider {
private buildDiagnostics: vscode.DiagnosticCollection;
Expand Down Expand Up @@ -58,16 +59,15 @@ export default class ZigCompilerProvider implements vscode.CodeActionProvider {
}

private _doASTGenErrorCheck(change: vscode.TextDocumentChangeEvent) {
let config = vscode.workspace.getConfiguration("zig");
const textDocument = change.document;
if (textDocument.languageId !== "zig") {
return;
}
const zig_path = config.get("zigPath");
const zigPath = getZigPath();
const cwd = vscode.workspace.getWorkspaceFolder(textDocument.uri).uri
.fsPath;

let childProcess = cp.spawn(zig_path as string, ["ast-check"], { cwd });
let childProcess = cp.spawn(zigPath, ["ast-check"], { cwd });

if (!childProcess.pid) {
return;
Expand Down
5 changes: 2 additions & 3 deletions src/zigFormat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as vscode from 'vscode';
import { TextEdit, OutputChannel } from 'vscode';
import { execCmd } from './zigUtil';
import { execCmd, getZigPath } from './zigUtil';

export class ZigFormatProvider implements vscode.DocumentFormattingEditProvider {
private _channel: OutputChannel;
Expand Down Expand Up @@ -80,8 +80,7 @@ export class ZigRangeFormatProvider implements vscode.DocumentRangeFormattingEdi
}

function zigFormat(document: vscode.TextDocument) {
const config = vscode.workspace.getConfiguration("zig");
const zigPath = config.get<string>("zigPath");
const zigPath = getZigPath();

const options = {
cmdArguments: ['fmt', '--stdin'],
Expand Down
9 changes: 3 additions & 6 deletions src/zigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,10 @@ export async function setupZig(context: ExtensionContext) {
});

const configuration = workspace.getConfiguration("zig", null);
if (!configuration.get<string | null>("zigPath", null)) {
const zigInPath = which.sync("zig", { nothrow: true });
const options = ["Install", "Specify path"];
if (zigInPath) options.push("Use Zig in PATH");
if (configuration.get<string | null>("zigPath", null) === null) {
const response = await window.showInformationMessage(
"Zig path hasn't been set, do you want to specify the path or install Zig?",
...options
"Install", "Specify path", "Use Zig in PATH"
);

if (response === "Install") {
Expand All @@ -200,7 +197,7 @@ export async function setupZig(context: ExtensionContext) {
await configuration.update("zigPath", uris[0].fsPath, true);
}
} else if (response == "Use Zig in PATH") {
await configuration.update("zigPath", zigInPath, true);
await configuration.update("zigPath", "", true);
} else throw "zigPath not specified";
}

Expand Down
16 changes: 15 additions & 1 deletion src/zigUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as cp from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { window, workspace } from 'vscode';
import which from 'which';

export const isWindows = process.platform === 'win32';

Expand Down Expand Up @@ -50,7 +51,7 @@ export function execCmd
let cmdArguments = options ? options.cmdArguments : [];

childProcess =
cp.execFile(cmd, cmdArguments, { cwd: detectProjectRoot(fileName || workspace.rootPath + '/fakeFileName'), maxBuffer: 10 * 1024 * 1024}, handleExit);
cp.execFile(cmd, cmdArguments, { cwd: detectProjectRoot(fileName || workspace.rootPath + '/fakeFileName'), maxBuffer: 10 * 1024 * 1024 }, handleExit);


childProcess.stdout.on('data', (data: Buffer) => {
Expand Down Expand Up @@ -143,3 +144,16 @@ export function detectProjectRoot(fileName: string): string {
}
return undefined;
}

export function getZigPath(): string {
const configuration = workspace.getConfiguration("zig");
let zigPath = configuration.get<string | null>("zigPath", null);
if (!zigPath) {
zigPath = which.sync("zig", { nothrow: true });
if (!zigPath) {
window.showErrorMessage("zig not found in PATH");
throw "zig not found in PATH";
}
}
return zigPath;
}
37 changes: 31 additions & 6 deletions src/zls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
} from "vscode-languageclient/node";
import which from "which";
import { shouldCheckUpdate } from "./extension";
import { getZigPath } from "./zigUtil";

export let outputChannel: vscode.OutputChannel;
export let client: LanguageClient | null = null;
Expand Down Expand Up @@ -127,10 +128,12 @@ export async function startClient(context: ExtensionContext) {
workspace: {
async configuration(params, token, next) {
let indexOfAstCheck = null;
let indexOfZigPath = null;

for (const [index, param] of Object.entries(params.items)) {
if (param.section === "zls.zig_exe_path") {
param.section = `zig.zigPath`;
indexOfZigPath = index;
} else if (param.section === "zls.enable_ast_check_diagnostics") {
indexOfAstCheck = index;
} else {
Expand All @@ -143,6 +146,13 @@ export async function startClient(context: ExtensionContext) {
if (indexOfAstCheck !== null) {
result[indexOfAstCheck] = workspace.getConfiguration("zig").get<string>("astCheckProvider", "zls") === "zls";
}
if (indexOfZigPath !== null) {
try {
result[indexOfZigPath] = getZigPath();
} catch {
result[indexOfZigPath] = "zig";
}
}

return result;
}
Expand Down Expand Up @@ -382,14 +392,29 @@ export async function activate(context: ExtensionContext) {

const configuration = workspace.getConfiguration("zig.zls", null);
if (!configuration.get<string | null>("path", null)) {
const response = await window.showInformationMessage("We recommend enabling ZLS (the Zig Language Server) for a better editing experience. Would you like to enable it? You can always change this later by modifying `zig.zls.enabled` in your settings.", "Enable", "Disable");
const response = await window.showInformationMessage(
"We recommend enabling ZLS (the Zig Language Server) for a better editing experience. Would you like to install it? You can always change this later by modifying `zig.zls.enabled` in your settings.",
"Install", "Specify path", "Use ZLS in PATH", "Disable"
);

if (response === "Enable") {
if (response === "Install") {
await configuration.update("enabled", true, true);
await installExecutable(context);
} else if (response === "Disable") {
await configuration.update("enabled", false, true);
return;
}
} else if (response === "Specify path") {
await configuration.update("enabled", true, true);
const uris = await window.showOpenDialog({
canSelectFiles: true,
canSelectFolders: false,
canSelectMany: false,
title: "Select Zig Language Server (ZLS) executable",
});

if (uris) {
await configuration.update("path", uris[0].fsPath, true);
}
} else {
await configuration.update("enabled", response === "Use ZLS in PATH", true);
};
}

if (shouldCheckUpdate(context, "zlsUpdate")) {
Expand Down

0 comments on commit a6f239a

Please sign in to comment.