Skip to content

Commit 89afb1a

Browse files
committed
Remove two stage constuction
1 parent 978bea2 commit 89afb1a

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

editors/code/src/ctx.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as vscode from 'vscode';
22
import * as lc from 'vscode-languageclient';
3-
import { strict as assert } from "assert";
43

54
import { Config } from './config';
65
import { createClient } from './client';
@@ -15,23 +14,21 @@ export class Ctx {
1514
// FIXME: this actually needs syncronization of some kind (check how
1615
// vscode deals with `deactivate()` call when extension has some work scheduled
1716
// on the event loop to get a better picture of what we can do here)
18-
client: lc.LanguageClient | null = null;
17+
client: lc.LanguageClient;
1918
private extCtx: vscode.ExtensionContext;
2019

21-
constructor(extCtx: vscode.ExtensionContext) {
22-
this.config = new Config(extCtx);
23-
this.extCtx = extCtx;
24-
}
25-
26-
async startServer(serverPath: string) {
27-
assert(this.client == null);
28-
29-
const client = await createClient(this.config, serverPath);
30-
31-
this.pushCleanup(client.start());
20+
static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
21+
const client = await createClient(config, serverPath);
22+
const res = new Ctx(config, extCtx, client);
23+
res.pushCleanup(client.start());
3224
await client.onReady();
25+
return res;
26+
}
3327

34-
this.client = client;
28+
private constructor(config: Config, extCtx: vscode.ExtensionContext, client: lc.LanguageClient) {
29+
this.config = config;
30+
this.extCtx = extCtx;
31+
this.client = client
3532
}
3633

3734
get activeRustEditor(): vscode.TextEditor | undefined {

editors/code/src/main.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ import { activateStatusDisplay } from './status_display';
66
import { Ctx } from './ctx';
77
import { activateHighlighting } from './highlighting';
88
import { ensureServerBinary } from './installation/server';
9+
import { Config } from './config';
910

1011
let ctx: Ctx | undefined;
1112

1213
export async function activate(context: vscode.ExtensionContext) {
13-
ctx = new Ctx(context);
14+
const config = new Config(context)
1415

15-
const serverPath = await ensureServerBinary(ctx.config.serverSource);
16+
const serverPath = await ensureServerBinary(config.serverSource);
1617
if (serverPath == null) {
1718
throw new Error(
1819
"Rust Analyzer Language Server is not available. " +
@@ -24,11 +25,7 @@ export async function activate(context: vscode.ExtensionContext) {
2425
// registers its `onDidChangeDocument` handler before us.
2526
//
2627
// This a horribly, horribly wrong way to deal with this problem.
27-
try {
28-
await ctx.startServer(serverPath);
29-
} catch (e) {
30-
vscode.window.showErrorMessage(e.message);
31-
}
28+
ctx = await Ctx.create(config, context, serverPath);
3229

3330
// Commands which invokes manually via command palette, shortcut, etc.
3431
ctx.registerCommand('reload', (ctx) => {

0 commit comments

Comments
 (0)