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

192 lines
6.6 KiB
TypeScript
Raw Normal View History

import * as os from "os";
2018-10-07 20:59:02 +00:00
import * as vscode from 'vscode';
import { ArtifactSource } from "./installation/interfaces";
2020-02-21 14:59:46 +00:00
import { log } from "./util";
2018-10-07 20:59:02 +00:00
const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
export interface InlayHintOptions {
typeHints: boolean;
parameterHints: boolean;
2020-03-12 15:43:07 +00:00
maxLength: number | null;
}
export interface CargoWatchOptions {
enable: boolean;
arguments: string[];
command: string;
allTargets: boolean;
2019-04-02 06:43:02 +00:00
}
2019-12-13 10:16:34 +00:00
export interface CargoFeatures {
noDefaultFeatures: boolean;
allFeatures: boolean;
features: string[];
}
2018-10-07 20:59:02 +00:00
export class Config {
private static readonly rootSection = "rust-analyzer";
private static readonly requiresReloadOpts = [
"cargoFeatures",
"cargo-watch",
"highlighting.semanticTokens",
"inlayHints",
]
2020-02-17 22:42:25 +00:00
.map(opt => `${Config.rootSection}.${opt}`);
private static readonly extensionVersion: string = (() => {
const packageJsonVersion = vscode
.extensions
.getExtension("matklad.rust-analyzer")!
.packageJSON
.version as string; // n.n.YYYYMMDD
const realVersionRegexp = /^\d+\.\d+\.(\d{4})(\d{2})(\d{2})/;
const [, yyyy, mm, dd] = packageJsonVersion.match(realVersionRegexp)!;
return `${yyyy}-${mm}-${dd}`;
})();
private cfg!: vscode.WorkspaceConfiguration;
constructor(private readonly ctx: vscode.ExtensionContext) {
vscode.workspace.onDidChangeConfiguration(this.onConfigChange, this, ctx.subscriptions);
this.refreshConfig();
}
private refreshConfig() {
this.cfg = vscode.workspace.getConfiguration(Config.rootSection);
2020-02-21 14:59:46 +00:00
const enableLogging = this.cfg.get("trace.extension") as boolean;
log.setEnabled(enableLogging);
log.debug("Using configuration:", this.cfg);
}
2020-02-14 21:06:11 +00:00
private async onConfigChange(event: vscode.ConfigurationChangeEvent) {
this.refreshConfig();
const requiresReloadOpt = Config.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") {
vscode.commands.executeCommand("workbench.action.reloadWindow");
}
2018-10-08 21:36:47 +00:00
}
2018-10-07 20:59:02 +00:00
private static replaceTildeWithHomeDir(path: string) {
if (path.startsWith("~/")) {
return os.homedir() + path.slice("~".length);
}
return path;
}
/**
2020-02-18 11:33:16 +00:00
* Name of the binary artifact for `rust-analyzer` that is published for
* `platform` on GitHub releases. (It is also stored under the same name when
* downloaded by the extension).
*/
2020-02-14 22:42:32 +00:00
get prebuiltServerFileName(): null | string {
// See possible `arch` values here:
// https://nodejs.org/api/process.html#process_process_arch
switch (process.platform) {
case "linux": {
switch (process.arch) {
case "arm":
case "arm64": return null;
2020-02-18 11:33:16 +00:00
default: return "rust-analyzer-linux";
}
}
2020-02-18 11:33:16 +00:00
case "darwin": return "rust-analyzer-mac";
case "win32": return "rust-analyzer-windows.exe";
// Users on these platforms yet need to manually build from sources
case "aix":
case "android":
case "freebsd":
case "openbsd":
case "sunos":
case "cygwin":
case "netbsd": return null;
2020-02-08 22:28:32 +00:00
// The list of platforms is exhaustive (see `NodeJS.Platform` type definition)
}
}
get serverSource(): null | ArtifactSource {
2020-02-18 11:35:44 +00:00
const serverPath = RA_LSP_DEBUG ?? this.cfg.get<null | string>("serverPath");
2020-02-14 22:42:32 +00:00
if (serverPath) {
return {
type: ArtifactSource.Type.ExplicitPath,
2020-02-14 22:42:32 +00:00
path: Config.replaceTildeWithHomeDir(serverPath)
};
}
2020-02-14 22:42:32 +00:00
const prebuiltBinaryName = this.prebuiltServerFileName;
if (!prebuiltBinaryName) return null;
return {
type: ArtifactSource.Type.GithubRelease,
2020-02-17 22:42:25 +00:00
dir: this.ctx.globalStoragePath,
file: prebuiltBinaryName,
storage: this.ctx.globalState,
tag: Config.extensionVersion,
askBeforeDownload: this.cfg.get("updates.askBeforeDownload") as boolean,
repo: {
name: "rust-analyzer",
owner: "rust-analyzer",
}
};
}
// 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-26 15:03:30 +00:00
get highlightingSemanticTokens() { return this.cfg.get("highlighting.semanticTokens") as boolean; }
2020-02-17 22:42:25 +00:00
get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; }
get rainbowHighlightingOn() { return this.cfg.get("rainbowHighlightingOn") as boolean; }
2020-02-17 22:42:25 +00:00
get lruCapacity() { return this.cfg.get("lruCapacity") as null | number; }
get inlayHints(): InlayHintOptions {
return {
typeHints: this.cfg.get("inlayHints.typeHints") as boolean,
parameterHints: this.cfg.get("inlayHints.parameterHints") as boolean,
2020-03-12 15:43:07 +00:00
maxLength: this.cfg.get("inlayHints.maxLength") as null | number,
};
}
2020-02-17 22:42:25 +00:00
get excludeGlobs() { return this.cfg.get("excludeGlobs") as string[]; }
get useClientWatching() { return this.cfg.get("useClientWatching") as boolean; }
get featureFlags() { return this.cfg.get("featureFlags") as Record<string, boolean>; }
2020-03-10 13:58:15 +00:00
get additionalOutDirs() { return this.cfg.get("additionalOutDirs") as Record<string, string>; }
2020-02-17 22:42:25 +00:00
get rustfmtArgs() { return this.cfg.get("rustfmtArgs") as string[]; }
get cargoWatchOptions(): CargoWatchOptions {
return {
2020-02-17 22:42:25 +00:00
enable: this.cfg.get("cargo-watch.enable") as boolean,
arguments: this.cfg.get("cargo-watch.arguments") as string[],
allTargets: this.cfg.get("cargo-watch.allTargets") as boolean,
2020-02-17 22:42:25 +00:00
command: this.cfg.get("cargo-watch.command") as string,
};
}
2019-12-13 10:16:34 +00:00
get cargoFeatures(): CargoFeatures {
return {
noDefaultFeatures: this.cfg.get("cargoFeatures.noDefaultFeatures") as boolean,
2020-02-17 22:42:25 +00:00
allFeatures: this.cfg.get("cargoFeatures.allFeatures") as boolean,
features: this.cfg.get("cargoFeatures.features") as string[],
};
2018-10-07 20:59:02 +00:00
}
// for internal use
get withSysroot() { return this.cfg.get("withSysroot", true) as boolean; }
2018-10-07 20:59:02 +00:00
}