From c9230b88b4852faf1dac59e05fd4f4d8c1f0dfb0 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:57:49 +0200 Subject: [PATCH] vscode: migrate inlay_hints to rust-analyzer-api.ts --- editors/code/src/inlay_hints.ts | 31 ++++++++----------------------- editors/code/src/util.ts | 16 ++++++++-------- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index 5f9229efbce..5951cf1b457 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -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 { + private async queryHints(documentUri: string): Promise { 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( - 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) { diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 2f18f85a392..68c2a94d045 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -20,21 +20,21 @@ export const log = { } }; -export async function sendRequestWithRetry( +export async function sendRequestWithRetry( client: lc.LanguageClient, - method: string, - param: unknown, + reqType: lc.RequestType, + param: TParam, token?: vscode.CancellationToken, -): Promise { +): Promise { 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( } 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; }