mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 11:31:15 +00:00
Don't request inline hints repeatedly
This commit is contained in:
parent
23bac12062
commit
cdd7118cbf
@ -62,20 +62,23 @@ export class Ctx {
|
|||||||
this.extCtx.subscriptions.push(d);
|
this.extCtx.subscriptions.push(d);
|
||||||
}
|
}
|
||||||
|
|
||||||
async sendRequestWithRetry<R>(method: string, param: any): Promise<R> {
|
async sendRequestWithRetry<R>(method: string, param: any, token: vscode.CancellationToken): Promise<R> {
|
||||||
await this.client.onReady();
|
await this.client.onReady();
|
||||||
const nRetries = 3;
|
for (const delay of [2, 4, 6, 8, 10, null]) {
|
||||||
for (let triesLeft = nRetries; ; triesLeft--) {
|
|
||||||
try {
|
try {
|
||||||
return await this.client.sendRequest(method, param);
|
return await this.client.sendRequest(method, param, token);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.code === lc.ErrorCodes.ContentModified && triesLeft > 0) {
|
if (e.code === lc.ErrorCodes.ContentModified && delay !== null) {
|
||||||
|
await sleep(10 * (1 << delay))
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
throw 'unreachable'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export type Cmd = (...args: any[]) => any;
|
export type Cmd = (...args: any[]) => any;
|
||||||
|
|
||||||
|
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms))
|
||||||
|
@ -41,6 +41,7 @@ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
|
|||||||
});
|
});
|
||||||
|
|
||||||
class HintsUpdater {
|
class HintsUpdater {
|
||||||
|
private pending: Map<string, vscode.CancellationTokenSource> = new Map();
|
||||||
private ctx: Ctx;
|
private ctx: Ctx;
|
||||||
private enabled = true;
|
private enabled = true;
|
||||||
|
|
||||||
@ -67,7 +68,8 @@ class HintsUpdater {
|
|||||||
|
|
||||||
private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
|
private async refreshEditor(editor: vscode.TextEditor): Promise<void> {
|
||||||
const newHints = await this.queryHints(editor.document.uri.toString());
|
const newHints = await this.queryHints(editor.document.uri.toString());
|
||||||
const newDecorations = (newHints ? newHints : []).map(hint => ({
|
if (newHints == null) return;
|
||||||
|
const newDecorations = newHints.map(hint => ({
|
||||||
range: hint.range,
|
range: hint.range,
|
||||||
renderOptions: {
|
renderOptions: {
|
||||||
after: {
|
after: {
|
||||||
@ -98,9 +100,20 @@ class HintsUpdater {
|
|||||||
const request: InlayHintsParams = {
|
const request: InlayHintsParams = {
|
||||||
textDocument: { uri: documentUri },
|
textDocument: { uri: documentUri },
|
||||||
};
|
};
|
||||||
return this.ctx.sendRequestWithRetry<InlayHint[] | null>(
|
let tokenSource = new vscode.CancellationTokenSource();
|
||||||
|
let prev = this.pending.get(documentUri);
|
||||||
|
if (prev) prev.cancel()
|
||||||
|
this.pending.set(documentUri, tokenSource);
|
||||||
|
try {
|
||||||
|
return await this.ctx.sendRequestWithRetry<InlayHint[] | null>(
|
||||||
'rust-analyzer/inlayHints',
|
'rust-analyzer/inlayHints',
|
||||||
request,
|
request,
|
||||||
|
tokenSource.token,
|
||||||
);
|
);
|
||||||
|
} finally {
|
||||||
|
if (!tokenSource.token.isCancellationRequested) {
|
||||||
|
this.pending.delete(documentUri)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user