Skip to content

Commit 4fea580

Browse files
bors[bot]matklad
andauthored
Merge #3191
3191: Remove two stage constuction r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 6167101 + 3717b0e commit 4fea580

File tree

7 files changed

+26
-51
lines changed

7 files changed

+26
-51
lines changed

editors/code/src/client.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,14 @@ import * as lc from 'vscode-languageclient';
22
import * as vscode from 'vscode';
33

44
import { Config } from './config';
5-
import { ensureServerBinary } from './installation/server';
65
import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed';
76

8-
export async function createClient(config: Config): Promise<null | lc.LanguageClient> {
7+
export async function createClient(config: Config, serverPath: string): Promise<lc.LanguageClient> {
98
// '.' Is the fallback if no folder is open
109
// TODO?: Workspace folders support Uri's (eg: file://test.txt).
1110
// It might be a good idea to test if the uri points to a file.
1211
const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.';
1312

14-
const serverPath = await ensureServerBinary(config.serverSource);
15-
if (!serverPath) return null;
16-
1713
const run: lc.Executable = {
1814
command: serverPath,
1915
options: { cwd: workspaceFolderPath },

editors/code/src/commands/analyzer_status.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,10 @@ export function analyzerStatus(ctx: Ctx): Cmd {
3737

3838
class TextDocumentContentProvider
3939
implements vscode.TextDocumentContentProvider {
40-
private ctx: Ctx;
4140
uri = vscode.Uri.parse('rust-analyzer-status://status');
4241
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
4342

44-
constructor(ctx: Ctx) {
45-
this.ctx = ctx;
43+
constructor(private readonly ctx: Ctx) {
4644
}
4745

4846
provideTextDocumentContent(

editors/code/src/commands/expand_macro.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,10 @@ function code_format(expanded: ExpandedMacro): string {
4242

4343
class TextDocumentContentProvider
4444
implements vscode.TextDocumentContentProvider {
45-
private ctx: Ctx;
4645
uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs');
4746
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
4847

49-
constructor(ctx: Ctx) {
50-
this.ctx = ctx;
48+
constructor(private readonly ctx: Ctx) {
5149
}
5250

5351
async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> {

editors/code/src/commands/syntax_tree.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ interface SyntaxTreeParams {
6868

6969
class TextDocumentContentProvider
7070
implements vscode.TextDocumentContentProvider {
71-
private ctx: Ctx;
7271
uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
7372
eventEmitter = new vscode.EventEmitter<vscode.Uri>();
7473

75-
constructor(ctx: Ctx) {
76-
this.ctx = ctx;
74+
constructor(private readonly ctx: Ctx) {
7775
}
7876

7977
provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> {

editors/code/src/ctx.ts

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,24 @@
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';
76

87
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+
) {
2013

21-
constructor(extCtx: vscode.ExtensionContext) {
22-
this.config = new Config(extCtx);
23-
this.extCtx = extCtx;
2414
}
2515

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());
3820
await client.onReady();
39-
40-
this.client = client;
21+
return res;
4122
}
4223

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

editors/code/src/inlay_hints.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@ class HintsUpdater {
9292

9393
async refresh() {
9494
if (!this.enabled) return;
95-
console.log("freshin!");
96-
9795
await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
9896
}
9997

editors/code/src/main.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,27 @@ import { activateInlayHints } from './inlay_hints';
55
import { activateStatusDisplay } from './status_display';
66
import { Ctx } from './ctx';
77
import { activateHighlighting } from './highlighting';
8+
import { ensureServerBinary } from './installation/server';
9+
import { Config } from './config';
810

911
let ctx: Ctx | undefined;
1012

1113
export async function activate(context: vscode.ExtensionContext) {
12-
ctx = new Ctx(context);
14+
const config = new Config(context)
15+
16+
const serverPath = await ensureServerBinary(config.serverSource);
17+
if (serverPath == null) {
18+
throw new Error(
19+
"Rust Analyzer Language Server is not available. " +
20+
"Please, ensure its [proper installation](https://rust-analyzer.github.io/manual.html#installation)."
21+
);
22+
}
1323

1424
// Note: we try to start the server before we activate type hints so that it
1525
// registers its `onDidChangeDocument` handler before us.
1626
//
1727
// This a horribly, horribly wrong way to deal with this problem.
18-
try {
19-
await ctx.startServer();
20-
} catch (e) {
21-
vscode.window.showErrorMessage(e.message);
22-
}
28+
ctx = await Ctx.create(config, context, serverPath);
2329

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

0 commit comments

Comments
 (0)