diff options
author | Veetaha <[email protected]> | 2020-02-24 22:57:49 +0000 |
---|---|---|
committer | Veetaha <[email protected]> | 2020-02-24 22:57:49 +0000 |
commit | c9230b88b4852faf1dac59e05fd4f4d8c1f0dfb0 (patch) | |
tree | 405af93a91f3cf9e1fa2d71315bc9392d41ecd26 | |
parent | 8aea0ec511d141b5c53d419960c688b13bb6c061 (diff) |
vscode: migrate inlay_hints to rust-analyzer-api.ts
-rw-r--r-- | editors/code/src/inlay_hints.ts | 31 | ||||
-rw-r--r-- | 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 5f9229efb..5951cf1b4 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 2 | import * as ra from './rust-analyzer-api'; |
3 | 3 | ||
4 | import { Ctx } from './ctx'; | 4 | import { Ctx } from './ctx'; |
5 | import { log, sendRequestWithRetry } from './util'; | 5 | import { log, sendRequestWithRetry } from './util'; |
@@ -39,16 +39,6 @@ export function activateInlayHints(ctx: Ctx) { | |||
39 | void hintsUpdater.setEnabled(ctx.config.displayInlayHints); | 39 | void hintsUpdater.setEnabled(ctx.config.displayInlayHints); |
40 | } | 40 | } |
41 | 41 | ||
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 | |||
52 | const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ | 42 | const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ |
53 | after: { | 43 | after: { |
54 | color: new vscode.ThemeColor('rust_analyzer.inlayHint'), | 44 | color: new vscode.ThemeColor('rust_analyzer.inlayHint'), |
@@ -107,9 +97,9 @@ class HintsUpdater { | |||
107 | if (newHints == null) return; | 97 | if (newHints == null) return; |
108 | 98 | ||
109 | const newTypeDecorations = newHints | 99 | const newTypeDecorations = newHints |
110 | .filter(hint => hint.kind === 'TypeHint') | 100 | .filter(hint => hint.kind === ra.InlayKind.TypeHint) |
111 | .map(hint => ({ | 101 | .map(hint => ({ |
112 | range: hint.range, | 102 | range: this.ctx.client.protocol2CodeConverter.asRange(hint.range), |
113 | renderOptions: { | 103 | renderOptions: { |
114 | after: { | 104 | after: { |
115 | contentText: `: ${hint.label}`, | 105 | contentText: `: ${hint.label}`, |
@@ -119,9 +109,9 @@ class HintsUpdater { | |||
119 | this.setTypeDecorations(editor, newTypeDecorations); | 109 | this.setTypeDecorations(editor, newTypeDecorations); |
120 | 110 | ||
121 | const newParameterDecorations = newHints | 111 | const newParameterDecorations = newHints |
122 | .filter(hint => hint.kind === 'ParameterHint') | 112 | .filter(hint => hint.kind === ra.InlayKind.ParameterHint) |
123 | .map(hint => ({ | 113 | .map(hint => ({ |
124 | range: hint.range, | 114 | range: this.ctx.client.protocol2CodeConverter.asRange(hint.range), |
125 | renderOptions: { | 115 | renderOptions: { |
126 | before: { | 116 | before: { |
127 | contentText: `${hint.label}: `, | 117 | contentText: `${hint.label}: `, |
@@ -151,20 +141,15 @@ class HintsUpdater { | |||
151 | ); | 141 | ); |
152 | } | 142 | } |
153 | 143 | ||
154 | private async queryHints(documentUri: string): Promise<InlayHint[] | null> { | 144 | private async queryHints(documentUri: string): Promise<ra.InlayHint[] | null> { |
155 | this.pending.get(documentUri)?.cancel(); | 145 | this.pending.get(documentUri)?.cancel(); |
156 | 146 | ||
157 | const tokenSource = new vscode.CancellationTokenSource(); | 147 | const tokenSource = new vscode.CancellationTokenSource(); |
158 | this.pending.set(documentUri, tokenSource); | 148 | this.pending.set(documentUri, tokenSource); |
159 | 149 | ||
160 | const request: InlayHintsParams = { textDocument: { uri: documentUri } }; | 150 | const request = { textDocument: { uri: documentUri } }; |
161 | 151 | ||
162 | return sendRequestWithRetry<InlayHint[]>( | 152 | return sendRequestWithRetry(this.ctx.client, ra.inlayHints, request, tokenSource.token) |
163 | this.ctx.client, | ||
164 | 'rust-analyzer/inlayHints', | ||
165 | request, | ||
166 | tokenSource.token | ||
167 | ) | ||
168 | .catch(_ => null) | 153 | .catch(_ => null) |
169 | .finally(() => { | 154 | .finally(() => { |
170 | if (!tokenSource.token.isCancellationRequested) { | 155 | if (!tokenSource.token.isCancellationRequested) { |
diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 2f18f85a3..68c2a94d0 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts | |||
@@ -20,21 +20,21 @@ export const log = { | |||
20 | } | 20 | } |
21 | }; | 21 | }; |
22 | 22 | ||
23 | export async function sendRequestWithRetry<R>( | 23 | export async function sendRequestWithRetry<TParam, TRet>( |
24 | client: lc.LanguageClient, | 24 | client: lc.LanguageClient, |
25 | method: string, | 25 | reqType: lc.RequestType<TParam, TRet, unknown>, |
26 | param: unknown, | 26 | param: TParam, |
27 | token?: vscode.CancellationToken, | 27 | token?: vscode.CancellationToken, |
28 | ): Promise<R> { | 28 | ): Promise<TRet> { |
29 | for (const delay of [2, 4, 6, 8, 10, null]) { | 29 | for (const delay of [2, 4, 6, 8, 10, null]) { |
30 | try { | 30 | try { |
31 | return await (token | 31 | return await (token |
32 | ? client.sendRequest(method, param, token) | 32 | ? client.sendRequest(reqType, param, token) |
33 | : client.sendRequest(method, param) | 33 | : client.sendRequest(reqType, param) |
34 | ); | 34 | ); |
35 | } catch (error) { | 35 | } catch (error) { |
36 | if (delay === null) { | 36 | 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 }); |
38 | throw error; | 38 | throw error; |
39 | } | 39 | } |
40 | 40 | ||
@@ -43,7 +43,7 @@ export async function sendRequestWithRetry<R>( | |||
43 | } | 43 | } |
44 | 44 | ||
45 | if (error.code !== lc.ErrorCodes.ContentModified) { | 45 | 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 }); |
47 | throw error; | 47 | throw error; |
48 | } | 48 | } |
49 | 49 | ||