1 import * as vscode from 'vscode';
2 import { Env } from './client';
3 import { log } from "./util";
5 export type UpdatesChannel = "stable" | "nightly";
7 export const NIGHTLY_TAG = "nightly";
9 export type RunnableEnvCfg = undefined | Record<string, string> | { mask?: string; env: Record<string, string> }[];
12 readonly extensionId = "matklad.rust-analyzer";
14 readonly rootSection = "rust-analyzer";
15 private readonly requiresReloadOpts = [
23 "lens", // works as lens.*
24 "hoverActions", // works as hoverActions.*
26 .map(opt => `${this.rootSection}.${opt}`);
30 releaseTag: string | null;
31 enableProposedApi: boolean | undefined;
32 } = vscode.extensions.getExtension(this.extensionId)!.packageJSON;
34 readonly globalStoragePath: string;
36 constructor(ctx: vscode.ExtensionContext) {
37 this.globalStoragePath = ctx.globalStoragePath;
38 vscode.workspace.onDidChangeConfiguration(this.onDidChangeConfiguration, this, ctx.subscriptions);
39 this.refreshLogging();
42 private refreshLogging() {
43 log.setEnabled(this.traceExtension);
44 log.info("Extension version:", this.package.version);
46 const cfg = Object.entries(this.cfg).filter(([_, val]) => !(val instanceof Function));
47 log.info("Using configuration", Object.fromEntries(cfg));
50 private async onDidChangeConfiguration(event: vscode.ConfigurationChangeEvent) {
51 this.refreshLogging();
53 const requiresReloadOpt = this.requiresReloadOpts.find(
54 opt => event.affectsConfiguration(opt)
57 if (!requiresReloadOpt) return;
59 const userResponse = await vscode.window.showInformationMessage(
60 `Changing "${requiresReloadOpt}" requires a reload`,
64 if (userResponse === "Reload now") {
65 await vscode.commands.executeCommand("workbench.action.reloadWindow");
69 // We don't do runtime config validation here for simplicity. More on stackoverflow:
70 // https://stackoverflow.com/questions/60135780/what-is-the-best-way-to-type-check-the-configuration-for-vscode-extension
72 private get cfg(): vscode.WorkspaceConfiguration {
73 return vscode.workspace.getConfiguration(this.rootSection);
77 * Beware that postfix `!` operator erases both `null` and `undefined`.
78 * This is why the following doesn't work as expected:
81 * const nullableNum = vscode
84 * .getConfiguration("rust-analyer")
85 * .get<number | null>(path)!;
87 * // What happens is that type of `nullableNum` is `number` but not `null | number`:
88 * const fullFledgedNum: number = nullableNum;
90 * So this getter handles this quirk by not requiring the caller to use postfix `!`
92 private get<T>(path: string): T {
93 return this.cfg.get<T>(path)!;
97 return this.get<null | string>("server.path") ?? this.get<null | string>("serverPath");
99 get serverExtraEnv() { return this.get<Env | null>("server.extraEnv") ?? {}; }
100 get channel() { return this.get<UpdatesChannel>("updates.channel"); }
101 get askBeforeDownload() { return this.get<boolean>("updates.askBeforeDownload"); }
102 get traceExtension() { return this.get<boolean>("trace.extension"); }
104 const httpProxy = vscode
106 .getConfiguration('http')
107 .get<null | string>("proxy")!;
109 return httpProxy || process.env["https_proxy"] || process.env["HTTPS_PROXY"];
114 enable: this.get<boolean>("inlayHints.enable"),
115 typeHints: this.get<boolean>("inlayHints.typeHints"),
116 parameterHints: this.get<boolean>("inlayHints.parameterHints"),
117 chainingHints: this.get<boolean>("inlayHints.chainingHints"),
118 smallerHints: this.get<boolean>("inlayHints.smallerHints"),
119 maxLength: this.get<null | number>("inlayHints.maxLength"),
125 command: this.get<string>("checkOnSave.command"),
130 return this.get<string | undefined>("cargoRunner");
134 return this.get<RunnableEnvCfg>("runnableEnv");
138 // "/rustc/<id>" used by suggestions only.
139 const { ["/rustc/<id>"]: _, ...sourceFileMap } = this.get<Record<string, string>>("debug.sourceFileMap");
142 engine: this.get<string>("debug.engine"),
143 engineSettings: this.get<object>("debug.engineSettings"),
144 openDebugPane: this.get<boolean>("debug.openDebugPane"),
145 sourceFileMap: sourceFileMap
151 enable: this.get<boolean>("lens.enable"),
152 run: this.get<boolean>("lens.run"),
153 debug: this.get<boolean>("lens.debug"),
154 implementations: this.get<boolean>("lens.implementations"),
155 methodReferences: this.get<boolean>("lens.methodReferences"),
156 references: this.get<boolean>("lens.references"),
162 enable: this.get<boolean>("hoverActions.enable"),
163 implementations: this.get<boolean>("hoverActions.implementations"),
164 run: this.get<boolean>("hoverActions.run"),
165 debug: this.get<boolean>("hoverActions.debug"),
166 gotoTypeDef: this.get<boolean>("hoverActions.gotoTypeDef"),