vscode: migrate inlay_hints to rust-analyzer-api.ts

This commit is contained in:
Veetaha 2020-02-25 00:57:49 +02:00
parent 8aea0ec511
commit c9230b88b4
2 changed files with 16 additions and 31 deletions

View file

@ -1,5 +1,5 @@
import * as vscode from 'vscode';
import * as lc from 'vscode-languageclient';
import * as ra from './rust-analyzer-api';
import { Ctx } from './ctx';
import { log, sendRequestWithRetry } from './util';
@ -39,16 +39,6 @@ export function activateInlayHints(ctx: Ctx) {
void hintsUpdater.setEnabled(ctx.config.displayInlayHints);
}
interface InlayHintsParams {
textDocument: lc.TextDocumentIdentifier;
}
interface InlayHint {
range: vscode.Range;
kind: "TypeHint" | "ParameterHint";
label: string;
}
const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
after: {
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
@ -107,9 +97,9 @@ class HintsUpdater {
if (newHints == null) return;
const newTypeDecorations = newHints
.filter(hint => hint.kind === 'TypeHint')
.filter(hint => hint.kind === ra.InlayKind.TypeHint)
.map(hint => ({
range: hint.range,
range: this.ctx.client.protocol2CodeConverter.asRange(hint.range),
renderOptions: {
after: {
contentText: `: ${hint.label}`,
@ -119,9 +109,9 @@ class HintsUpdater {
this.setTypeDecorations(editor, newTypeDecorations);
const newParameterDecorations = newHints
.filter(hint => hint.kind === 'ParameterHint')
.filter(hint => hint.kind === ra.InlayKind.ParameterHint)
.map(hint => ({
range: hint.range,
range: this.ctx.client.protocol2CodeConverter.asRange(hint.range),
renderOptions: {
before: {
contentText: `${hint.label}: `,
@ -151,20 +141,15 @@ class HintsUpdater {
);
}
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
private async queryHints(documentUri: string): Promise<ra.InlayHint[] | null> {
this.pending.get(documentUri)?.cancel();
const tokenSource = new vscode.CancellationTokenSource();
this.pending.set(documentUri, tokenSource);
const request: InlayHintsParams = { textDocument: { uri: documentUri } };
const request = { textDocument: { uri: documentUri } };
return sendRequestWithRetry<InlayHint[]>(
this.ctx.client,
'rust-analyzer/inlayHints',
request,
tokenSource.token
)
return sendRequestWithRetry(this.ctx.client, ra.inlayHints, request, tokenSource.token)
.catch(_ => null)
.finally(() => {
if (!tokenSource.token.isCancellationRequested) {

View file

@ -20,21 +20,21 @@ export const log = {
}
};
export async function sendRequestWithRetry<R>(
export async function sendRequestWithRetry<TParam, TRet>(
client: lc.LanguageClient,
method: string,
param: unknown,
reqType: lc.RequestType<TParam, TRet, unknown>,
param: TParam,
token?: vscode.CancellationToken,
): Promise<R> {
): Promise<TRet> {
for (const delay of [2, 4, 6, 8, 10, null]) {
try {
return await (token
? client.sendRequest(method, param, token)
: client.sendRequest(method, param)
? client.sendRequest(reqType, param, token)
: client.sendRequest(reqType, param)
);
} catch (error) {
if (delay === null) {
log.error("LSP request timed out", { method, param, error });
log.error("LSP request timed out", { method: reqType.method, param, error });
throw error;
}
@ -43,7 +43,7 @@ export async function sendRequestWithRetry<R>(
}
if (error.code !== lc.ErrorCodes.ContentModified) {
log.error("LSP request failed", { method, param, error });
log.error("LSP request failed", { method: reqType.method, param, error });
throw error;
}