2020-02-07 20:22:44 -06:00
|
|
|
import * as os from "os";
|
2018-10-07 15:59:02 -05:00
|
|
|
import * as vscode from 'vscode';
|
2020-03-07 16:01:48 -06:00
|
|
|
import { ArtifactSource } from "./installation/interfaces";
|
2020-02-21 08:59:46 -06:00
|
|
|
import { log } from "./util";
|
2018-10-07 15:59:02 -05:00
|
|
|
|
2019-01-18 04:59:08 -06:00
|
|
|
const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
|
|
|
|
|
2020-03-10 02:55:46 -05:00
|
|
|
export interface InlayHintOptions {
|
2020-03-11 22:14:39 -05:00
|
|
|
typeHints: boolean;
|
|
|
|
parameterHints: boolean;
|
2020-03-12 10:43:07 -05:00
|
|
|
maxLength: number | null;
|
2020-03-10 02:55:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-25 13:23:44 -06:00
|
|
|
export interface CargoWatchOptions {
|
|
|
|
enable: boolean;
|
2019-12-25 09:50:38 -06:00
|
|
|
arguments: string[];
|
2019-12-25 13:23:44 -06:00
|
|
|
command: string;
|
|
|
|
allTargets: boolean;
|
2019-04-02 01:43:02 -05:00
|
|
|
}
|
2019-03-21 06:56:25 -05:00
|
|
|
|
2019-12-13 04:16:34 -06:00
|
|
|
export interface CargoFeatures {
|
|
|
|
noDefaultFeatures: boolean;
|
|
|
|
allFeatures: boolean;
|
|
|
|
features: string[];
|
|
|
|
}
|
2020-03-09 12:57:13 -05:00
|
|
|
|
|
|
|
export const enum UpdatesChannel {
|
|
|
|
Stable = "stable",
|
|
|
|
Nightly = "nightly"
|
|
|
|
}
|
|
|
|
|
|
|
|
export const NIGHTLY_TAG = "nightly";
|
2018-10-07 15:59:02 -05:00
|
|
|
export class Config {
|
2020-03-09 12:57:13 -05:00
|
|
|
readonly extensionId = "matklad.rust-analyzer";
|
|
|
|
|
|
|
|
private readonly rootSection = "rust-analyzer";
|
|
|
|
private readonly requiresReloadOpts = [
|
2020-02-13 14:48:20 -06:00
|
|
|
"cargoFeatures",
|
|
|
|
"cargo-watch",
|
2020-03-10 13:21:56 -05:00
|
|
|
"highlighting.semanticTokens",
|
2020-03-11 22:14:39 -05:00
|
|
|
"inlayHints",
|
2020-02-13 14:48:20 -06:00
|
|
|
]
|
2020-03-09 12:57:13 -05:00
|
|
|
.map(opt => `${this.rootSection}.${opt}`);
|
2020-02-13 14:48:20 -06:00
|
|
|
|
2020-03-09 12:57:13 -05:00
|
|
|
/**
|
|
|
|
* Either `nightly` or `YYYY-MM-DD` (i.e. `stable` release)
|
|
|
|
*/
|
2020-03-09 13:14:55 -05:00
|
|
|
readonly extensionReleaseTag: string = (() => {
|
2020-02-15 19:08:36 -06:00
|
|
|
const packageJsonVersion = vscode
|
|
|
|
.extensions
|
2020-03-09 12:57:13 -05:00
|
|
|
.getExtension(this.extensionId)!
|
2020-02-15 19:08:36 -06:00
|
|
|
.packageJSON
|
2020-03-09 12:57:13 -05:00
|
|
|
.version as string;
|
|
|
|
|
|
|
|
if (packageJsonVersion.endsWith(NIGHTLY_TAG)) return NIGHTLY_TAG;
|
2020-02-15 19:08:36 -06:00
|
|
|
|
|
|
|
const realVersionRegexp = /^\d+\.\d+\.(\d{4})(\d{2})(\d{2})/;
|
|
|
|
const [, yyyy, mm, dd] = packageJsonVersion.match(realVersionRegexp)!;
|
|
|
|
|
|
|
|
return `${yyyy}-${mm}-${dd}`;
|
|
|
|
})();
|
|
|
|
|
2020-02-13 14:48:20 -06:00
|
|
|
private cfg!: vscode.WorkspaceConfiguration;
|
|
|
|
|
2020-02-13 15:05:32 -06:00
|
|
|
constructor(private readonly ctx: vscode.ExtensionContext) {
|
|
|
|
vscode.workspace.onDidChangeConfiguration(this.onConfigChange, this, ctx.subscriptions);
|
|
|
|
this.refreshConfig();
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:48:20 -06:00
|
|
|
private refreshConfig() {
|
2020-03-09 12:57:13 -05:00
|
|
|
this.cfg = vscode.workspace.getConfiguration(this.rootSection);
|
2020-02-21 08:59:46 -06:00
|
|
|
const enableLogging = this.cfg.get("trace.extension") as boolean;
|
|
|
|
log.setEnabled(enableLogging);
|
|
|
|
log.debug("Using configuration:", this.cfg);
|
2020-02-13 14:48:20 -06:00
|
|
|
}
|
2020-02-07 20:22:44 -06:00
|
|
|
|
2020-02-14 15:06:11 -06:00
|
|
|
private async onConfigChange(event: vscode.ConfigurationChangeEvent) {
|
2020-02-13 14:48:20 -06:00
|
|
|
this.refreshConfig();
|
|
|
|
|
2020-03-09 12:57:13 -05:00
|
|
|
const requiresReloadOpt = this.requiresReloadOpts.find(
|
2020-02-13 14:48:20 -06:00
|
|
|
opt => event.affectsConfiguration(opt)
|
|
|
|
);
|
2018-10-07 15:59:02 -05:00
|
|
|
|
2020-02-13 14:48:20 -06:00
|
|
|
if (!requiresReloadOpt) return;
|
2019-02-07 04:37:36 -06:00
|
|
|
|
2020-02-13 14:48:20 -06:00
|
|
|
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 16:36:47 -05:00
|
|
|
}
|
2018-10-07 15:59:02 -05:00
|
|
|
|
2020-02-13 14:48:20 -06:00
|
|
|
private static replaceTildeWithHomeDir(path: string) {
|
|
|
|
if (path.startsWith("~/")) {
|
|
|
|
return os.homedir() + path.slice("~".length);
|
2020-02-07 20:22:44 -06:00
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2020-02-08 16:27:04 -06:00
|
|
|
/**
|
2020-02-18 05:33:16 -06:00
|
|
|
* Name of the binary artifact for `rust-analyzer` that is published for
|
2020-02-08 16:27:04 -06:00
|
|
|
* `platform` on GitHub releases. (It is also stored under the same name when
|
|
|
|
* downloaded by the extension).
|
|
|
|
*/
|
2020-02-14 16:42:32 -06:00
|
|
|
get prebuiltServerFileName(): null | string {
|
2020-02-10 18:29:11 -06:00
|
|
|
// See possible `arch` values here:
|
|
|
|
// https://nodejs.org/api/process.html#process_process_arch
|
|
|
|
|
2020-02-14 15:04:50 -06:00
|
|
|
switch (process.platform) {
|
2020-02-10 18:29:11 -06:00
|
|
|
|
|
|
|
case "linux": {
|
2020-02-14 15:04:50 -06:00
|
|
|
switch (process.arch) {
|
2020-02-10 18:29:11 -06:00
|
|
|
case "arm":
|
|
|
|
case "arm64": return null;
|
|
|
|
|
2020-02-18 05:33:16 -06:00
|
|
|
default: return "rust-analyzer-linux";
|
2020-02-10 18:29:11 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 05:33:16 -06:00
|
|
|
case "darwin": return "rust-analyzer-mac";
|
|
|
|
case "win32": return "rust-analyzer-windows.exe";
|
2020-02-08 16:27:04 -06:00
|
|
|
|
|
|
|
// 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 16:28:32 -06:00
|
|
|
// The list of platforms is exhaustive (see `NodeJS.Platform` type definition)
|
2020-02-08 16:27:04 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 13:14:55 -05:00
|
|
|
get installedExtensionUpdateChannel(): UpdatesChannel {
|
|
|
|
return this.extensionReleaseTag === NIGHTLY_TAG
|
2020-03-09 12:57:13 -05:00
|
|
|
? UpdatesChannel.Nightly
|
|
|
|
: UpdatesChannel.Stable;
|
|
|
|
}
|
|
|
|
|
2020-03-07 16:01:48 -06:00
|
|
|
get serverSource(): null | ArtifactSource {
|
2020-03-09 12:57:13 -05:00
|
|
|
const serverPath = RA_LSP_DEBUG ?? this.serverPath;
|
2020-02-08 16:27:04 -06:00
|
|
|
|
2020-02-14 16:42:32 -06:00
|
|
|
if (serverPath) {
|
2020-02-08 16:27:04 -06:00
|
|
|
return {
|
2020-03-07 16:01:48 -06:00
|
|
|
type: ArtifactSource.Type.ExplicitPath,
|
2020-02-14 16:42:32 -06:00
|
|
|
path: Config.replaceTildeWithHomeDir(serverPath)
|
2020-02-08 16:27:04 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-14 16:42:32 -06:00
|
|
|
const prebuiltBinaryName = this.prebuiltServerFileName;
|
2020-02-08 16:27:04 -06:00
|
|
|
|
2020-02-09 05:45:06 -06:00
|
|
|
if (!prebuiltBinaryName) return null;
|
|
|
|
|
2020-03-09 12:57:13 -05:00
|
|
|
return this.createGithubReleaseSource(
|
|
|
|
prebuiltBinaryName,
|
2020-03-09 13:14:55 -05:00
|
|
|
this.extensionReleaseTag
|
2020-03-09 12:57:13 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
private createGithubReleaseSource(file: string, tag: string): ArtifactSource.GithubRelease {
|
2020-02-09 05:45:06 -06:00
|
|
|
return {
|
2020-03-07 16:01:48 -06:00
|
|
|
type: ArtifactSource.Type.GithubRelease,
|
2020-03-09 12:57:13 -05:00
|
|
|
file,
|
|
|
|
tag,
|
2020-02-17 16:42:25 -06:00
|
|
|
dir: this.ctx.globalStoragePath,
|
2020-02-08 16:27:04 -06:00
|
|
|
repo: {
|
|
|
|
name: "rust-analyzer",
|
2020-03-09 12:57:13 -05:00
|
|
|
owner: "rust-analyzer"
|
2020-02-08 16:27:04 -06:00
|
|
|
}
|
2020-03-09 13:04:11 -05:00
|
|
|
};
|
2020-02-08 16:27:04 -06:00
|
|
|
}
|
|
|
|
|
2020-03-09 12:57:13 -05:00
|
|
|
get nightlyVsixSource(): ArtifactSource.GithubRelease {
|
|
|
|
return this.createGithubReleaseSource("rust-analyzer.vsix", NIGHTLY_TAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
readonly installedNightlyExtensionReleaseDate = new DateStorage(
|
|
|
|
"rust-analyzer-installed-nightly-extension-release-date",
|
|
|
|
this.ctx.globalState
|
|
|
|
);
|
|
|
|
readonly serverReleaseDate = new DateStorage(
|
|
|
|
"rust-analyzer-server-release-date",
|
|
|
|
this.ctx.globalState
|
|
|
|
);
|
|
|
|
readonly serverReleaseTag = new StringStorage(
|
|
|
|
"rust-analyzer-release-tag", this.ctx.globalState
|
|
|
|
);
|
|
|
|
|
2020-02-13 14:48:20 -06: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-08 16:27:04 -06:00
|
|
|
|
2020-03-09 13:14:55 -05:00
|
|
|
get serverPath() { return this.cfg.get("serverPath") as null | string; }
|
2020-03-09 12:57:13 -05:00
|
|
|
get updatesChannel() { return this.cfg.get("updates.channel") as UpdatesChannel; }
|
|
|
|
get askBeforeDownload() { return this.cfg.get("updates.askBeforeDownload") as boolean; }
|
2020-02-26 09:03:30 -06:00
|
|
|
get highlightingSemanticTokens() { return this.cfg.get("highlighting.semanticTokens") as boolean; }
|
2020-02-17 16:42:25 -06:00
|
|
|
get highlightingOn() { return this.cfg.get("highlightingOn") as boolean; }
|
2020-02-14 15:04:50 -06:00
|
|
|
get rainbowHighlightingOn() { return this.cfg.get("rainbowHighlightingOn") as boolean; }
|
2020-02-17 16:42:25 -06:00
|
|
|
get lruCapacity() { return this.cfg.get("lruCapacity") as null | number; }
|
2020-03-11 22:14:39 -05:00
|
|
|
get inlayHints(): InlayHintOptions {
|
2020-03-10 02:55:46 -05:00
|
|
|
return {
|
2020-03-11 22:14:39 -05:00
|
|
|
typeHints: this.cfg.get("inlayHints.typeHints") as boolean,
|
|
|
|
parameterHints: this.cfg.get("inlayHints.parameterHints") as boolean,
|
2020-03-12 10:43:07 -05:00
|
|
|
maxLength: this.cfg.get("inlayHints.maxLength") as null | number,
|
2020-03-10 02:55:46 -05:00
|
|
|
};
|
|
|
|
}
|
2020-02-17 16:42:25 -06: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 08:58:15 -05:00
|
|
|
get additionalOutDirs() { return this.cfg.get("additionalOutDirs") as Record<string, string>; }
|
2020-02-17 16:42:25 -06:00
|
|
|
get rustfmtArgs() { return this.cfg.get("rustfmtArgs") as string[]; }
|
|
|
|
|
2020-02-14 15:04:50 -06:00
|
|
|
get cargoWatchOptions(): CargoWatchOptions {
|
2020-02-13 14:48:20 -06:00
|
|
|
return {
|
2020-02-17 16:42:25 -06:00
|
|
|
enable: this.cfg.get("cargo-watch.enable") as boolean,
|
|
|
|
arguments: this.cfg.get("cargo-watch.arguments") as string[],
|
2020-02-13 14:48:20 -06:00
|
|
|
allTargets: this.cfg.get("cargo-watch.allTargets") as boolean,
|
2020-02-17 16:42:25 -06:00
|
|
|
command: this.cfg.get("cargo-watch.command") as string,
|
2020-02-13 14:48:20 -06:00
|
|
|
};
|
|
|
|
}
|
2019-12-13 04:16:34 -06:00
|
|
|
|
2020-02-14 15:04:50 -06:00
|
|
|
get cargoFeatures(): CargoFeatures {
|
2020-02-13 14:48:20 -06:00
|
|
|
return {
|
|
|
|
noDefaultFeatures: this.cfg.get("cargoFeatures.noDefaultFeatures") as boolean,
|
2020-02-17 16:42:25 -06:00
|
|
|
allFeatures: this.cfg.get("cargoFeatures.allFeatures") as boolean,
|
|
|
|
features: this.cfg.get("cargoFeatures.features") as string[],
|
2020-02-13 14:48:20 -06:00
|
|
|
};
|
2018-10-07 15:59:02 -05:00
|
|
|
}
|
2020-02-13 14:48:20 -06:00
|
|
|
|
|
|
|
// for internal use
|
2020-02-16 03:15:19 -06:00
|
|
|
get withSysroot() { return this.cfg.get("withSysroot", true) as boolean; }
|
2018-10-07 15:59:02 -05:00
|
|
|
}
|
2020-03-09 12:57:13 -05:00
|
|
|
|
|
|
|
export class StringStorage {
|
|
|
|
constructor(
|
|
|
|
private readonly key: string,
|
|
|
|
private readonly storage: vscode.Memento
|
2020-03-09 13:04:11 -05:00
|
|
|
) { }
|
2020-03-09 12:57:13 -05:00
|
|
|
|
|
|
|
get(): null | string {
|
|
|
|
const tag = this.storage.get(this.key, null);
|
|
|
|
log.debug(this.key, "==", tag);
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
async set(tag: string) {
|
|
|
|
log.debug(this.key, "=", tag);
|
|
|
|
await this.storage.update(this.key, tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
export class DateStorage {
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private readonly key: string,
|
|
|
|
private readonly storage: vscode.Memento
|
2020-03-09 13:04:11 -05:00
|
|
|
) { }
|
2020-03-09 12:57:13 -05:00
|
|
|
|
|
|
|
get(): null | Date {
|
|
|
|
const date = this.storage.get(this.key, null);
|
|
|
|
log.debug(this.key, "==", date);
|
|
|
|
return date ? new Date(date) : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
async set(date: null | Date) {
|
|
|
|
log.debug(this.key, "=", date);
|
|
|
|
await this.storage.update(this.key, date);
|
|
|
|
}
|
|
|
|
}
|