diff options
| author | Veetaha <gerzoh1@gmail.com> | 2020-02-17 01:47:14 +0200 |
|---|---|---|
| committer | Veetaha <gerzoh1@gmail.com> | 2020-02-17 02:07:11 +0200 |
| commit | bd113623a02dc253549464667af8931e2ff378bc (patch) | |
| tree | a3fabd75ef0e1b5b60381d62f761c94daf2d1299 /editors/code/src | |
| parent | 31ae64644864257b2375167df56c0b2e3839a9fd (diff) | |
| download | rust-bd113623a02dc253549464667af8931e2ff378bc.tar.gz rust-bd113623a02dc253549464667af8931e2ff378bc.zip | |
vscode: minor refactorings
Diffstat (limited to 'editors/code/src')
| -rw-r--r-- | editors/code/src/config.ts | 1 | ||||
| -rw-r--r-- | editors/code/src/ctx.ts | 12 | ||||
| -rw-r--r-- | editors/code/src/inlay_hints.ts | 45 |
3 files changed, 28 insertions, 30 deletions
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 70cb0a612a0..53e2a414b05 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -44,7 +44,6 @@ export class Config { this.refreshConfig(); } - private refreshConfig() { this.cfg = vscode.workspace.getConfiguration(Config.rootSection); console.log("Using configuration:", this.cfg); diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 9fcf2ec3826..ff6245f7899 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -91,15 +91,11 @@ export async function sendRequestWithRetry<R>( for (const delay of [2, 4, 6, 8, 10, null]) { try { return await (token ? client.sendRequest(method, param, token) : client.sendRequest(method, param)); - } catch (e) { - if ( - e.code === lc.ErrorCodes.ContentModified && - delay !== null - ) { - await sleep(10 * (1 << delay)); - continue; + } catch (err) { + if (delay === null || err.code !== lc.ErrorCodes.ContentModified) { + throw err; } - throw e; + await sleep(10 * (1 << delay)); } } throw 'unreachable'; diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index 12d7ddf0d12..3896878cda5 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -38,7 +38,7 @@ interface InlayHintsParams { interface InlayHint { range: vscode.Range; - kind: string; + kind: "TypeHint" | "ParameterHint"; label: string; } @@ -55,7 +55,7 @@ const parameterHintDecorationType = vscode.window.createTextEditorDecorationType }); class HintsUpdater { - private pending: Map<string, vscode.CancellationTokenSource> = new Map(); + private pending = new Map<string, vscode.CancellationTokenSource>(); private ctx: Ctx; private enabled: boolean; @@ -64,30 +64,36 @@ class HintsUpdater { this.enabled = ctx.config.displayInlayHints; } - async setEnabled(enabled: boolean) { + async setEnabled(enabled: boolean): Promise<void> { if (this.enabled == enabled) return; this.enabled = enabled; if (this.enabled) { - await this.refresh(); - } else { - this.allEditors.forEach(it => { - this.setTypeDecorations(it, []); - this.setParameterDecorations(it, []); - }); + return await this.refresh(); } + this.allEditors.forEach(it => { + this.setTypeDecorations(it, []); + this.setParameterDecorations(it, []); + }); } async refresh() { if (!this.enabled) return; - const promises = this.allEditors.map(it => this.refreshEditor(it)); - await Promise.all(promises); + await Promise.all(this.allEditors.map(it => this.refreshEditor(it))); + } + + private get allEditors(): vscode.TextEditor[] { + return vscode.window.visibleTextEditors.filter( + editor => editor.document.languageId === 'rust', + ); } private async refreshEditor(editor: vscode.TextEditor): Promise<void> { const newHints = await this.queryHints(editor.document.uri.toString()); if (newHints == null) return; - const newTypeDecorations = newHints.filter(hint => hint.kind === 'TypeHint') + + const newTypeDecorations = newHints + .filter(hint => hint.kind === 'TypeHint') .map(hint => ({ range: hint.range, renderOptions: { @@ -98,7 +104,8 @@ class HintsUpdater { })); this.setTypeDecorations(editor, newTypeDecorations); - const newParameterDecorations = newHints.filter(hint => hint.kind === 'ParameterHint') + const newParameterDecorations = newHints + .filter(hint => hint.kind === 'ParameterHint') .map(hint => ({ range: hint.range, renderOptions: { @@ -110,12 +117,6 @@ class HintsUpdater { this.setParameterDecorations(editor, newParameterDecorations); } - private get allEditors(): vscode.TextEditor[] { - return vscode.window.visibleTextEditors.filter( - editor => editor.document.languageId === 'rust', - ); - } - private setTypeDecorations( editor: vscode.TextEditor, decorations: vscode.DecorationOptions[], @@ -139,12 +140,14 @@ class HintsUpdater { private async queryHints(documentUri: string): Promise<InlayHint[] | null> { const client = this.ctx.client; if (!client) return null; + const request: InlayHintsParams = { textDocument: { uri: documentUri }, }; const tokenSource = new vscode.CancellationTokenSource(); - const prev = this.pending.get(documentUri); - if (prev) prev.cancel(); + const prevHintsRequest = this.pending.get(documentUri); + prevHintsRequest?.cancel(); + this.pending.set(documentUri, tokenSource); try { return await sendRequestWithRetry<InlayHint[] | null>( |
