2018-10-07 22:59:02 +02:00
|
|
|
import * as vscode from 'vscode';
|
2020-03-17 12:44:31 +01:00
|
|
|
import { log } from "./util";
|
2019-01-18 12:59:08 +02:00
|
|
|
|
2020-03-17 12:44:31 +01:00
|
|
|
export type UpdatesChannel = "stable" | "nightly";
|
2020-03-09 19:57:13 +02:00
|
|
|
|
|
|
|
|
export const NIGHTLY_TAG = "nightly";
|
2020-03-24 01:00:57 +02:00
|
|
|
|
2018-10-07 22:59:02 +02:00
|
|
|
export class Config {
|
2020-03-09 19:57:13 +02:00
|
|
|
readonly extensionId = "matklad.rust-analyzer";
|
|
|
|
|
|
|
|
|
|
private readonly rootSection = "rust-analyzer";
|
|
|
|
|
private readonly requiresReloadOpts = [
|
2020-03-14 02:00:34 +02:00
|
|
|
"serverPath",
|
2020-04-02 12:47:58 +02:00
|
|
|
"cargo",
|
|
|
|
|
"files",
|
2020-03-19 23:56:32 +02:00
|
|
|
"highlighting",
|
2020-03-17 12:44:31 +01:00
|
|
|
"updates.channel",
|
2020-02-13 22:48:20 +02:00
|
|
|
]
|
2020-03-09 19:57:13 +02:00
|
|
|
.map(opt => `${this.rootSection}.${opt}`);
|
2020-02-13 22:48:20 +02:00
|
|
|
|
2020-03-24 09:31:42 +01:00
|
|
|
readonly package: {
|
|
|
|
|
version: string;
|
2020-03-25 20:56:48 +02:00
|
|
|
releaseTag: string | null;
|
2020-03-24 09:31:42 +01:00
|
|
|
enableProposedApi: boolean | undefined;
|
|
|
|
|
} = vscode.extensions.getExtension(this.extensionId)!.packageJSON;
|
2020-02-16 03:08:36 +02:00
|
|
|
|
2020-03-24 01:00:57 +02:00
|
|
|
readonly globalStoragePath: string;
|
2020-02-13 22:48:20 +02:00
|
|
|
|
2020-03-24 01:00:57 +02:00
|
|
|
constructor(ctx: vscode.ExtensionContext) {
|
|
|
|
|
this.globalStoragePath = ctx.globalStoragePath;
|
|
|
|
|
vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
|
|
|
|
|
this.refreshLogging();
|
2020-02-13 23:05:32 +02:00
|
|
|
}
|
|
|
|
|
|
2020-03-24 01:00:57 +02:00
|
|
|
private refreshLogging() {
|
|
|
|
|
log.setEnabled(this.traceExtension);
|
2020-03-14 03:01:14 +02:00
|
|
|
log.debug(
|
2020-03-24 09:31:42 +01:00
|
|
|
"Extension version:", this.package.version,
|
2020-03-14 03:01:14 +02:00
|
|
|
"using configuration:", this.cfg
|
|
|
|
|
);
|
2020-02-13 22:48:20 +02:00
|
|
|
}
|
2020-02-08 04:22:44 +02:00
|
|
|
|
2020-03-24 01:00:57 +02:00
|
|
|
private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
|
|
|
|
|
this.refreshLogging();
|
2020-02-13 22:48:20 +02:00
|
|
|
|
2020-03-09 19:57:13 +02:00
|
|
|
const requiresReloadOpt = this.requiresReloadOpts.find(
|
2020-02-13 22:48:20 +02:00
|
|
|
opt => event.affectsConfiguration(opt)
|
|
|
|
|
);
|
2018-10-07 22:59:02 +02:00
|
|
|
|
2020-02-13 22:48:20 +02:00
|
|
|
if (!requiresReloadOpt) return;
|
2019-02-07 12:37:36 +02:00
|
|
|
|
2020-02-13 22:48:20 +02:00
|
|
|
const userResponse = await vscode.window.showInformationMessage(
|
|
|
|
|
`Changing "${requiresReloadOpt}" requires a reload`,
|
|
|
|
|
"Reload now"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (userResponse === "Reload now") {
|
2020-03-17 12:44:31 +01:00
|
|
|
await vscode.commands.executeCommand("workbench.action.reloadWindow");
|
2020-02-09 00:27:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-13 22:48:20 +02: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-09 00:27:04 +02:00
|
|
|
|
2020-03-24 01:00:57 +02:00
|
|
|
private get cfg(): vscode.WorkspaceConfiguration {
|
|
|
|
|
return vscode.workspace.getConfiguration(this.rootSection);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get serverPath() { return this.cfg.get<null | string>("serverPath")!; }
|
|
|
|
|
get channel() { return this.cfg.get<UpdatesChannel>("updates.channel")!; }
|
|
|
|
|
get askBeforeDownload() { return this.cfg.get<boolean>("updates.askBeforeDownload")!; }
|
|
|
|
|
get traceExtension() { return this.cfg.get<boolean>("trace.extension")!; }
|
|
|
|
|
|
|
|
|
|
get inlayHints() {
|
2020-03-10 00:55:46 -07:00
|
|
|
return {
|
2020-03-24 01:00:57 +02:00
|
|
|
typeHints: this.cfg.get<boolean>("inlayHints.typeHints")!,
|
|
|
|
|
parameterHints: this.cfg.get<boolean>("inlayHints.parameterHints")!,
|
2020-03-23 23:32:50 +01:00
|
|
|
chainingHints: this.cfg.get<boolean>("inlayHints.chainingHints")!,
|
2020-03-24 01:00:57 +02:00
|
|
|
maxLength: this.cfg.get<null | number>("inlayHints.maxLength")!,
|
2020-03-10 00:55:46 -07:00
|
|
|
};
|
|
|
|
|
}
|
2020-02-17 23:42:25 +01:00
|
|
|
|
2020-04-02 12:47:58 +02:00
|
|
|
get checkOnSave() {
|
2020-02-13 22:48:20 +02:00
|
|
|
return {
|
2020-04-02 12:47:58 +02:00
|
|
|
command: this.cfg.get<string>("checkOnSave.command")!,
|
2020-02-13 22:48:20 +02:00
|
|
|
};
|
2018-10-07 22:59:02 +02:00
|
|
|
}
|
|
|
|
|
}
|