Skip to content

Commit

Permalink
add zig.zls.additionalOptions
Browse files Browse the repository at this point in the history
fixes  #182
  • Loading branch information
Techatrix authored and Vexu committed Apr 6, 2024
1 parent eb2453f commit ac8bff3
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 32 deletions.
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,16 @@
"type": "boolean",
"description": "When false, the function signature of completion results is hidden. Improves readability in some editors",
"default": true
},
"zig.zls.additionalOptions": {
"scope": "resource",
"type": "object",
"markdownDescription": "Additional config options that should be forwarded to ZLS. Every property must have the format 'zig.zls.someOptionName'. You will **not** be warned about unused or ignored options.",
"default": {},
"additionalProperties": false,
"patternProperties": {
"^zig\\.zls\\.[a-z]+[A-Z0-9][a-z0-9]+[A-Za-z0-9]*$": {}
}
}
}
},
Expand Down
119 changes: 87 additions & 32 deletions src/zls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import vscode from "vscode";

import fs from "fs";

import { LanguageClient, LanguageClientOptions, ResponseError, ServerOptions } from "vscode-languageclient/node";
import {
CancellationToken,
ConfigurationParams,
LSPAny,
LanguageClient,
LanguageClientOptions,
RequestHandler,
ResponseError,
ServerOptions,
} from "vscode-languageclient/node";
import axios from "axios";
import camelCase from "camelcase";
import mkdirp from "mkdirp";
Expand Down Expand Up @@ -30,37 +39,7 @@ async function startClient() {
outputChannel,
middleware: {
workspace: {
async configuration(params, token, next) {
let indexOfZigPath: number | undefined;

params.items.forEach((param, index) => {
if (param.section) {
if (param.section === "zls.zig_exe_path") {
param.section = "zig.path";
indexOfZigPath = index;
} else {
param.section = `zig.zls.${camelCase(param.section.slice(4))}`;
}
}
});

const result = await next(params, token);
if (result instanceof ResponseError) {
return result;
}

if (indexOfZigPath !== undefined) {
try {
result[indexOfZigPath] = getZigPath();
} catch {
// ZLS will try to find Zig by itself and likely fail as well.
// This will cause two "Zig can't be found in $PATH" error messages to be reported.
result[indexOfZigPath] = null;
}
}

return result as unknown[];
},
configuration: configurationMiddleware,
},
},
};
Expand Down Expand Up @@ -97,6 +76,82 @@ export function getZLSPath(): string {
return getExePath(zlsPath, "zls", "zig.zls.path");
}

async function configurationMiddleware(
params: ConfigurationParams,
token: CancellationToken,
next: RequestHandler<ConfigurationParams, LSPAny[], void>,
): Promise<LSPAny[] | ResponseError> {
const optionIndices: Record<string, number | undefined> = {};

params.items.forEach((param, index) => {
if (param.section) {
if (param.section === "zls.zig_exe_path") {
param.section = "zig.path";
} else {
param.section = `zig.zls.${camelCase(param.section.slice(4))}`;
}
optionIndices[param.section] = index;
}
});

const result = await next(params, token);
if (result instanceof ResponseError) {
return result;
}

const indexOfZigPath = optionIndices["zig.path"];
if (indexOfZigPath !== undefined) {
try {
result[indexOfZigPath] = getZigPath();
} catch {
// ZLS will try to find Zig by itself and likely fail as well.
// This will cause two "Zig can't be found in $PATH" error messages to be reported.
result[indexOfZigPath] = null;
}
}

const configuration = vscode.workspace.getConfiguration("zig.zls");
const additionalOptions = configuration.get<Record<string, unknown>>("additionalOptions", {});

for (const optionName in additionalOptions) {
const section = optionName.slice("zig.zls.".length);

const doesOptionExist = configuration.inspect(section)?.defaultValue !== undefined;
if (doesOptionExist) {
// The extension has defined a config option with the given name but the user still used `additionalOptions`.
const response = await vscode.window.showWarningMessage(
`The config option 'zig.zls.additionalOptions' contains the already existing option '${optionName}'`,
`Use ${optionName} instead`,
"Show zig.zls.additionalOptions",
);
switch (response) {
case `Use ${optionName} instead`:
const { [optionName]: newValue, ...updatedAdditionalOptions } = additionalOptions;
await configuration.update("additionalOptions", updatedAdditionalOptions, true);
await configuration.update(section, newValue, true);
break;
case "Show zig.zls.additionalOptions":
await vscode.commands.executeCommand("workbench.action.openSettingsJson", {
revealSetting: { key: "zig.zls.additionalOptions" },
});
continue;
case undefined:
continue;
}
}

const optionIndex = optionIndices[optionName];
if (!optionIndex) {
// ZLS has not requested a config option with the given name.
continue;
}

result[optionIndex] = additionalOptions[optionName];
}

return result as unknown[];
}

const downloadsRoot = "https://zigtools-releases.nyc3.digitaloceanspaces.com/zls";

interface Version {
Expand Down

0 comments on commit ac8bff3

Please sign in to comment.