2022-05-11 12:34:43 +00:00
|
|
|
import path = require('path');
|
2018-10-07 20:59:02 +00:00
|
|
|
import * as vscode from 'vscode';
|
2020-12-30 09:17:25 +00:00
|
|
|
import { Env } from './client';
|
2020-03-17 11:44:31 +00:00
|
|
|
import { log } from "./util";
|
2019-01-18 10:59:08 +00:00
|
|
|
|
2020-03-17 11:44:31 +00:00
|
|
|
export type UpdatesChannel = "stable" | "nightly";
|
2020-03-09 17:57:13 +00:00
|
|
|
|
2021-05-23 10:57:04 +00:00
|
|
|
const NIGHTLY_TAG = "nightly";
|
2020-03-23 23:00:57 +00:00
|
|
|
|
2020-07-02 19:08:33 +00:00
|
|
|
export type RunnableEnvCfg = undefined | Record<string, string> | { mask?: string; env: Record<string, string> }[];
|
2020-07-02 16:47:40 +00:00
|
|
|
|
2018-10-07 20:59:02 +00:00
|
|
|
export class Config {
|
2020-03-09 17:57:13 +00:00
|
|
|
readonly extensionId = "matklad.rust-analyzer";
|
|
|
|
|
2020-05-25 00:47:33 +00:00
|
|
|
readonly rootSection = "rust-analyzer";
|
2020-03-09 17:57:13 +00:00
|
|
|
private readonly requiresReloadOpts = [
|
2020-03-14 00:00:34 +00:00
|
|
|
"serverPath",
|
2020-12-30 09:17:25 +00:00
|
|
|
"server",
|
2020-04-02 10:47:58 +00:00
|
|
|
"cargo",
|
2020-04-12 16:05:33 +00:00
|
|
|
"procMacro",
|
2020-04-02 10:47:58 +00:00
|
|
|
"files",
|
2020-06-03 11:15:54 +00:00
|
|
|
"lens", // works as lens.*
|
2020-02-13 20:48:20 +00:00
|
|
|
]
|
2020-03-09 17:57:13 +00:00
|
|
|
.map(opt => `${this.rootSection}.${opt}`);
|
2020-02-13 20:48:20 +00:00
|
|
|
|
2020-03-24 08:31:42 +00:00
|
|
|
readonly package: {
|
|
|
|
version: string;
|
2020-03-25 18:56:48 +00:00
|
|
|
releaseTag: string | null;
|
2020-03-24 08:31:42 +00:00
|
|
|
enableProposedApi: boolean | undefined;
|
|
|
|
} = vscode.extensions.getExtension(this.extensionId)!.packageJSON;
|
2020-02-16 01:08:36 +00:00
|
|
|
|
2021-05-23 20:33:32 +00:00
|
|
|
readonly globalStorageUri: vscode.Uri;
|
2020-02-13 20:48:20 +00:00
|
|
|
|
2020-03-23 23:00:57 +00:00
|
|
|
constructor(ctx: vscode.ExtensionContext) {
|
2021-05-23 20:33:32 +00:00
|
|
|
this.globalStorageUri = ctx.globalStorageUri;
|
2020-03-23 23:00:57 +00:00
|
|
|
vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
|
|
|
|
this.refreshLogging();
|
2020-02-13 21:05:32 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 23:00:57 +00:00
|
|
|
private refreshLogging() {
|
|
|
|
log.setEnabled(this.traceExtension);
|
2020-07-05 14:42:52 +00:00
|
|
|
log.info("Extension version:", this.package.version);
|
|
|
|
|
|
|
|
const cfg = Object.entries(this.cfg).filter(([_, val]) => !(val instanceof Function));
|
|
|
|
log.info("Using configuration", Object.fromEntries(cfg));
|
2020-02-13 20:48:20 +00:00
|
|
|
}
|
2020-02-08 02:22:44 +00:00
|
|
|
|
2020-03-23 23:00:57 +00:00
|
|
|
private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
|
|
|
|
this.refreshLogging();
|
2020-02-13 20:48:20 +00:00
|
|
|
|
2020-03-09 17:57:13 +00:00
|
|
|
const requiresReloadOpt = this.requiresReloadOpts.find(
|
2020-02-13 20:48:20 +00:00
|
|
|
opt => event.affectsConfiguration(opt)
|
|
|
|
);
|
2018-10-07 20:59:02 +00:00
|
|
|
|
2020-02-13 20:48:20 +00:00
|
|
|
if (!requiresReloadOpt) return;
|
2019-02-07 10:37:36 +00:00
|
|
|
|
2020-02-13 20:48:20 +00:00
|
|
|
const userResponse = await vscode.window.showInformationMessage(
|
|
|
|
`Changing "${requiresReloadOpt}" requires a reload`,
|
|
|
|
"Reload now"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (userResponse === "Reload now") {
|
2020-03-17 11:44:31 +00:00
|
|
|
await vscode.commands.executeCommand("workbench.action.reloadWindow");
|
2020-02-08 22:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 20:48:20 +00:00
|
|
|
// We don't do runtime config validation here for simplicity. More on stackoverflow:
|
|
|
|
// https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension
|
2020-02-08 22:27:04 +00:00
|
|
|
|
2020-03-23 23:00:57 +00:00
|
|
|
private get cfg(): vscode.WorkspaceConfiguration {
|
|
|
|
return vscode.workspace.getConfiguration(this.rootSection);
|
|
|
|
}
|
|
|
|
|
2020-04-11 12:23:07 +00:00
|
|
|
/**
|
|
|
|
* Beware that postfix `!` operator erases both `null` and `undefined`.
|
|
|
|
* This is why the following doesn't work as expected:
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* const nullableNum = vscode
|
|
|
|
* .workspace
|
|
|
|
* .getConfiguration
|
2022-04-29 11:34:03 +00:00
|
|
|
* .getConfiguration("rust-analyzer")
|
2020-04-11 12:23:07 +00:00
|
|
|
* .get<number | null>(path)!;
|
|
|
|
*
|
|
|
|
* // What happens is that type of `nullableNum` is `number` but not `null | number`:
|
|
|
|
* const fullFledgedNum: number = nullableNum;
|
|
|
|
* ```
|
|
|
|
* So this getter handles this quirk by not requiring the caller to use postfix `!`
|
|
|
|
*/
|
|
|
|
private get<T>(path: string): T {
|
|
|
|
return this.cfg.get<T>(path)!;
|
|
|
|
}
|
|
|
|
|
2021-01-04 15:39:15 +00:00
|
|
|
get serverPath() {
|
|
|
|
return this.get<null | string>("server.path") ?? this.get<null | string>("serverPath");
|
|
|
|
}
|
2020-12-30 09:17:25 +00:00
|
|
|
get serverExtraEnv() { return this.get<Env | null>("server.extraEnv") ?? {}; }
|
2020-04-11 12:23:07 +00:00
|
|
|
get traceExtension() { return this.get<boolean>("trace.extension"); }
|
|
|
|
|
2020-06-18 19:20:13 +00:00
|
|
|
get cargoRunner() {
|
|
|
|
return this.get<string | undefined>("cargoRunner");
|
|
|
|
}
|
|
|
|
|
2020-07-02 16:47:40 +00:00
|
|
|
get runnableEnv() {
|
2020-07-02 18:33:26 +00:00
|
|
|
return this.get<RunnableEnvCfg>("runnableEnv");
|
2020-07-02 16:47:40 +00:00
|
|
|
}
|
|
|
|
|
2020-04-29 10:10:42 +00:00
|
|
|
get debug() {
|
2021-04-22 13:09:46 +00:00
|
|
|
let sourceFileMap = this.get<Record<string, string> | "auto">("debug.sourceFileMap");
|
|
|
|
if (sourceFileMap !== "auto") {
|
|
|
|
// "/rustc/<id>" used by suggestions only.
|
|
|
|
const { ["/rustc/<id>"]: _, ...trimmed } = this.get<Record<string, string>>("debug.sourceFileMap");
|
|
|
|
sourceFileMap = trimmed;
|
|
|
|
}
|
2020-05-06 13:01:35 +00:00
|
|
|
|
2020-04-29 10:10:42 +00:00
|
|
|
return {
|
2020-04-29 11:13:57 +00:00
|
|
|
engine: this.get<string>("debug.engine"),
|
2020-05-07 14:07:58 +00:00
|
|
|
engineSettings: this.get<object>("debug.engineSettings"),
|
2020-06-10 13:15:28 +00:00
|
|
|
openDebugPane: this.get<boolean>("debug.openDebugPane"),
|
2020-05-14 08:12:10 +00:00
|
|
|
sourceFileMap: sourceFileMap
|
2020-04-29 10:10:42 +00:00
|
|
|
};
|
|
|
|
}
|
2020-05-17 16:51:44 +00:00
|
|
|
|
2020-06-03 11:15:54 +00:00
|
|
|
get hoverActions() {
|
|
|
|
return {
|
|
|
|
enable: this.get<boolean>("hoverActions.enable"),
|
2022-04-29 11:34:03 +00:00
|
|
|
implementations: this.get<boolean>("hoverActions.implementations.enable"),
|
|
|
|
references: this.get<boolean>("hoverActions.references.enable"),
|
|
|
|
run: this.get<boolean>("hoverActions.run.enable"),
|
|
|
|
debug: this.get<boolean>("hoverActions.debug.enable"),
|
|
|
|
gotoTypeDef: this.get<boolean>("hoverActions.gotoTypeDef.enable"),
|
2020-06-03 11:15:54 +00:00
|
|
|
};
|
|
|
|
}
|
2021-05-23 10:57:04 +00:00
|
|
|
|
|
|
|
get currentExtensionIsNightly() {
|
|
|
|
return this.package.releaseTag === NIGHTLY_TAG;
|
|
|
|
}
|
2018-10-07 20:59:02 +00:00
|
|
|
}
|
2022-04-29 11:34:03 +00:00
|
|
|
|
|
|
|
export async function updateConfig(config: vscode.WorkspaceConfiguration) {
|
|
|
|
const renames = [
|
|
|
|
["assist.allowMergingIntoGlobImports", "imports.merge.glob",],
|
|
|
|
["assist.exprFillDefault", "assist.expressionFillDefault",],
|
|
|
|
["assist.importEnforceGranularity", "imports.granularity.enforce",],
|
|
|
|
["assist.importGranularity", "imports.granularity.group",],
|
|
|
|
["assist.importMergeBehavior", "imports.granularity.group",],
|
|
|
|
["assist.importMergeBehaviour", "imports.granularity.group",],
|
|
|
|
["assist.importGroup", "imports.group.enable",],
|
|
|
|
["assist.importPrefix", "imports.prefix",],
|
2022-05-12 10:29:40 +00:00
|
|
|
["primeCaches.enable", "cachePriming.enable",],
|
|
|
|
["cache.warmup", "cachePriming.enable",],
|
2022-04-29 11:34:03 +00:00
|
|
|
["cargo.loadOutDirsFromCheck", "cargo.buildScripts.enable",],
|
2022-05-11 10:33:56 +00:00
|
|
|
["cargo.runBuildScripts", "cargo.buildScripts.enable",],
|
|
|
|
["cargo.runBuildScriptsCommand", "cargo.buildScripts.overrideCommand",],
|
|
|
|
["cargo.useRustcWrapperForBuildScripts", "cargo.buildScripts.useRustcWrapper",],
|
2022-04-29 11:34:03 +00:00
|
|
|
["completion.snippets", "completion.snippets.custom",],
|
|
|
|
["diagnostics.enableExperimental", "diagnostics.experimental.enable",],
|
|
|
|
["experimental.procAttrMacros", "procMacro.attributes.enable",],
|
|
|
|
["highlighting.strings", "semanticHighlighting.strings.enable",],
|
|
|
|
["highlightRelated.breakPoints", "highlightRelated.breakPoints.enable",],
|
|
|
|
["highlightRelated.exitPoints", "highlightRelated.exitPoints.enable",],
|
|
|
|
["highlightRelated.yieldPoints", "highlightRelated.yieldPoints.enable",],
|
|
|
|
["highlightRelated.references", "highlightRelated.references.enable",],
|
|
|
|
["hover.documentation", "hover.documentation.enable",],
|
|
|
|
["hover.linksInHover", "hover.links.enable",],
|
|
|
|
["hoverActions.linksInHover", "hover.links.enable",],
|
|
|
|
["hoverActions.debug", "hoverActions.debug.enable",],
|
|
|
|
["hoverActions.enable", "hoverActions.enable.enable",],
|
|
|
|
["hoverActions.gotoTypeDef", "hoverActions.gotoTypeDef.enable",],
|
|
|
|
["hoverActions.implementations", "hoverActions.implementations.enable",],
|
|
|
|
["hoverActions.references", "hoverActions.references.enable",],
|
|
|
|
["hoverActions.run", "hoverActions.run.enable",],
|
|
|
|
["inlayHints.chainingHints", "inlayHints.chainingHints.enable",],
|
|
|
|
["inlayHints.closureReturnTypeHints", "inlayHints.closureReturnTypeHints.enable",],
|
|
|
|
["inlayHints.hideNamedConstructorHints", "inlayHints.typeHints.hideNamedConstructorHints",],
|
|
|
|
["inlayHints.parameterHints", "inlayHints.parameterHints.enable",],
|
|
|
|
["inlayHints.reborrowHints", "inlayHints.reborrowHints.enable",],
|
|
|
|
["inlayHints.typeHints", "inlayHints.typeHints.enable",],
|
|
|
|
["lruCapacity", "lru.capacity",],
|
|
|
|
["runnables.cargoExtraArgs", "runnables.extraArgs",],
|
|
|
|
["runnables.overrideCargo", "runnables.command",],
|
|
|
|
["rustcSource", "rustc.source",],
|
|
|
|
["rustfmt.enableRangeFormatting", "rustfmt.rangeFormatting.enable"]
|
|
|
|
];
|
|
|
|
|
|
|
|
for (const [oldKey, newKey] of renames) {
|
|
|
|
const inspect = config.inspect(oldKey);
|
|
|
|
if (inspect !== undefined) {
|
|
|
|
const valMatrix = [
|
|
|
|
{ val: inspect.globalValue, langVal: inspect.globalLanguageValue, target: vscode.ConfigurationTarget.Global },
|
|
|
|
{ val: inspect.workspaceFolderValue, langVal: inspect.workspaceFolderLanguageValue, target: vscode.ConfigurationTarget.WorkspaceFolder },
|
|
|
|
{ val: inspect.workspaceValue, langVal: inspect.workspaceLanguageValue, target: vscode.ConfigurationTarget.Workspace }
|
|
|
|
];
|
|
|
|
for (const { val, langVal, target } of valMatrix) {
|
|
|
|
const pred = (val: unknown) => {
|
|
|
|
// some of the updates we do only append "enable" or "custom"
|
|
|
|
// that means on the next run we would find these again, but as objects with
|
|
|
|
// these properties causing us to destroy the config
|
|
|
|
// so filter those already updated ones out
|
|
|
|
return val !== undefined && !(typeof val === "object" && val !== null && (val.hasOwnProperty("enable") || val.hasOwnProperty("custom")));
|
|
|
|
};
|
|
|
|
if (pred(val)) {
|
|
|
|
await config.update(newKey, val, target, false);
|
|
|
|
await config.update(oldKey, undefined, target, false);
|
|
|
|
}
|
|
|
|
if (pred(langVal)) {
|
|
|
|
await config.update(newKey, langVal, target, true);
|
|
|
|
await config.update(oldKey, undefined, target, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-11 12:22:58 +00:00
|
|
|
|
|
|
|
export function substituteVariablesInEnv(env: Env): Env {
|
|
|
|
const missingDeps = new Set<string>();
|
|
|
|
// vscode uses `env:ENV_NAME` for env vars resolution, and it's easier
|
|
|
|
// to follow the same convention for our dependency tracking
|
|
|
|
const definedEnvKeys = new Set(Object.keys(env).map(key => `env:${key}`));
|
|
|
|
const envWithDeps = Object.fromEntries(Object.entries(env).map(([key, value]) => {
|
|
|
|
const deps = new Set<string>();
|
|
|
|
const depRe = new RegExp(/\${(?<depName>.+?)}/g);
|
|
|
|
let match = undefined;
|
|
|
|
while ((match = depRe.exec(value))) {
|
|
|
|
const depName = match.groups!.depName;
|
|
|
|
deps.add(depName);
|
|
|
|
// `depName` at this point can have a form of `expression` or
|
|
|
|
// `prefix:expression`
|
|
|
|
if (!definedEnvKeys.has(depName)) {
|
|
|
|
missingDeps.add(depName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [`env:${key}`, { deps: [...deps], value }];
|
|
|
|
}));
|
|
|
|
|
|
|
|
const resolved = new Set<string>();
|
2022-05-11 12:28:08 +00:00
|
|
|
for (const dep of missingDeps) {
|
|
|
|
const match = /(?<prefix>.*?):(?<body>.+)/.exec(dep);
|
|
|
|
if (match) {
|
|
|
|
const { prefix, body } = match.groups!;
|
|
|
|
if (prefix === 'env') {
|
|
|
|
const envName = body;
|
|
|
|
envWithDeps[dep] = {
|
|
|
|
value: process.env[envName] ?? '',
|
|
|
|
deps: []
|
|
|
|
};
|
|
|
|
resolved.add(dep);
|
|
|
|
} else {
|
|
|
|
// we can't handle other prefixes at the moment
|
|
|
|
// leave values as is, but still mark them as resolved
|
|
|
|
envWithDeps[dep] = {
|
|
|
|
value: '${' + dep + '}',
|
|
|
|
deps: []
|
|
|
|
};
|
|
|
|
resolved.add(dep);
|
|
|
|
}
|
|
|
|
} else {
|
2022-05-11 12:34:43 +00:00
|
|
|
envWithDeps[dep] = {
|
|
|
|
value: computeVscodeVar(dep),
|
|
|
|
deps: []
|
|
|
|
};
|
2022-05-11 12:28:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-11 12:22:58 +00:00
|
|
|
const toResolve = new Set(Object.keys(envWithDeps));
|
|
|
|
|
|
|
|
let leftToResolveSize;
|
|
|
|
do {
|
|
|
|
leftToResolveSize = toResolve.size;
|
|
|
|
for (const key of toResolve) {
|
|
|
|
if (envWithDeps[key].deps.every(dep => resolved.has(dep))) {
|
|
|
|
envWithDeps[key].value = envWithDeps[key].value.replace(
|
|
|
|
/\${(?<depName>.+?)}/g, (_wholeMatch, depName) => {
|
|
|
|
return envWithDeps[depName].value;
|
|
|
|
});
|
|
|
|
resolved.add(key);
|
|
|
|
toResolve.delete(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (toResolve.size > 0 && toResolve.size < leftToResolveSize);
|
|
|
|
|
|
|
|
const resolvedEnv: Env = {};
|
|
|
|
for (const key of Object.keys(env)) {
|
|
|
|
resolvedEnv[key] = envWithDeps[`env:${key}`].value;
|
|
|
|
}
|
|
|
|
return resolvedEnv;
|
|
|
|
}
|
2022-05-11 12:34:43 +00:00
|
|
|
|
|
|
|
function computeVscodeVar(varName: string): string {
|
|
|
|
// https://code.visualstudio.com/docs/editor/variables-reference
|
|
|
|
const supportedVariables: { [k: string]: () => string } = {
|
|
|
|
workspaceFolder: () => {
|
|
|
|
const folders = vscode.workspace.workspaceFolders ?? [];
|
|
|
|
if (folders.length === 1) {
|
|
|
|
// TODO: support for remote workspaces?
|
|
|
|
return folders[0].uri.fsPath;
|
|
|
|
} else if (folders.length > 1) {
|
|
|
|
// could use currently opened document to detect the correct
|
|
|
|
// workspace. However, that would be determined by the document
|
|
|
|
// user has opened on Editor startup. Could lead to
|
|
|
|
// unpredictable workspace selection in practice.
|
|
|
|
// It's better to pick the first one
|
|
|
|
return folders[0].uri.fsPath;
|
|
|
|
} else {
|
|
|
|
// no workspace opened
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
workspaceFolderBasename: () => {
|
|
|
|
const workspaceFolder = computeVscodeVar('workspaceFolder');
|
|
|
|
if (workspaceFolder) {
|
|
|
|
return path.basename(workspaceFolder);
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
cwd: () => process.cwd(),
|
|
|
|
|
|
|
|
// see
|
|
|
|
// https://github.com/microsoft/vscode/blob/08ac1bb67ca2459496b272d8f4a908757f24f56f/src/vs/workbench/api/common/extHostVariableResolverService.ts#L81
|
|
|
|
// or
|
|
|
|
// https://github.com/microsoft/vscode/blob/29eb316bb9f154b7870eb5204ec7f2e7cf649bec/src/vs/server/node/remoteTerminalChannel.ts#L56
|
|
|
|
execPath: () => process.env.VSCODE_EXEC_PATH ?? process.execPath,
|
|
|
|
|
|
|
|
pathSeparator: () => path.sep
|
|
|
|
};
|
|
|
|
|
|
|
|
if (varName in supportedVariables) {
|
|
|
|
return supportedVariables[varName]();
|
|
|
|
} else {
|
|
|
|
// can't resolve, keep the expression as is
|
|
|
|
return '${' + varName + '}';
|
|
|
|
}
|
|
|
|
}
|