2018-10-07 15:59:02 -05:00
|
|
|
import * as vscode from 'vscode';
|
2020-03-17 06:44:31 -05:00
|
|
|
import { log } from "./util";
|
2019-01-18 04:59:08 -06:00
|
|
|
|
2020-03-17 06:44:31 -05:00
|
|
|
export type UpdatesChannel = "stable" | "nightly";
|
2020-03-09 12:57:13 -05:00
|
|
|
|
|
|
|
export const NIGHTLY_TAG = "nightly";
|
2020-03-23 18:00:57 -05:00
|
|
|
|
2020-07-02 11:47:40 -05:00
|
|
|
export type RunnableEnvCfg = Record<string, string> | [{ mask?: string, env: Record<string, string>; }]
|
|
|
|
|
2018-10-07 15:59:02 -05:00
|
|
|
export class Config {
|
2020-03-09 12:57:13 -05:00
|
|
|
readonly extensionId = "matklad.rust-analyzer";
|
|
|
|
|
2020-05-24 19:47:33 -05:00
|
|
|
readonly rootSection = "rust-analyzer";
|
2020-03-09 12:57:13 -05:00
|
|
|
private readonly requiresReloadOpts = [
|
2020-03-13 19:00:34 -05:00
|
|
|
"serverPath",
|
2020-04-02 05:47:58 -05:00
|
|
|
"cargo",
|
2020-04-12 11:05:33 -05:00
|
|
|
"procMacro",
|
2020-04-02 05:47:58 -05:00
|
|
|
"files",
|
2020-03-19 16:56:32 -05:00
|
|
|
"highlighting",
|
2020-03-17 06:44:31 -05:00
|
|
|
"updates.channel",
|
2020-06-03 06:15:54 -05:00
|
|
|
"lens", // works as lens.*
|
|
|
|
"hoverActions", // works as hoverActions.*
|
2020-02-13 14:48:20 -06:00
|
|
|
]
|
2020-03-09 12:57:13 -05:00
|
|
|
.map(opt => `${this.rootSection}.${opt}`);
|
2020-02-13 14:48:20 -06:00
|
|
|
|
2020-03-24 03:31:42 -05:00
|
|
|
readonly package: {
|
|
|
|
version: string;
|
2020-03-25 13:56:48 -05:00
|
|
|
releaseTag: string | null;
|
2020-03-24 03:31:42 -05:00
|
|
|
enableProposedApi: boolean | undefined;
|
|
|
|
} = vscode.extensions.getExtension(this.extensionId)!.packageJSON;
|
2020-02-15 19:08:36 -06:00
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
readonly globalStoragePath: string;
|
2020-02-13 14:48:20 -06:00
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
constructor(ctx: vscode.ExtensionContext) {
|
|
|
|
this.globalStoragePath = ctx.globalStoragePath;
|
|
|
|
vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
|
|
|
|
this.refreshLogging();
|
2020-02-13 15:05:32 -06:00
|
|
|
}
|
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
private refreshLogging() {
|
|
|
|
log.setEnabled(this.traceExtension);
|
2020-03-13 20:01:14 -05:00
|
|
|
log.debug(
|
2020-03-24 03:31:42 -05:00
|
|
|
"Extension version:", this.package.version,
|
2020-03-13 20:01:14 -05:00
|
|
|
"using configuration:", this.cfg
|
|
|
|
);
|
2020-02-13 14:48:20 -06:00
|
|
|
}
|
2020-02-07 20:22:44 -06:00
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
|
|
|
|
this.refreshLogging();
|
2020-02-13 14:48:20 -06:00
|
|
|
|
2020-03-09 12:57:13 -05:00
|
|
|
const requiresReloadOpt = this.requiresReloadOpts.find(
|
2020-02-13 14:48:20 -06:00
|
|
|
opt => event.affectsConfiguration(opt)
|
|
|
|
);
|
2018-10-07 15:59:02 -05:00
|
|
|
|
2020-02-13 14:48:20 -06:00
|
|
|
if (!requiresReloadOpt) return;
|
2019-02-07 04:37:36 -06:00
|
|
|
|
2020-02-13 14:48:20 -06:00
|
|
|
const userResponse = await vscode.window.showInformationMessage(
|
|
|
|
`Changing "${requiresReloadOpt}" requires a reload`,
|
|
|
|
"Reload now"
|
|
|
|
);
|
|
|
|
|
|
|
|
if (userResponse === "Reload now") {
|
2020-03-17 06:44:31 -05:00
|
|
|
await vscode.commands.executeCommand("workbench.action.reloadWindow");
|
2020-02-08 16:27:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:48:20 -06: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 16:27:04 -06:00
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
private get cfg(): vscode.WorkspaceConfiguration {
|
|
|
|
return vscode.workspace.getConfiguration(this.rootSection);
|
|
|
|
}
|
|
|
|
|
2020-04-11 07:23:07 -05: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)!;
|
|
|
|
}
|
|
|
|
|
|
|
|
get serverPath() { return this.get<null | string>("serverPath"); }
|
|
|
|
get channel() { return this.get<UpdatesChannel>("updates.channel"); }
|
|
|
|
get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
|
|
|
|
get traceExtension() { return this.get<boolean>("trace.extension"); }
|
|
|
|
|
2020-03-23 18:00:57 -05:00
|
|
|
get inlayHints() {
|
2020-03-10 02:55:46 -05:00
|
|
|
return {
|
2020-05-08 02:28:15 -05:00
|
|
|
enable: this.get<boolean>("inlayHints.enable"),
|
2020-04-11 07:23:07 -05: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-03-10 02:55:46 -05:00
|
|
|
};
|
|
|
|
}
|
2020-02-17 16:42:25 -06:00
|
|
|
|
2020-04-02 05:47:58 -05:00
|
|
|
get checkOnSave() {
|
2020-02-13 14:48:20 -06:00
|
|
|
return {
|
2020-04-11 07:23:07 -05:00
|
|
|
command: this.get<string>("checkOnSave.command"),
|
2020-02-13 14:48:20 -06:00
|
|
|
};
|
2018-10-07 15:59:02 -05:00
|
|
|
}
|
2020-04-29 05:10:42 -05:00
|
|
|
|
2020-06-18 14:20:13 -05:00
|
|
|
get cargoRunner() {
|
|
|
|
return this.get<string | undefined>("cargoRunner");
|
|
|
|
}
|
|
|
|
|
2020-07-02 11:47:40 -05:00
|
|
|
get runnableEnv() {
|
|
|
|
return this.get<RunnableEnvCfg | undefined>("runnableEnv");
|
|
|
|
}
|
|
|
|
|
2020-04-29 05:10:42 -05:00
|
|
|
get debug() {
|
2020-05-06 08:01:35 -05:00
|
|
|
// "/rustc/<id>" used by suggestions only.
|
|
|
|
const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
|
|
|
|
|
2020-04-29 05:10:42 -05:00
|
|
|
return {
|
2020-04-29 06:13:57 -05:00
|
|
|
engine: this.get<string>("debug.engine"),
|
2020-05-07 09:07:58 -05:00
|
|
|
engineSettings: this.get<object>("debug.engineSettings"),
|
2020-06-10 08:15:28 -05:00
|
|
|
openDebugPane: this.get<boolean>("debug.openDebugPane"),
|
2020-05-14 03:12:10 -05:00
|
|
|
sourceFileMap: sourceFileMap
|
2020-04-29 05:10:42 -05:00
|
|
|
};
|
|
|
|
}
|
2020-05-17 11:51:44 -05:00
|
|
|
|
|
|
|
get lens() {
|
|
|
|
return {
|
2020-05-18 02:27:00 -05:00
|
|
|
enable: this.get<boolean>("lens.enable"),
|
2020-05-17 11:51:44 -05:00
|
|
|
run: this.get<boolean>("lens.run"),
|
|
|
|
debug: this.get<boolean>("lens.debug"),
|
|
|
|
implementations: this.get<boolean>("lens.implementations"),
|
|
|
|
};
|
|
|
|
}
|
2020-06-03 06:15:54 -05:00
|
|
|
|
|
|
|
get hoverActions() {
|
|
|
|
return {
|
|
|
|
enable: this.get<boolean>("hoverActions.enable"),
|
|
|
|
implementations: this.get<boolean>("hoverActions.implementations"),
|
2020-06-10 15:01:19 -05:00
|
|
|
run: this.get<boolean>("hoverActions.run"),
|
|
|
|
debug: this.get<boolean>("hoverActions.debug"),
|
|
|
|
gotoTypeDef: this.get<boolean>("hoverActions.gotoTypeDef"),
|
2020-06-03 06:15:54 -05:00
|
|
|
};
|
|
|
|
}
|
2018-10-07 15:59:02 -05:00
|
|
|
}
|