mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 21:54:42 +00:00
vscode: gracefully handle cancellation errors
This commit is contained in:
parent
838ad6bcfb
commit
4cee7cddc8
4 changed files with 60 additions and 44 deletions
|
@ -52,24 +52,3 @@ export interface Disposable {
|
||||||
dispose(): void;
|
dispose(): void;
|
||||||
}
|
}
|
||||||
export type Cmd = (...args: any[]) => unknown;
|
export type Cmd = (...args: any[]) => unknown;
|
||||||
|
|
||||||
export async function sendRequestWithRetry<R>(
|
|
||||||
client: lc.LanguageClient,
|
|
||||||
method: string,
|
|
||||||
param: unknown,
|
|
||||||
token?: vscode.CancellationToken,
|
|
||||||
): Promise<R> {
|
|
||||||
for (const delay of [2, 4, 6, 8, 10, null]) {
|
|
||||||
try {
|
|
||||||
return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param));
|
|
||||||
} catch (err) {
|
|
||||||
if (delay === null || err.code !== lc.ErrorCodes.ContentModified) {
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
await sleep(10 * (1 << delay));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw 'unreachable';
|
|
||||||
}
|
|
||||||
|
|
||||||
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
|
||||||
|
|
|
@ -3,7 +3,8 @@ import * as lc from 'vscode-languageclient';
|
||||||
|
|
||||||
import { ColorTheme, TextMateRuleSettings } from './color_theme';
|
import { ColorTheme, TextMateRuleSettings } from './color_theme';
|
||||||
|
|
||||||
import { Ctx, sendRequestWithRetry } from './ctx';
|
import { Ctx } from './ctx';
|
||||||
|
import { sendRequestWithRetry } from './util';
|
||||||
|
|
||||||
export function activateHighlighting(ctx: Ctx) {
|
export function activateHighlighting(ctx: Ctx) {
|
||||||
const highlighter = new Highlighter(ctx);
|
const highlighter = new Highlighter(ctx);
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
import * as vscode from 'vscode';
|
import * as vscode from 'vscode';
|
||||||
import * as lc from 'vscode-languageclient';
|
import * as lc from 'vscode-languageclient';
|
||||||
|
|
||||||
import { Ctx, sendRequestWithRetry } from './ctx';
|
import { Ctx } from './ctx';
|
||||||
import { log } from './util';
|
import { log, sendRequestWithRetry } from './util';
|
||||||
|
|
||||||
export function activateInlayHints(ctx: Ctx) {
|
export function activateInlayHints(ctx: Ctx) {
|
||||||
const hintsUpdater = new HintsUpdater(ctx);
|
const hintsUpdater = new HintsUpdater(ctx);
|
||||||
|
@ -152,28 +152,24 @@ class HintsUpdater {
|
||||||
}
|
}
|
||||||
|
|
||||||
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
|
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
|
||||||
const client = this.ctx.client;
|
this.pending.get(documentUri)?.cancel();
|
||||||
if (!client) return null;
|
|
||||||
|
|
||||||
const request: InlayHintsParams = {
|
|
||||||
textDocument: { uri: documentUri },
|
|
||||||
};
|
|
||||||
const tokenSource = new vscode.CancellationTokenSource();
|
const tokenSource = new vscode.CancellationTokenSource();
|
||||||
const prevHintsRequest = this.pending.get(documentUri);
|
|
||||||
prevHintsRequest?.cancel();
|
|
||||||
|
|
||||||
this.pending.set(documentUri, tokenSource);
|
this.pending.set(documentUri, tokenSource);
|
||||||
try {
|
|
||||||
return await sendRequestWithRetry<InlayHint[] | null>(
|
const request: InlayHintsParams = { textDocument: { uri: documentUri } };
|
||||||
client,
|
|
||||||
'rust-analyzer/inlayHints',
|
return sendRequestWithRetry<InlayHint[]>(
|
||||||
request,
|
this.ctx.client,
|
||||||
tokenSource.token,
|
'rust-analyzer/inlayHints',
|
||||||
);
|
request,
|
||||||
} finally {
|
tokenSource.token
|
||||||
if (!tokenSource.token.isCancellationRequested) {
|
)
|
||||||
this.pending.delete(documentUri);
|
.catch(_ => null)
|
||||||
}
|
.finally(() => {
|
||||||
}
|
if (!tokenSource.token.isCancellationRequested) {
|
||||||
|
this.pending.delete(documentUri);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,6 @@
|
||||||
|
import * as lc from "vscode-languageclient";
|
||||||
|
import * as vscode from "vscode";
|
||||||
|
|
||||||
let enabled: boolean = false;
|
let enabled: boolean = false;
|
||||||
|
|
||||||
export const log = {
|
export const log = {
|
||||||
|
@ -16,3 +19,40 @@ export const log = {
|
||||||
enabled = yes;
|
enabled = yes;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export async function sendRequestWithRetry<R>(
|
||||||
|
client: lc.LanguageClient,
|
||||||
|
method: string,
|
||||||
|
param: unknown,
|
||||||
|
token?: vscode.CancellationToken,
|
||||||
|
): Promise<R> {
|
||||||
|
for (const delay of [2, 4, 6, 8, 10, null]) {
|
||||||
|
try {
|
||||||
|
return await (token
|
||||||
|
? client.sendRequest(method, param, token)
|
||||||
|
: client.sendRequest(method, param)
|
||||||
|
);
|
||||||
|
} catch (error) {
|
||||||
|
if (delay === null) {
|
||||||
|
log.error("LSP request timed out", { method, param, error });
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error.code === lc.ErrorCodes.RequestCancelled) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (error.code !== lc.ErrorCodes.ContentModified) {
|
||||||
|
log.error("LSP request failed", { method, param, error });
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
await sleep(10 * (1 << delay));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
throw 'unreachable';
|
||||||
|
}
|
||||||
|
|
||||||
|
function sleep(ms: number) {
|
||||||
|
return new Promise(resolve => setTimeout(resolve, ms));
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue