Skip to content

Commit c9230b8

Browse files
author
Veetaha
committed
vscode: migrate inlay_hints to rust-analyzer-api.ts
1 parent 8aea0ec commit c9230b8

File tree

2 files changed

+16
-31
lines changed

2 files changed

+16
-31
lines changed

editors/code/src/inlay_hints.ts

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as vscode from 'vscode';
2-
import * as lc from 'vscode-languageclient';
2+
import * as ra from './rust-analyzer-api';
33

44
import { Ctx } from './ctx';
55
import { log, sendRequestWithRetry } from './util';
@@ -39,16 +39,6 @@ export function activateInlayHints(ctx: Ctx) {
3939
void hintsUpdater.setEnabled(ctx.config.displayInlayHints);
4040
}
4141

42-
interface InlayHintsParams {
43-
textDocument: lc.TextDocumentIdentifier;
44-
}
45-
46-
interface InlayHint {
47-
range: vscode.Range;
48-
kind: "TypeHint" | "ParameterHint";
49-
label: string;
50-
}
51-
5242
const typeHintDecorationType = vscode.window.createTextEditorDecorationType({
5343
after: {
5444
color: new vscode.ThemeColor('rust_analyzer.inlayHint'),
@@ -107,9 +97,9 @@ class HintsUpdater {
10797
if (newHints == null) return;
10898

10999
const newTypeDecorations = newHints
110-
.filter(hint => hint.kind === 'TypeHint')
100+
.filter(hint => hint.kind === ra.InlayKind.TypeHint)
111101
.map(hint => ({
112-
range: hint.range,
102+
range: this.ctx.client.protocol2CodeConverter.asRange(hint.range),
113103
renderOptions: {
114104
after: {
115105
contentText: `: ${hint.label}`,
@@ -119,9 +109,9 @@ class HintsUpdater {
119109
this.setTypeDecorations(editor, newTypeDecorations);
120110

121111
const newParameterDecorations = newHints
122-
.filter(hint => hint.kind === 'ParameterHint')
112+
.filter(hint => hint.kind === ra.InlayKind.ParameterHint)
123113
.map(hint => ({
124-
range: hint.range,
114+
range: this.ctx.client.protocol2CodeConverter.asRange(hint.range),
125115
renderOptions: {
126116
before: {
127117
contentText: `${hint.label}: `,
@@ -151,20 +141,15 @@ class HintsUpdater {
151141
);
152142
}
153143

154-
private async queryHints(documentUri: string): Promise<InlayHint[] | null> {
144+
private async queryHints(documentUri: string): Promise<ra.InlayHint[] | null> {
155145
this.pending.get(documentUri)?.cancel();
156146

157147
const tokenSource = new vscode.CancellationTokenSource();
158148
this.pending.set(documentUri, tokenSource);
159149

160-
const request: InlayHintsParams = { textDocument: { uri: documentUri } };
150+
const request = { textDocument: { uri: documentUri } };
161151

162-
return sendRequestWithRetry<InlayHint[]>(
163-
this.ctx.client,
164-
'rust-analyzer/inlayHints',
165-
request,
166-
tokenSource.token
167-
)
152+
return sendRequestWithRetry(this.ctx.client, ra.inlayHints, request, tokenSource.token)
168153
.catch(_ => null)
169154
.finally(() => {
170155
if (!tokenSource.token.isCancellationRequested) {

editors/code/src/util.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,21 @@ export const log = {
2020
}
2121
};
2222

23-
export async function sendRequestWithRetry<R>(
23+
export async function sendRequestWithRetry<TParam, TRet>(
2424
client: lc.LanguageClient,
25-
method: string,
26-
param: unknown,
25+
reqType: lc.RequestType<TParam, TRet, unknown>,
26+
param: TParam,
2727
token?: vscode.CancellationToken,
28-
): Promise<R> {
28+
): Promise<TRet> {
2929
for (const delay of [2, 4, 6, 8, 10, null]) {
3030
try {
3131
return await (token
32-
? client.sendRequest(method, param, token)
33-
: client.sendRequest(method, param)
32+
? client.sendRequest(reqType, param, token)
33+
: client.sendRequest(reqType, param)
3434
);
3535
} catch (error) {
3636
if (delay === null) {
37-
log.error("LSP request timed out", { method, param, error });
37+
log.error("LSP request timed out", { method: reqType.method, param, error });
3838
throw error;
3939
}
4040

@@ -43,7 +43,7 @@ export async function sendRequestWithRetry<R>(
4343
}
4444

4545
if (error.code !== lc.ErrorCodes.ContentModified) {
46-
log.error("LSP request failed", { method, param, error });
46+
log.error("LSP request failed", { method: reqType.method, param, error });
4747
throw error;
4848
}
4949

0 commit comments

Comments
 (0)