|
1 | 1 | import * as vscode from 'vscode'; |
2 | 2 | import * as lc from 'vscode-languageclient'; |
3 | | -import { strict as assert } from "assert"; |
4 | 3 |
|
5 | 4 | import { Config } from './config'; |
6 | 5 | import { createClient } from './client'; |
7 | 6 |
|
8 | 7 | export class Ctx { |
9 | | - readonly config: Config; |
10 | | - // Because we have "reload server" action, various listeners **will** face a |
11 | | - // situation where the client is not ready yet, and should be prepared to |
12 | | - // deal with it. |
13 | | - // |
14 | | - // Ideally, this should be replaced with async getter though. |
15 | | - // FIXME: this actually needs syncronization of some kind (check how |
16 | | - // vscode deals with `deactivate()` call when extension has some work scheduled |
17 | | - // on the event loop to get a better picture of what we can do here) |
18 | | - client: lc.LanguageClient | null = null; |
19 | | - private extCtx: vscode.ExtensionContext; |
| 8 | + private constructor( |
| 9 | + readonly config: Config, |
| 10 | + private readonly extCtx: vscode.ExtensionContext, |
| 11 | + readonly client: lc.LanguageClient |
| 12 | + ) { |
20 | 13 |
|
21 | | - constructor(extCtx: vscode.ExtensionContext) { |
22 | | - this.config = new Config(extCtx); |
23 | | - this.extCtx = extCtx; |
24 | 14 | } |
25 | 15 |
|
26 | | - async startServer() { |
27 | | - assert(this.client == null); |
28 | | - |
29 | | - const client = await createClient(this.config); |
30 | | - if (!client) { |
31 | | - throw new Error( |
32 | | - "Rust Analyzer Language Server is not available. " + |
33 | | - "Please, ensure its [proper installation](https://github.com/rust-analyzer/rust-analyzer/tree/master/docs/user#vs-code)." |
34 | | - ); |
35 | | - } |
36 | | - |
37 | | - this.pushCleanup(client.start()); |
| 16 | + static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> { |
| 17 | + const client = await createClient(config, serverPath); |
| 18 | + const res = new Ctx(config, extCtx, client); |
| 19 | + res.pushCleanup(client.start()); |
38 | 20 | await client.onReady(); |
39 | | - |
40 | | - this.client = client; |
| 21 | + return res; |
41 | 22 | } |
42 | 23 |
|
43 | 24 | get activeRustEditor(): vscode.TextEditor | undefined { |
|
0 commit comments