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

114 lines
4.4 KiB
TypeScript
Raw Normal View History

2018-10-07 20:59:02 +00:00
import * as vscode from 'vscode';
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
2018-10-07 20:59:02 +00:00
export class Config {
2020-03-09 17:57:13 +00:00
readonly extensionId = "matklad.rust-analyzer";
private readonly rootSection = "rust-analyzer";
private readonly requiresReloadOpts = [
"serverPath",
"cargoFeatures",
"cargo-watch",
"highlighting.semanticTokens",
"inlayHints",
"updates.channel",
]
2020-03-09 17:57:13 +00:00
.map(opt => `${this.rootSection}.${opt}`);
readonly package: {
version: string;
releaseTag: string | undefined;
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.debug(
"Extension version:", this.package.version,
"using configuration:", this.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);
}
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 highlightingSemanticTokens() { return this.cfg.get<boolean>("highlighting.semanticTokens")!; }
get highlightingOn() { return this.cfg.get<boolean>("highlightingOn")!; }
get rainbowHighlightingOn() { return this.cfg.get<boolean>("rainbowHighlightingOn")!; }
get lruCapacity() { return this.cfg.get<null | number>("lruCapacity")!; }
get excludeGlobs() { return this.cfg.get<string[]>("excludeGlobs")!; }
get useClientWatching() { return this.cfg.get<boolean>("useClientWatching")!; }
get featureFlags() { return this.cfg.get<Record<string, boolean>>("featureFlags")!; }
get rustfmtArgs() { return this.cfg.get<string[]>("rustfmtArgs")!; }
get loadOutDirsFromCheck() { return this.cfg.get<boolean>("loadOutDirsFromCheck")!; }
get traceExtension() { return this.cfg.get<boolean>("trace.extension")!; }
// for internal use
get withSysroot() { return this.cfg.get<boolean>("withSysroot", true)!; }
get inlayHints() {
return {
2020-03-23 23:00:57 +00:00
typeHints: this.cfg.get<boolean>("inlayHints.typeHints")!,
parameterHints: this.cfg.get<boolean>("inlayHints.parameterHints")!,
2020-03-23 22:32:50 +00:00
chainingHints: this.cfg.get<boolean>("inlayHints.chainingHints")!,
2020-03-23 23:00:57 +00:00
maxLength: this.cfg.get<null | number>("inlayHints.maxLength")!,
};
}
2020-02-17 22:42:25 +00:00
2020-03-23 23:00:57 +00:00
get cargoWatchOptions() {
return {
2020-03-23 23:00:57 +00:00
enable: this.cfg.get<boolean>("cargo-watch.enable")!,
arguments: this.cfg.get<string[]>("cargo-watch.arguments")!,
allTargets: this.cfg.get<boolean>("cargo-watch.allTargets")!,
command: this.cfg.get<string>("cargo-watch.command")!,
};
}
2019-12-13 10:16:34 +00:00
2020-03-23 23:00:57 +00:00
get cargoFeatures() {
return {
2020-03-23 23:00:57 +00:00
noDefaultFeatures: this.cfg.get<boolean>("cargoFeatures.noDefaultFeatures")!,
allFeatures: this.cfg.get<boolean>("cargoFeatures.allFeatures")!,
features: this.cfg.get<string[]>("cargoFeatures.features")!,
loadOutDirsFromCheck: this.cfg.get<boolean>("cargoFeatures.loadOutDirsFromCheck")!,
};
2018-10-07 20:59:02 +00:00
}
}