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 vscode from 'vscode';
import * as lc from 'vscode-languageclient'; import * as ra from './rust-analyzer-api';
import { Ctx } from './ctx'; import { Ctx } from './ctx';
import { log, sendRequestWithRetry } from './util'; import { log, sendRequestWithRetry } from './util';
@ -39,16 +39,6 @@ export function activateInlayHints(ctx: Ctx) {
void hintsUpdater.setEnabled(ctx.config.displayInlayHints); 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({ const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
after: { after: {
color: new vscode.ThemeColor('rust_analyzer.inlayHint'), color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
@ -107,9 +97,9 @@ class HintsUpdater {
if (newHints == null) return; if (newHints == null) return;
const newTypeDecorations = newHints const newTypeDecorations = newHints
.filter(hint => hint.kind === 'TypeHint') .filter(hint => hint.kind === ra.InlayKind.TypeHint)
.map(hint => ({ .map(hint => ({
range: hint.range, range: this.ctx.client.protocol2CodeConverter.asRange(hint.range),
renderOptions: { renderOptions: {
after: { after: {
contentText: `: ${hint.label}`, contentText: `: ${hint.label}`,
@ -119,9 +109,9 @@ class HintsUpdater {
this.setTypeDecorations(editor, newTypeDecorations); this.setTypeDecorations(editor, newTypeDecorations);
const newParameterDecorations = newHints const newParameterDecorations = newHints
.filter(hint => hint.kind === 'ParameterHint') .filter(hint => hint.kind === ra.InlayKind.ParameterHint)
.map(hint => ({ .map(hint => ({
range: hint.range, range: this.ctx.client.protocol2CodeConverter.asRange(hint.range),
renderOptions: { renderOptions: {
before: { before: {
contentText: `${hint.label}: `, 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(); this.pending.get(documentUri)?.cancel();
const tokenSource = new vscode.CancellationTokenSource(); const tokenSource = new vscode.CancellationTokenSource();
this.pending.set(documentUri, tokenSource); this.pending.set(documentUri, tokenSource);
const request: InlayHintsParams = { textDocument: { uri: documentUri } }; const request = { textDocument: { uri: documentUri } };
return sendRequestWithRetry<InlayHint[]>( return sendRequestWithRetry(this.ctx.client, ra.inlayHints, request, tokenSource.token)
this.ctx.client,
'rust-analyzer/inlayHints',
request,
tokenSource.token
)
.catch(_ => null) .catch(_ => null)
.finally(() => { .finally(() => {
if (!tokenSource.token.isCancellationRequested) { 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, client: lc.LanguageClient,
method: string, reqType: lc.RequestType<TParam, TRet, unknown>,
param: unknown, param: TParam,
token?: vscode.CancellationToken, token?: vscode.CancellationToken,
): Promise<R> { ): Promise<TRet> {
for (const delay of [2, 4, 6, 8, 10, null]) { for (const delay of [2, 4, 6, 8, 10, null]) {
try { try {
return await (token return await (token
? client.sendRequest(method, param, token) ? client.sendRequest(reqType, param, token)
: client.sendRequest(method, param) : client.sendRequest(reqType, param)
); );
} catch (error) { } catch (error) {
if (delay === null) { 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; throw error;
} }
@ -43,7 +43,7 @@ export async function sendRequestWithRetry<R>(
} }
if (error.code !== lc.ErrorCodes.ContentModified) { 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; throw error;
} }