2020-09-01 11:53:07 -05:00
|
|
|
import * as lc from "vscode-languageclient/node";
|
2020-02-22 12:58:00 -06:00
|
|
|
import * as vscode from "vscode";
|
2020-02-28 16:07:29 -06:00
|
|
|
import { strict as nativeAssert } from "assert";
|
2020-05-05 17:42:04 -05:00
|
|
|
import { spawnSync } from "child_process";
|
2020-07-05 09:42:52 -05:00
|
|
|
import { inspect } from "util";
|
2020-02-22 12:58:00 -06:00
|
|
|
|
2020-02-28 16:46:48 -06:00
|
|
|
export function assert(condition: boolean, explanation: string): asserts condition {
|
2020-02-28 16:02:19 -06:00
|
|
|
try {
|
2020-02-28 16:07:29 -06:00
|
|
|
nativeAssert(condition, explanation);
|
2020-02-28 16:02:19 -06:00
|
|
|
} catch (err) {
|
|
|
|
log.error(`Assertion failed:`, explanation);
|
|
|
|
throw err;
|
|
|
|
}
|
2020-02-28 15:56:17 -06:00
|
|
|
}
|
2020-02-21 08:59:46 -06:00
|
|
|
|
2020-03-09 12:54:54 -05:00
|
|
|
export const log = new class {
|
|
|
|
private enabled = true;
|
2020-07-05 09:42:52 -05:00
|
|
|
private readonly output = vscode.window.createOutputChannel("Rust Analyzer Client");
|
2020-03-09 12:54:54 -05:00
|
|
|
|
|
|
|
setEnabled(yes: boolean): void {
|
|
|
|
log.enabled = yes;
|
|
|
|
}
|
|
|
|
|
2020-07-05 09:42:52 -05:00
|
|
|
// Hint: the type [T, ...T[]] means a non-empty array
|
|
|
|
debug(...msg: [unknown, ...unknown[]]): void {
|
2020-02-28 15:56:17 -06:00
|
|
|
if (!log.enabled) return;
|
2020-07-05 09:42:52 -05:00
|
|
|
log.write("DEBUG", ...msg);
|
2020-03-09 12:54:54 -05:00
|
|
|
}
|
|
|
|
|
2020-07-05 09:42:52 -05:00
|
|
|
info(...msg: [unknown, ...unknown[]]): void {
|
|
|
|
log.write("INFO", ...msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
warn(...msg: [unknown, ...unknown[]]): void {
|
|
|
|
debugger;
|
|
|
|
log.write("WARN", ...msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
error(...msg: [unknown, ...unknown[]]): void {
|
2020-02-21 08:59:46 -06:00
|
|
|
debugger;
|
2020-07-05 09:42:52 -05:00
|
|
|
log.write("ERROR", ...msg);
|
|
|
|
log.output.show(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private write(label: string, ...messageParts: unknown[]): void {
|
|
|
|
const message = messageParts.map(log.stringify).join(" ");
|
|
|
|
const dateTime = new Date().toLocaleString();
|
|
|
|
log.output.appendLine(`${label} [${dateTime}]: ${message}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
private stringify(val: unknown): string {
|
|
|
|
if (typeof val === "string") return val;
|
|
|
|
return inspect(val, {
|
|
|
|
colors: false,
|
|
|
|
depth: 6, // heuristic
|
|
|
|
});
|
2020-03-09 12:54:54 -05:00
|
|
|
}
|
2020-02-21 08:59:46 -06:00
|
|
|
};
|
2020-02-22 12:58:00 -06:00
|
|
|
|
2020-02-24 16:57:49 -06:00
|
|
|
export async function sendRequestWithRetry<TParam, TRet>(
|
2020-02-22 12:58:00 -06:00
|
|
|
client: lc.LanguageClient,
|
2020-02-24 16:57:49 -06:00
|
|
|
reqType: lc.RequestType<TParam, TRet, unknown>,
|
|
|
|
param: TParam,
|
2020-02-22 12:58:00 -06:00
|
|
|
token?: vscode.CancellationToken,
|
2020-02-24 16:57:49 -06:00
|
|
|
): Promise<TRet> {
|
2020-08-08 13:53:38 -05:00
|
|
|
// The sequence is `10 * (2 ** (2 * n))` where n is 1, 2, 3...
|
|
|
|
for (const delay of [40, 160, 640, 2560, 10240, null]) {
|
2020-02-22 12:58:00 -06:00
|
|
|
try {
|
|
|
|
return await (token
|
2020-02-24 16:57:49 -06:00
|
|
|
? client.sendRequest(reqType, param, token)
|
|
|
|
: client.sendRequest(reqType, param)
|
2020-02-22 12:58:00 -06:00
|
|
|
);
|
|
|
|
} catch (error) {
|
|
|
|
if (delay === null) {
|
2020-07-05 09:42:52 -05:00
|
|
|
log.warn("LSP request timed out", { method: reqType.method, param, error });
|
2020-02-22 12:58:00 -06:00
|
|
|
throw error;
|
|
|
|
}
|
2020-10-21 13:57:55 -05:00
|
|
|
if (error.code === lc.LSPErrorCodes.RequestCancelled) {
|
2020-02-22 12:58:00 -06:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
2020-10-21 13:57:55 -05:00
|
|
|
if (error.code !== lc.LSPErrorCodes.ContentModified) {
|
2020-07-05 09:42:52 -05:00
|
|
|
log.warn("LSP request failed", { method: reqType.method, param, error });
|
2020-02-22 12:58:00 -06:00
|
|
|
throw error;
|
|
|
|
}
|
2020-08-08 13:53:38 -05:00
|
|
|
await sleep(delay);
|
2020-02-22 12:58:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
throw 'unreachable';
|
|
|
|
}
|
|
|
|
|
2020-03-31 08:05:42 -05:00
|
|
|
export function sleep(ms: number) {
|
2020-02-22 12:58:00 -06:00
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|
2020-03-02 15:54:29 -06:00
|
|
|
|
2020-03-07 06:07:44 -06:00
|
|
|
export type RustDocument = vscode.TextDocument & { languageId: "rust" };
|
2020-03-31 08:05:42 -05:00
|
|
|
export type RustEditor = vscode.TextEditor & { document: RustDocument };
|
2020-03-07 06:07:44 -06:00
|
|
|
|
|
|
|
export function isRustDocument(document: vscode.TextDocument): document is RustDocument {
|
2020-05-26 14:51:04 -05:00
|
|
|
// Prevent corrupted text (particularly via inlay hints) in diff views
|
|
|
|
// by allowing only `file` schemes
|
|
|
|
// unfortunately extensions that use diff views not always set this
|
|
|
|
// to something different than 'file' (see ongoing bug: #4608)
|
|
|
|
return document.languageId === 'rust' && document.uri.scheme === 'file';
|
2020-03-07 06:07:44 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isRustEditor(editor: vscode.TextEditor): editor is RustEditor {
|
|
|
|
return isRustDocument(editor.document);
|
|
|
|
}
|
2020-05-05 17:42:04 -05:00
|
|
|
|
|
|
|
export function isValidExecutable(path: string): boolean {
|
|
|
|
log.debug("Checking availability of a binary at", path);
|
|
|
|
|
|
|
|
const res = spawnSync(path, ["--version"], { encoding: 'utf8' });
|
|
|
|
|
2020-07-06 05:39:08 -05:00
|
|
|
const printOutput = res.error && (res.error as any).code !== 'ENOENT' ? log.warn : log.debug;
|
2020-07-05 09:42:52 -05:00
|
|
|
printOutput(path, "--version:", res);
|
2020-05-05 17:42:04 -05:00
|
|
|
|
2020-07-06 05:39:08 -05:00
|
|
|
return res.status === 0;
|
2020-05-05 17:42:04 -05:00
|
|
|
}
|
2020-05-27 11:40:13 -05:00
|
|
|
|
|
|
|
/** Sets ['when'](https://code.visualstudio.com/docs/getstarted/keybindings#_when-clause-contexts) clause contexts */
|
|
|
|
export function setContextValue(key: string, value: any): Thenable<void> {
|
|
|
|
return vscode.commands.executeCommand('setContext', key, value);
|
|
|
|
}
|
2020-05-30 21:13:08 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a higher-order function that caches the results of invoking the
|
|
|
|
* underlying function.
|
|
|
|
*/
|
|
|
|
export function memoize<Ret, TThis, Param extends string>(func: (this: TThis, arg: Param) => Ret) {
|
|
|
|
const cache = new Map<string, Ret>();
|
|
|
|
|
|
|
|
return function(this: TThis, arg: Param) {
|
|
|
|
const cached = cache.get(arg);
|
|
|
|
if (cached) return cached;
|
|
|
|
|
|
|
|
const result = func.call(this, arg);
|
|
|
|
cache.set(arg, result);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
}
|