rust-analyzer/editors/code/src/config.ts

169 lines
5.7 KiB
TypeScript
Raw Normal View History

2018-10-07 20:59:02 +00:00
import * as vscode from 'vscode';
import { Env } from './client';
import { log } from "./util";
export type UpdatesChannel = "stable" | "nightly";
2020-03-09 17:57:13 +00:00
export 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";
readonly rootSection = "rust-analyzer";
2020-03-09 17:57:13 +00:00
private readonly requiresReloadOpts = [
"serverPath",
"server",
"cargo",
2020-04-12 16:05:33 +00:00
"procMacro",
"files",
2020-03-19 21:56:32 +00:00
"highlighting",
"updates.channel",
2020-06-03 11:15:54 +00:00
"lens", // works as lens.*
"hoverActions", // works as hoverActions.*
]
2020-03-09 17:57:13 +00:00
.map(opt => `${this.rootSection}.${opt}`);
readonly package: {
version: string;
releaseTag: string | null;
enableProposedApi: boolean | undefined;
} = vscode.extensions.getExtension(this.extensionId)!.packageJSON;
2020-03-23 23:00:57 +00:00
readonly globalStoragePath: string;
2020-03-23 23:00:57 +00:00
constructor(ctx: vscode.ExtensionContext) {
this.globalStoragePath = ctx.globalStoragePath;
vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
this.refreshLogging();
}
2020-03-23 23:00:57 +00:00
private refreshLogging() {
log.setEnabled(this.traceExtension);
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-03-23 23:00:57 +00:00
private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
this.refreshLogging();
2020-03-09 17:57:13 +00:00
const requiresReloadOpt = this.requiresReloadOpts.find(
opt => event.affectsConfiguration(opt)
);
2018-10-07 20:59:02 +00:00
if (!requiresReloadOpt) return;
const userResponse = await vscode.window.showInformationMessage(
`Changing "${requiresReloadOpt}" requires a reload`,
"Reload now"
);
if (userResponse === "Reload now") {
await vscode.commands.executeCommand("workbench.action.reloadWindow");
}
}
// 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-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
* .getConfiguration("rust-analyer")
* .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");
}
get serverExtraEnv() { return this.get<Env | null>("server.extraEnv") ?? {}; }
2020-04-11 12:23:07 +00:00
get channel() { return this.get<UpdatesChannel>("updates.channel"); }
get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
get traceExtension() { return this.get<boolean>("trace.extension"); }
get httpProxy() {
const httpProxy = vscode
.workspace
.getConfiguration('http')
.get<null | string>("proxy")!;
return httpProxy || process.env["https_proxy"] || process.env["HTTPS_PROXY"];
}
2020-04-11 12:23:07 +00:00
2020-03-23 23:00:57 +00:00
get inlayHints() {
return {
enable: this.get<boolean>("inlayHints.enable"),
2020-04-11 12:23:07 +00:00
typeHints: this.get<boolean>("inlayHints.typeHints"),
parameterHints: this.get<boolean>("inlayHints.parameterHints"),
chainingHints: this.get<boolean>("inlayHints.chainingHints"),
maxLength: this.get<null | number>("inlayHints.maxLength"),
};
}
2020-02-17 22:42:25 +00:00
get checkOnSave() {
return {
2020-04-11 12:23:07 +00:00
command: this.get<string>("checkOnSave.command"),
};
2018-10-07 20:59:02 +00:00
}
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
}
get debug() {
// "/rustc/<id>" used by suggestions only.
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
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"),
openDebugPane: this.get<boolean>("debug.openDebugPane"),
sourceFileMap: sourceFileMap
};
}
2020-05-17 16:51:44 +00:00
get lens() {
return {
2020-05-18 07:27:00 +00:00
enable: this.get<boolean>("lens.enable"),
2020-05-17 16:51:44 +00:00
run: this.get<boolean>("lens.run"),
debug: this.get<boolean>("lens.debug"),
implementations: this.get<boolean>("lens.implementations"),
2020-09-01 13:33:02 +00:00
methodReferences: this.get<boolean>("lens.methodReferences"),
references: this.get<boolean>("lens.references"),
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"),
implementations: this.get<boolean>("hoverActions.implementations"),
run: this.get<boolean>("hoverActions.run"),
debug: this.get<boolean>("hoverActions.debug"),
gotoTypeDef: this.get<boolean>("hoverActions.gotoTypeDef"),
2020-06-03 11:15:54 +00:00
};
}
2018-10-07 20:59:02 +00:00
}