diff options
author | Kevin DeLorey <[email protected]> | 2020-02-09 16:25:47 +0000 |
---|---|---|
committer | Kevin DeLorey <[email protected]> | 2020-02-09 16:37:43 +0000 |
commit | a957c473fdb79880c39b73dc9e0c923093cf16ac (patch) | |
tree | f998b548f530ce604651e0e6af314ed2ec74b3b5 /editors/code/src/ctx.ts | |
parent | 22caf982b99c54058e2e9200aeea0e61cada284a (diff) | |
parent | 1b9b13b4b4a75b5531c3f046ce6bf72d681f2732 (diff) |
Merge branch 'master' into kdelorey/complete-trait-impl
Diffstat (limited to 'editors/code/src/ctx.ts')
-rw-r--r-- | editors/code/src/ctx.ts | 48 |
1 files changed, 20 insertions, 28 deletions
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index a2a4e42a9..70042a479 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts | |||
@@ -1,5 +1,6 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 2 | import * as lc from 'vscode-languageclient'; |
3 | |||
3 | import { Config } from './config'; | 4 | import { Config } from './config'; |
4 | import { createClient } from './client'; | 5 | import { createClient } from './client'; |
5 | 6 | ||
@@ -10,6 +11,9 @@ export class Ctx { | |||
10 | // deal with it. | 11 | // deal with it. |
11 | // | 12 | // |
12 | // Ideally, this should be replaced with async getter though. | 13 | // Ideally, this should be replaced with async getter though. |
14 | // FIXME: this actually needs syncronization of some kind (check how | ||
15 | // vscode deals with `deactivate()` call when extension has some work scheduled | ||
16 | // on the event loop to get a better picture of what we can do here) | ||
13 | client: lc.LanguageClient | null = null; | 17 | client: lc.LanguageClient | null = null; |
14 | private extCtx: vscode.ExtensionContext; | 18 | private extCtx: vscode.ExtensionContext; |
15 | private onDidRestartHooks: Array<(client: lc.LanguageClient) => void> = []; | 19 | private onDidRestartHooks: Array<(client: lc.LanguageClient) => void> = []; |
@@ -20,12 +24,19 @@ export class Ctx { | |||
20 | } | 24 | } |
21 | 25 | ||
22 | async restartServer() { | 26 | async restartServer() { |
23 | let old = this.client; | 27 | const old = this.client; |
24 | if (old) { | 28 | if (old) { |
25 | await old.stop(); | 29 | await old.stop(); |
26 | } | 30 | } |
27 | this.client = null; | 31 | this.client = null; |
28 | const client = createClient(this.config); | 32 | const client = await createClient(this.config); |
33 | if (!client) { | ||
34 | throw new Error( | ||
35 | "Rust Analyzer Language Server is not available. " + | ||
36 | "Please, ensure its [proper installation](https://github.com/rust-analyzer/rust-analyzer/tree/master/docs/user#vs-code)." | ||
37 | ); | ||
38 | } | ||
39 | |||
29 | this.pushCleanup(client.start()); | 40 | this.pushCleanup(client.start()); |
30 | await client.onReady(); | 41 | await client.onReady(); |
31 | 42 | ||
@@ -49,33 +60,11 @@ export class Ctx { | |||
49 | this.pushCleanup(d); | 60 | this.pushCleanup(d); |
50 | } | 61 | } |
51 | 62 | ||
52 | overrideCommand(name: string, factory: (ctx: Ctx) => Cmd) { | 63 | get subscriptions(): Disposable[] { |
53 | const defaultCmd = `default:${name}`; | ||
54 | const override = factory(this); | ||
55 | const original = (...args: any[]) => | ||
56 | vscode.commands.executeCommand(defaultCmd, ...args); | ||
57 | try { | ||
58 | const d = vscode.commands.registerCommand( | ||
59 | name, | ||
60 | async (...args: any[]) => { | ||
61 | if (!(await override(...args))) { | ||
62 | return await original(...args); | ||
63 | } | ||
64 | }, | ||
65 | ); | ||
66 | this.pushCleanup(d); | ||
67 | } catch (_) { | ||
68 | vscode.window.showWarningMessage( | ||
69 | 'Enhanced typing feature is disabled because of incompatibility with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings', | ||
70 | ); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | get subscriptions(): { dispose(): any }[] { | ||
75 | return this.extCtx.subscriptions; | 64 | return this.extCtx.subscriptions; |
76 | } | 65 | } |
77 | 66 | ||
78 | pushCleanup(d: { dispose(): any }) { | 67 | pushCleanup(d: Disposable) { |
79 | this.extCtx.subscriptions.push(d); | 68 | this.extCtx.subscriptions.push(d); |
80 | } | 69 | } |
81 | 70 | ||
@@ -84,12 +73,15 @@ export class Ctx { | |||
84 | } | 73 | } |
85 | } | 74 | } |
86 | 75 | ||
87 | export type Cmd = (...args: any[]) => any; | 76 | export interface Disposable { |
77 | dispose(): void; | ||
78 | } | ||
79 | export type Cmd = (...args: any[]) => unknown; | ||
88 | 80 | ||
89 | export async function sendRequestWithRetry<R>( | 81 | export async function sendRequestWithRetry<R>( |
90 | client: lc.LanguageClient, | 82 | client: lc.LanguageClient, |
91 | method: string, | 83 | method: string, |
92 | param: any, | 84 | param: unknown, |
93 | token?: vscode.CancellationToken, | 85 | token?: vscode.CancellationToken, |
94 | ): Promise<R> { | 86 | ): Promise<R> { |
95 | for (const delay of [2, 4, 6, 8, 10, null]) { | 87 | for (const delay of [2, 4, 6, 8, 10, null]) { |