2020-02-22 12:58:00 -06:00
|
|
|
import * as lc from "vscode-languageclient";
|
|
|
|
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-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;
|
|
|
|
|
|
|
|
setEnabled(yes: boolean): void {
|
|
|
|
log.enabled = yes;
|
|
|
|
}
|
|
|
|
|
2020-02-21 08:59:46 -06:00
|
|
|
debug(message?: any, ...optionalParams: any[]): void {
|
2020-02-28 15:56:17 -06:00
|
|
|
if (!log.enabled) return;
|
2020-02-21 08:59:46 -06:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(message, ...optionalParams);
|
2020-03-09 12:54:54 -05:00
|
|
|
}
|
|
|
|
|
2020-02-21 08:59:46 -06:00
|
|
|
error(message?: any, ...optionalParams: any[]): void {
|
2020-02-28 15:56:17 -06:00
|
|
|
if (!log.enabled) return;
|
2020-02-21 08:59:46 -06:00
|
|
|
debugger;
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.error(message, ...optionalParams);
|
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-02-22 12:58:00 -06:00
|
|
|
for (const delay of [2, 4, 6, 8, 10, null]) {
|
|
|
|
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-02-24 16:57:49 -06:00
|
|
|
log.error("LSP request timed out", { method: reqType.method, param, error });
|
2020-02-22 12:58:00 -06:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.code === lc.ErrorCodes.RequestCancelled) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error.code !== lc.ErrorCodes.ContentModified) {
|
2020-02-24 16:57:49 -06:00
|
|
|
log.error("LSP request failed", { method: reqType.method, param, error });
|
2020-02-22 12:58:00 -06:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
|
|
|
|
await sleep(10 * (1 << delay));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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-03-02 15:54:29 -06:00
|
|
|
return document.languageId === 'rust'
|
|
|
|
// SCM diff views have the same URI as the on-disk document but not the same content
|
|
|
|
&& document.uri.scheme !== 'git'
|
|
|
|
&& document.uri.scheme !== 'svn';
|
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' });
|
|
|
|
|
|
|
|
log.debug(res, "--version output:", res.output);
|
|
|
|
|
|
|
|
return res.status === 0;
|
|
|
|
}
|