From ee4e41cbea8ef3758ccebd9ffb35c5290aebfceb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 14:03:33 +0100 Subject: Push IO and error handling up --- editors/code/src/client.ts | 6 +----- editors/code/src/ctx.ts | 10 ++-------- editors/code/src/main.ts | 11 ++++++++++- 3 files changed, 13 insertions(+), 14 deletions(-) (limited to 'editors') 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'; import * as vscode from 'vscode'; import { Config } from './config'; -import { ensureServerBinary } from './installation/server'; import { CallHierarchyFeature } from 'vscode-languageclient/lib/callHierarchy.proposed'; -export async function createClient(config: Config): Promise { +export async function createClient(config: Config, serverPath: string): Promise { // '.' Is the fallback if no folder is open // TODO?: Workspace folders support Uri's (eg: file://test.txt). // It might be a good idea to test if the uri points to a file. const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.'; - const serverPath = await ensureServerBinary(config.serverSource); - if (!serverPath) return null; - const run: lc.Executable = { command: serverPath, options: { cwd: workspaceFolderPath }, diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index c06d8ac31..935a6f2b5 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -23,16 +23,10 @@ export class Ctx { this.extCtx = extCtx; } - async startServer() { + async startServer(serverPath: string) { assert(this.client == null); - const client = await createClient(this.config); - if (!client) { - throw new Error( - "Rust Analyzer Language Server is not available. " + - "Please, ensure its [proper installation](https://github.com/rust-analyzer/rust-analyzer/tree/master/docs/user#vs-code)." - ); - } + const client = await createClient(this.config, serverPath); this.pushCleanup(client.start()); await client.onReady(); diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 0bf2c4829..ece4883bd 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -5,18 +5,27 @@ import { activateInlayHints } from './inlay_hints'; import { activateStatusDisplay } from './status_display'; import { Ctx } from './ctx'; import { activateHighlighting } from './highlighting'; +import { ensureServerBinary } from './installation/server'; let ctx: Ctx | undefined; export async function activate(context: vscode.ExtensionContext) { ctx = new Ctx(context); + const serverPath = await ensureServerBinary(ctx.config.serverSource); + if (serverPath == null) { + throw new Error( + "Rust Analyzer Language Server is not available. " + + "Please, ensure its [proper installation](https://github.com/rust-analyzer/rust-analyzer/tree/master/docs/user#vs-code)." + ); + } + // Note: we try to start the server before we activate type hints so that it // registers its `onDidChangeDocument` handler before us. // // This a horribly, horribly wrong way to deal with this problem. try { - await ctx.startServer(); + await ctx.startServer(serverPath); } catch (e) { vscode.window.showErrorMessage(e.message); } -- cgit v1.2.3 From 978bea2b3110cf84defae37a56157b8b7016bd3d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 14:04:32 +0100 Subject: Fix link to the manual --- editors/code/src/main.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors') diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index ece4883bd..947d5eb90 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -16,7 +16,7 @@ export async function activate(context: vscode.ExtensionContext) { if (serverPath == null) { throw new Error( "Rust Analyzer Language Server is not available. " + - "Please, ensure its [proper installation](https://github.com/rust-analyzer/rust-analyzer/tree/master/docs/user#vs-code)." + "Please, ensure its [proper installation](https://rust-analyzer.github.io/manual.html#installation)." ); } -- cgit v1.2.3 From 89afb1a841c83d41ca3da72217c377932cdf5e93 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 14:11:01 +0100 Subject: Remove two stage constuction --- editors/code/src/ctx.ts | 25 +++++++++++-------------- editors/code/src/main.ts | 11 ++++------- 2 files changed, 15 insertions(+), 21 deletions(-) (limited to 'editors') diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 935a6f2b5..21f1025cf 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -1,6 +1,5 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; -import { strict as assert } from "assert"; import { Config } from './config'; import { createClient } from './client'; @@ -15,23 +14,21 @@ export class Ctx { // FIXME: this actually needs syncronization of some kind (check how // vscode deals with `deactivate()` call when extension has some work scheduled // on the event loop to get a better picture of what we can do here) - client: lc.LanguageClient | null = null; + client: lc.LanguageClient; private extCtx: vscode.ExtensionContext; - constructor(extCtx: vscode.ExtensionContext) { - this.config = new Config(extCtx); - this.extCtx = extCtx; - } - - async startServer(serverPath: string) { - assert(this.client == null); - - const client = await createClient(this.config, serverPath); - - this.pushCleanup(client.start()); + static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise { + const client = await createClient(config, serverPath); + const res = new Ctx(config, extCtx, client); + res.pushCleanup(client.start()); await client.onReady(); + return res; + } - this.client = client; + private constructor(config: Config, extCtx: vscode.ExtensionContext, client: lc.LanguageClient) { + this.config = config; + this.extCtx = extCtx; + this.client = client } get activeRustEditor(): vscode.TextEditor | undefined { diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 947d5eb90..0ad7ef1bb 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -6,13 +6,14 @@ import { activateStatusDisplay } from './status_display'; import { Ctx } from './ctx'; import { activateHighlighting } from './highlighting'; import { ensureServerBinary } from './installation/server'; +import { Config } from './config'; let ctx: Ctx | undefined; export async function activate(context: vscode.ExtensionContext) { - ctx = new Ctx(context); + const config = new Config(context) - const serverPath = await ensureServerBinary(ctx.config.serverSource); + const serverPath = await ensureServerBinary(config.serverSource); if (serverPath == null) { throw new Error( "Rust Analyzer Language Server is not available. " + @@ -24,11 +25,7 @@ export async function activate(context: vscode.ExtensionContext) { // registers its `onDidChangeDocument` handler before us. // // This a horribly, horribly wrong way to deal with this problem. - try { - await ctx.startServer(serverPath); - } catch (e) { - vscode.window.showErrorMessage(e.message); - } + ctx = await Ctx.create(config, context, serverPath); // Commands which invokes manually via command palette, shortcut, etc. ctx.registerCommand('reload', (ctx) => { -- cgit v1.2.3 From 7dccfd9183cc10acae7ead993979c3261fbe37ca Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 14:20:18 +0100 Subject: remove debug print --- editors/code/src/inlay_hints.ts | 2 -- 1 file changed, 2 deletions(-) (limited to 'editors') 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 { async refresh() { if (!this.enabled) return; - console.log("freshin!"); - await Promise.all(this.allEditors.map(it => this.refreshEditor(it))); } -- cgit v1.2.3 From 3c12cd49ecf8bdcd8c4ce82cb449836b7a02d4fb Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 14:21:50 +0100 Subject: Simplify ctor --- editors/code/src/ctx.ts | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) (limited to 'editors') diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 21f1025cf..dfc8aa7b9 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -5,17 +5,13 @@ import { Config } from './config'; import { createClient } from './client'; export class Ctx { - readonly config: Config; - // Because we have "reload server" action, various listeners **will** face a - // situation where the client is not ready yet, and should be prepared to - // deal with it. - // - // Ideally, this should be replaced with async getter though. - // FIXME: this actually needs syncronization of some kind (check how - // vscode deals with `deactivate()` call when extension has some work scheduled - // on the event loop to get a better picture of what we can do here) - client: lc.LanguageClient; - private extCtx: vscode.ExtensionContext; + private constructor( + readonly config: Config, + private readonly extCtx: vscode.ExtensionContext, + readonly client: lc.LanguageClient + ) { + + } static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise { const client = await createClient(config, serverPath); @@ -25,12 +21,6 @@ export class Ctx { return res; } - private constructor(config: Config, extCtx: vscode.ExtensionContext, client: lc.LanguageClient) { - this.config = config; - this.extCtx = extCtx; - this.client = client - } - get activeRustEditor(): vscode.TextEditor | undefined { const editor = vscode.window.activeTextEditor; return editor && editor.document.languageId === 'rust' -- cgit v1.2.3 From 3717b0e03f2336dcccea34c5a362b20966151b18 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 17 Feb 2020 14:23:23 +0100 Subject: Simplify some more ctors --- editors/code/src/commands/analyzer_status.ts | 4 +--- editors/code/src/commands/expand_macro.ts | 4 +--- editors/code/src/commands/syntax_tree.ts | 4 +--- 3 files changed, 3 insertions(+), 9 deletions(-) (limited to 'editors') 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 { class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { - private ctx: Ctx; uri = vscode.Uri.parse('rust-analyzer-status://status'); eventEmitter = new vscode.EventEmitter(); - constructor(ctx: Ctx) { - this.ctx = ctx; + constructor(private readonly ctx: Ctx) { } 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 { class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { - private ctx: Ctx; uri = vscode.Uri.parse('rust-analyzer://expandMacro/[EXPANSION].rs'); eventEmitter = new vscode.EventEmitter(); - constructor(ctx: Ctx) { - this.ctx = ctx; + constructor(private readonly ctx: Ctx) { } async provideTextDocumentContent(_uri: vscode.Uri): Promise { 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 { class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { - private ctx: Ctx; uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); eventEmitter = new vscode.EventEmitter(); - constructor(ctx: Ctx) { - this.ctx = ctx; + constructor(private readonly ctx: Ctx) { } provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult { -- cgit v1.2.3