aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-02-17 13:24:07 +0000
committerGitHub <[email protected]>2020-02-17 13:24:07 +0000
commit4fea5808e9209c385e915d9d060c18520ce081b3 (patch)
treeb1b3be960ba393200c6676fa096f8c1e12e64cc2 /editors/code/src
parent6167101302bcc2d7f1a345e0ee44e1411056b4b3 (diff)
parent3717b0e03f2336dcccea34c5a362b20966151b18 (diff)
Merge #3191
3191: Remove two stage constuction r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'editors/code/src')
-rw-r--r--editors/code/src/client.ts6
-rw-r--r--editors/code/src/commands/analyzer_status.ts4
-rw-r--r--editors/code/src/commands/expand_macro.ts4
-rw-r--r--editors/code/src/commands/syntax_tree.ts4
-rw-r--r--editors/code/src/ctx.ts39
-rw-r--r--editors/code/src/inlay_hints.ts2
-rw-r--r--editors/code/src/main.ts18
7 files changed, 26 insertions, 51 deletions
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts
index 11894973c..aaf2ef40e 100644
--- a/editors/code/src/client.ts
+++ b/editors/code/src/client.ts
@@ -2,18 +2,14 @@ import * as lc from 'vscode-languageclient';
2import * as vscode from 'vscode'; 2import * as vscode from 'vscode';
3 3
4import { Config } from './config'; 4import { Config } from './config';
5import { ensureServerBinary } from './installation/server';
6import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed'; 5import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed';
7 6
8export async function createClient(config: Config): Promise<null | lc.LanguageClient> { 7export async function createClient(config: Config, serverPath: string): Promise<lc.LanguageClient> {
9 // '.' Is the fallback if no folder is open 8 // '.' Is the fallback if no folder is open
10 // TODO?: Workspace folders support Uri's (eg: file://test.txt). 9 // TODO?: Workspace folders support Uri's (eg: file://test.txt).
11 // It might be a good idea to test if the uri points to a file. 10 // It might be a good idea to test if the uri points to a file.
12 const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.'; 11 const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.';
13 12
14 const serverPath = await ensureServerBinary(config.serverSource);
15 if (!serverPath) return null;
16
17 const run: lc.Executable = { 13 const run: lc.Executable = {
18 command: serverPath, 14 command: serverPath,
19 options: { cwd: workspaceFolderPath }, 15 options: { cwd: workspaceFolderPath },
diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts
index cfe7d1af0..6631e8db7 100644
--- a/editors/code/src/commands/analyzer_status.ts
+++ b/editors/code/src/commands/analyzer_status.ts
@@ -37,12 +37,10 @@ export function analyzerStatus(ctx: Ctx): Cmd {
37 37
38class TextDocumentContentProvider 38class TextDocumentContentProvider
39 implements vscode.TextDocumentContentProvider { 39 implements vscode.TextDocumentContentProvider {
40 private ctx: Ctx;
41 uri = vscode.Uri.parse('rust-analyzer-status://status'); 40 uri = vscode.Uri.parse('rust-analyzer-status://status');
42 eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 41 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
43 42
44 constructor(ctx: Ctx) { 43 constructor(private readonly ctx: Ctx) {
45 this.ctx = ctx;
46 } 44 }
47 45
48 provideTextDocumentContent( 46 provideTextDocumentContent(
diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts
index dcdde78af..6fee6eb41 100644
--- a/editors/code/src/commands/expand_macro.ts
+++ b/editors/code/src/commands/expand_macro.ts
@@ -42,12 +42,10 @@ function code_format(expanded: ExpandedMacro): string {
42 42
43class TextDocumentContentProvider 43class TextDocumentContentProvider
44 implements vscode.TextDocumentContentProvider { 44 implements vscode.TextDocumentContentProvider {
45 private ctx: Ctx;
46 uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs'); 45 uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs');
47 eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 46 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
48 47
49 constructor(ctx: Ctx) { 48 constructor(private readonly ctx: Ctx) {
50 this.ctx = ctx;
51 } 49 }
52 50
53 async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> { 51 async provideTextDocumentContent(_uri: vscode.Uri): Promise<string> {
diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts
index 7dde66ad1..2887c96c8 100644
--- a/editors/code/src/commands/syntax_tree.ts
+++ b/editors/code/src/commands/syntax_tree.ts
@@ -68,12 +68,10 @@ interface SyntaxTreeParams {
68 68
69class TextDocumentContentProvider 69class TextDocumentContentProvider
70 implements vscode.TextDocumentContentProvider { 70 implements vscode.TextDocumentContentProvider {
71 private ctx: Ctx;
72 uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); 71 uri = vscode.Uri.parse('rust-analyzer://syntaxtree');
73 eventEmitter = new vscode.EventEmitter<vscode.Uri>(); 72 eventEmitter = new vscode.EventEmitter<vscode.Uri>();
74 73
75 constructor(ctx: Ctx) { 74 constructor(private readonly ctx: Ctx) {
76 this.ctx = ctx;
77 } 75 }
78 76
79 provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> { 77 provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> {
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
index c06d8ac31..dfc8aa7b9 100644
--- a/editors/code/src/ctx.ts
+++ b/editors/code/src/ctx.ts
@@ -1,43 +1,24 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as lc from 'vscode-languageclient';
3import { strict as assert } from "assert";
4 3
5import { Config } from './config'; 4import { Config } from './config';
6import { createClient } from './client'; 5import { createClient } from './client';
7 6
8export class Ctx { 7export class Ctx {
9 readonly config: Config; 8 private constructor(
10 // Because we have "reload server" action, various listeners **will** face a 9 readonly config: Config,
11 // situation where the client is not ready yet, and should be prepared to 10 private readonly extCtx: vscode.ExtensionContext,
12 // deal with it. 11 readonly client: lc.LanguageClient
13 // 12 ) {
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;
20 13
21 constructor(extCtx: vscode.ExtensionContext) {
22 this.config = new Config(extCtx);
23 this.extCtx = extCtx;
24 } 14 }
25 15
26 async startServer() { 16 static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> {
27 assert(this.client == null); 17 const client = await createClient(config, serverPath);
28 18 const res = new Ctx(config, extCtx, client);
29 const client = await createClient(this.config); 19 res.pushCleanup(client.start());
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());
38 await client.onReady(); 20 await client.onReady();
39 21 return res;
40 this.client = client;
41 } 22 }
42 23
43 get activeRustEditor(): vscode.TextEditor | undefined { 24 get activeRustEditor(): vscode.TextEditor | undefined {
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts
index 55bbd7f00..f82df66ae 100644
--- a/editors/code/src/inlay_hints.ts
+++ b/editors/code/src/inlay_hints.ts
@@ -92,8 +92,6 @@ class HintsUpdater {
92 92
93 async refresh() { 93 async refresh() {
94 if (!this.enabled) return; 94 if (!this.enabled) return;
95 console.log("freshin!");
96
97 await Promise.all(this.allEditors.map(it => this.refreshEditor(it))); 95 await Promise.all(this.allEditors.map(it => this.refreshEditor(it)));
98 } 96 }
99 97
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index 0bf2c4829..0ad7ef1bb 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -5,21 +5,27 @@ import { activateInlayHints } from './inlay_hints';
5import { activateStatusDisplay } from './status_display'; 5import { activateStatusDisplay } from './status_display';
6import { Ctx } from './ctx'; 6import { Ctx } from './ctx';
7import { activateHighlighting } from './highlighting'; 7import { activateHighlighting } from './highlighting';
8import { ensureServerBinary } from './installation/server';
9import { Config } from './config';
8 10
9let ctx: Ctx | undefined; 11let ctx: Ctx | undefined;
10 12
11export async function activate(context: vscode.ExtensionContext) { 13export 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 }
13 23
14 // Note: we try to start the server before we activate type hints so that it 24 // Note: we try to start the server before we activate type hints so that it
15 // registers its `onDidChangeDocument` handler before us. 25 // registers its `onDidChangeDocument` handler before us.
16 // 26 //
17 // This a horribly, horribly wrong way to deal with this problem. 27 // This a horribly, horribly wrong way to deal with this problem.
18 try { 28 ctx = await Ctx.create(config, context, serverPath);
19 await ctx.startServer();
20 } catch (e) {
21 vscode.window.showErrorMessage(e.message);
22 }
23 29
24 // Commands which invokes manually via command palette, shortcut, etc. 30 // Commands which invokes manually via command palette, shortcut, etc.
25 ctx.registerCommand('reload', (ctx) => { 31 ctx.registerCommand('reload', (ctx) => {