From a781a58fe2cefefbf9bf505247df78fd750a8f13 Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 31 Mar 2020 09:05:22 +0100 Subject: Throw error if no folder is opened --- editors/code/src/client.ts | 4 ++-- editors/code/src/ctx.ts | 2 +- editors/code/src/main.ts | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) (limited to 'editors') diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index c9819e457..0de45bb30 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts @@ -30,14 +30,14 @@ export function configToServerOptions(config: Config) { }; } -export async function createClient(config: Config, serverPath: string, workspaceFolder: vscode.WorkspaceFolder | null): Promise { +export async function createClient(config: Config, serverPath: string, workspaceFolder: vscode.WorkspaceFolder): 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 run: lc.Executable = { command: serverPath, - options: { cwd: workspaceFolder?.uri.fsPath ?? '.' }, + options: { cwd: workspaceFolder.uri.fsPath }, }; const serverOptions: lc.ServerOptions = { run, diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 0e705bc84..255d57f5e 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -19,7 +19,7 @@ export class Ctx { config: Config, extCtx: vscode.ExtensionContext, serverPath: string, - workspaceFolder: vscode.WorkspaceFolder | null, + workspaceFolder: vscode.WorkspaceFolder, ): Promise { const client = await createClient(config, serverPath, workspaceFolder); const res = new Ctx(config, extCtx, client, serverPath); diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 40701e4f5..ee6e712a4 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -42,7 +42,12 @@ export async function activate(context: vscode.ExtensionContext) { const state = new PersistentState(context.globalState); const serverPath = await bootstrap(config, state); - const workspaceFolder = vscode.workspace.workspaceFolders?.[0] ?? null; + const workspaceFolder = vscode.workspace.workspaceFolders?.[0]; + if (workspaceFolder === undefined) { + const err = "Cannot activate rust-analyzer when no folder is opened"; + void vscode.window.showErrorMessage(err); + throw new Error(err); + } // Note: we try to start the server before we activate type hints so that it // registers its `onDidChangeDocument` handler before us. -- cgit v1.2.3