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-03-02 15:54:29 -06:00
|
|
|
import { TextDocument } from "vscode";
|
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
|
|
|
|
|
|
|
export const log = {
|
2020-02-28 15:56:17 -06:00
|
|
|
enabled: true,
|
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);
|
|
|
|
},
|
|
|
|
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);
|
|
|
|
},
|
|
|
|
setEnabled(yes: boolean): void {
|
2020-02-28 15:56:17 -06:00
|
|
|
log.enabled = yes;
|
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';
|
|
|
|
}
|
|
|
|
|
|
|
|
function sleep(ms: number) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
}
|
2020-03-02 15:54:29 -06:00
|
|
|
|
|
|
|
export function isRustDocument(document: TextDocument) {
|
|
|
|
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';
|
|
|
|
}
|