aboutsummaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
Diffstat (limited to 'editors')
-rw-r--r--editors/code/src/client.ts5
-rw-r--r--editors/code/src/ctx.ts9
-rw-r--r--editors/code/src/main.ts12
-rw-r--r--editors/code/src/tasks.ts52
4 files changed, 72 insertions, 6 deletions
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts
index d72ecc58f..f909f8db2 100644
--- a/editors/code/src/client.ts
+++ b/editors/code/src/client.ts
@@ -30,15 +30,14 @@ export function configToServerOptions(config: Config) {
30 }; 30 };
31} 31}
32 32
33export async function createClient(config: Config, serverPath: string): Promise<lc.LanguageClient> { 33export async function createClient(config: Config, serverPath: string, cwd: string): Promise<lc.LanguageClient> {
34 // '.' Is the fallback if no folder is open 34 // '.' Is the fallback if no folder is open
35 // TODO?: Workspace folders support Uri's (eg: file://test.txt). 35 // TODO?: Workspace folders support Uri's (eg: file://test.txt).
36 // It might be a good idea to test if the uri points to a file. 36 // It might be a good idea to test if the uri points to a file.
37 const workspaceFolderPath = vscode.workspace.workspaceFolders?.[0]?.uri.fsPath ?? '.';
38 37
39 const run: lc.Executable = { 38 const run: lc.Executable = {
40 command: serverPath, 39 command: serverPath,
41 options: { cwd: workspaceFolderPath }, 40 options: { cwd },
42 }; 41 };
43 const serverOptions: lc.ServerOptions = { 42 const serverOptions: lc.ServerOptions = {
44 run, 43 run,
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts
index d2f49cd23..86b5f3629 100644
--- a/editors/code/src/ctx.ts
+++ b/editors/code/src/ctx.ts
@@ -15,8 +15,13 @@ export class Ctx {
15 15
16 } 16 }
17 17
18 static async create(config: Config, extCtx: vscode.ExtensionContext, serverPath: string): Promise<Ctx> { 18 static async create(
19 const client = await createClient(config, serverPath); 19 config: Config,
20 extCtx: vscode.ExtensionContext,
21 serverPath: string,
22 cwd: string,
23 ): Promise<Ctx> {
24 const client = await createClient(config, serverPath, cwd);
20 const res = new Ctx(config, extCtx, client, serverPath); 25 const res = new Ctx(config, extCtx, client, serverPath);
21 res.pushCleanup(client.start()); 26 res.pushCleanup(client.start());
22 await client.onReady(); 27 await client.onReady();
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts
index a46dbde33..7ba16120c 100644
--- a/editors/code/src/main.ts
+++ b/editors/code/src/main.ts
@@ -13,6 +13,7 @@ import { log, assert } from './util';
13import { PersistentState } from './persistent_state'; 13import { PersistentState } from './persistent_state';
14import { fetchRelease, download } from './net'; 14import { fetchRelease, download } from './net';
15import { spawnSync } from 'child_process'; 15import { spawnSync } from 'child_process';
16import { activateTaskProvider } from './tasks';
16 17
17let ctx: Ctx | undefined; 18let ctx: Ctx | undefined;
18 19
@@ -41,11 +42,18 @@ export async function activate(context: vscode.ExtensionContext) {
41 const state = new PersistentState(context.globalState); 42 const state = new PersistentState(context.globalState);
42 const serverPath = await bootstrap(config, state); 43 const serverPath = await bootstrap(config, state);
43 44
45 const workspaceFolder = vscode.workspace.workspaceFolders?.[0];
46 if (workspaceFolder === undefined) {
47 const err = "Cannot activate rust-analyzer when no folder is opened";
48 void vscode.window.showErrorMessage(err);
49 throw new Error(err);
50 }
51
44 // Note: we try to start the server before we activate type hints so that it 52 // Note: we try to start the server before we activate type hints so that it
45 // registers its `onDidChangeDocument` handler before us. 53 // registers its `onDidChangeDocument` handler before us.
46 // 54 //
47 // This a horribly, horribly wrong way to deal with this problem. 55 // This a horribly, horribly wrong way to deal with this problem.
48 ctx = await Ctx.create(config, context, serverPath); 56 ctx = await Ctx.create(config, context, serverPath, workspaceFolder.uri.fsPath);
49 57
50 // Commands which invokes manually via command palette, shortcut, etc. 58 // Commands which invokes manually via command palette, shortcut, etc.
51 59
@@ -85,6 +93,8 @@ export async function activate(context: vscode.ExtensionContext) {
85 ctx.registerCommand('applySourceChange', commands.applySourceChange); 93 ctx.registerCommand('applySourceChange', commands.applySourceChange);
86 ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); 94 ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange);
87 95
96 ctx.pushCleanup(activateTaskProvider(workspaceFolder));
97
88 activateStatusDisplay(ctx); 98 activateStatusDisplay(ctx);
89 99
90 if (!ctx.config.highlightingSemanticTokens) { 100 if (!ctx.config.highlightingSemanticTokens) {
diff --git a/editors/code/src/tasks.ts b/editors/code/src/tasks.ts
new file mode 100644
index 000000000..fa1c4a951
--- /dev/null
+++ b/editors/code/src/tasks.ts
@@ -0,0 +1,52 @@
1import * as vscode from 'vscode';
2
3// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
4// our configuration should be compatible with it so use the same key.
5const TASK_TYPE = 'cargo';
6
7export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
8 const provider: vscode.TaskProvider = {
9 // Detect Rust tasks. Currently we do not do any actual detection
10 // of tasks (e.g. aliases in .cargo/config) and just return a fixed
11 // set of tasks that always exist. These tasks cannot be removed in
12 // tasks.json - only tweaked.
13 provideTasks: () => getStandardCargoTasks(target),
14
15 // We don't need to implement this.
16 resolveTask: () => undefined,
17 };
18
19 return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
20}
21
22function getStandardCargoTasks(target: vscode.WorkspaceFolder): vscode.Task[] {
23 return [
24 { command: 'build', group: vscode.TaskGroup.Build },
25 { command: 'check', group: vscode.TaskGroup.Build },
26 { command: 'test', group: vscode.TaskGroup.Test },
27 { command: 'clean', group: vscode.TaskGroup.Clean },
28 { command: 'run', group: undefined },
29 ]
30 .map(({ command, group }) => {
31 const vscodeTask = new vscode.Task(
32 // The contents of this object end up in the tasks.json entries.
33 {
34 type: TASK_TYPE,
35 command,
36 },
37 // The scope of the task - workspace or specific folder (global
38 // is not supported).
39 target,
40 // The task name, and task source. These are shown in the UI as
41 // `${source}: ${name}`, e.g. `rust: cargo build`.
42 `cargo ${command}`,
43 'rust',
44 // What to do when this command is executed.
45 new vscode.ShellExecution('cargo', [command]),
46 // Problem matchers.
47 ['$rustc'],
48 );
49 vscodeTask.group = group;
50 return vscodeTask;
51 });
52}