1 import * as vscode from 'vscode';
2 import { log } from "./util";
4 export type UpdatesChannel = "stable" | "nightly";
6 export const NIGHTLY_TAG = "nightly";
9 readonly extensionId = "matklad.rust-analyzer";
11 private readonly rootSection = "rust-analyzer";
12 private readonly requiresReloadOpts = [
20 .map(opt => `${this.rootSection}.${opt}`);
24 releaseTag: string | null;
25 enableProposedApi: boolean | undefined;
26 } = vscode.extensions.getExtension(this.extensionId)!.packageJSON;
28 readonly globalStoragePath: string;
30 constructor(ctx: vscode.ExtensionContext) {
31 this.globalStoragePath = ctx.globalStoragePath;
32 vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
33 this.refreshLogging();
36 private refreshLogging() {
37 log.setEnabled(this.traceExtension);
39 "Extension version:", this.package.version,
40 "using configuration:", this.cfg
44 private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
45 this.refreshLogging();
47 const requiresReloadOpt = this.requiresReloadOpts.find(
48 opt => event.affectsConfiguration(opt)
51 if (!requiresReloadOpt) return;
53 const userResponse = await vscode.window.showInformationMessage(
54 `Changing "${requiresReloadOpt}" requires a reload`,
58 if (userResponse === "Reload now") {
59 await vscode.commands.executeCommand("workbench.action.reloadWindow");
63 // We don't do runtime config validation here for simplicity. More on stackoverflow:
64 // https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension
66 private get cfg(): vscode.WorkspaceConfiguration {
67 return vscode.workspace.getConfiguration(this.rootSection);
71 * Beware that postfix `!` operator erases both `null` and `undefined`.
72 * This is why the following doesn't work as expected:
75 * const nullableNum = vscode
78 * .getConfiguration("rust-analyer")
79 * .get<number | null>(path)!;
81 * // What happens is that type of `nullableNum` is `number` but not `null | number`:
82 * const fullFledgedNum: number = nullableNum;
84 * So this getter handles this quirk by not requiring the caller to use postfix `!`
86 private get<T>(path: string): T {
87 return this.cfg.get<T>(path)!;
90 get serverPath() { return this.get<null | string>("serverPath"); }
91 get channel() { return this.get<UpdatesChannel>("updates.channel"); }
92 get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
93 get traceExtension() { return this.get<boolean>("trace.extension"); }
97 enable: this.get<boolean>("inlayHints.enable"),
98 typeHints: this.get<boolean>("inlayHints.typeHints"),
99 parameterHints: this.get<boolean>("inlayHints.parameterHints"),
100 chainingHints: this.get<boolean>("inlayHints.chainingHints"),
101 maxLength: this.get<null | number>("inlayHints.maxLength"),
107 command: this.get<string>("checkOnSave.command"),
112 // "/rustc/<id>" used by suggestions only.
113 const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
116 engine: this.get<string>("debug.engine"),
117 engineSettings: this.get<object>("debug.engineSettings"),
118 openUpDebugPane: this.get<boolean>("debug.openUpDebugPane"),
119 sourceFileMap: sourceFileMap,
120 useLaunchJson: this.get<object>("debug.useLaunchJson"),