diff options
author | Tim <[email protected]> | 2020-03-30 18:12:22 +0100 |
---|---|---|
committer | Tim Hutt <[email protected]> | 2020-03-30 21:23:21 +0100 |
commit | 768aa4259fce15f313042892739ed4d8b7e518b4 (patch) | |
tree | f50618675b04ea7d29490fc942fb6cde6b3c161a /editors/code | |
parent | 671926ac93f0ff921758a919eaf87c056979189f (diff) |
Add basic task support
This adds basic support for running `cargo build`, `cargo run`, etc.
Diffstat (limited to 'editors/code')
-rw-r--r-- | editors/code/src/client.ts | 5 | ||||
-rw-r--r-- | editors/code/src/ctx.ts | 9 | ||||
-rw-r--r-- | editors/code/src/main.ts | 9 | ||||
-rw-r--r-- | editors/code/src/tasks.ts | 60 |
4 files changed, 77 insertions, 6 deletions
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index d72ecc58f..c9819e457 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 | ||
33 | export async function createClient(config: Config, serverPath: string): Promise<lc.LanguageClient> { | 33 | export async function createClient(config: Config, serverPath: string, workspaceFolder: vscode.WorkspaceFolder | null): 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: workspaceFolder?.uri.fsPath ?? '.' }, |
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..0e705bc84 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 | workspaceFolder: vscode.WorkspaceFolder | null, | ||
23 | ): Promise<Ctx> { | ||
24 | const client = await createClient(config, serverPath, workspaceFolder); | ||
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..40701e4f5 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -13,6 +13,7 @@ import { log, assert } from './util'; | |||
13 | import { PersistentState } from './persistent_state'; | 13 | import { PersistentState } from './persistent_state'; |
14 | import { fetchRelease, download } from './net'; | 14 | import { fetchRelease, download } from './net'; |
15 | import { spawnSync } from 'child_process'; | 15 | import { spawnSync } from 'child_process'; |
16 | import { activateTaskProvider } from './tasks'; | ||
16 | 17 | ||
17 | let ctx: Ctx | undefined; | 18 | let ctx: Ctx | undefined; |
18 | 19 | ||
@@ -41,11 +42,13 @@ 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] ?? null; | ||
46 | |||
44 | // Note: we try to start the server before we activate type hints so that it | 47 | // Note: we try to start the server before we activate type hints so that it |
45 | // registers its `onDidChangeDocument` handler before us. | 48 | // registers its `onDidChangeDocument` handler before us. |
46 | // | 49 | // |
47 | // This a horribly, horribly wrong way to deal with this problem. | 50 | // This a horribly, horribly wrong way to deal with this problem. |
48 | ctx = await Ctx.create(config, context, serverPath); | 51 | ctx = await Ctx.create(config, context, serverPath, workspaceFolder); |
49 | 52 | ||
50 | // Commands which invokes manually via command palette, shortcut, etc. | 53 | // Commands which invokes manually via command palette, shortcut, etc. |
51 | 54 | ||
@@ -85,6 +88,10 @@ export async function activate(context: vscode.ExtensionContext) { | |||
85 | ctx.registerCommand('applySourceChange', commands.applySourceChange); | 88 | ctx.registerCommand('applySourceChange', commands.applySourceChange); |
86 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); | 89 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); |
87 | 90 | ||
91 | if (workspaceFolder !== null) { | ||
92 | ctx.pushCleanup(activateTaskProvider(workspaceFolder)); | ||
93 | } | ||
94 | |||
88 | activateStatusDisplay(ctx); | 95 | activateStatusDisplay(ctx); |
89 | 96 | ||
90 | if (!ctx.config.highlightingSemanticTokens) { | 97 | if (!ctx.config.highlightingSemanticTokens) { |
diff --git a/editors/code/src/tasks.ts b/editors/code/src/tasks.ts new file mode 100644 index 000000000..be036b872 --- /dev/null +++ b/editors/code/src/tasks.ts | |||
@@ -0,0 +1,60 @@ | |||
1 | import { | ||
2 | Disposable, | ||
3 | ShellExecution, | ||
4 | Task, | ||
5 | TaskGroup, | ||
6 | TaskProvider, | ||
7 | tasks, | ||
8 | WorkspaceFolder, | ||
9 | } from 'vscode'; | ||
10 | |||
11 | // This ends up as the `type` key in tasks.json. RLS also uses `cargo` and | ||
12 | // our configuration should be compatible with it so use the same key. | ||
13 | const TASK_TYPE = 'cargo'; | ||
14 | |||
15 | export function activateTaskProvider(target: WorkspaceFolder): Disposable { | ||
16 | const provider: TaskProvider = { | ||
17 | // Detect Rust tasks. Currently we do not do any actual detection | ||
18 | // of tasks (e.g. aliases in .cargo/config) and just return a fixed | ||
19 | // set of tasks that always exist. These tasks cannot be removed in | ||
20 | // tasks.json - only tweaked. | ||
21 | provideTasks: () => getStandardCargoTasks(target), | ||
22 | |||
23 | // We don't need to implement this. | ||
24 | resolveTask: () => undefined, | ||
25 | }; | ||
26 | |||
27 | return tasks.registerTaskProvider(TASK_TYPE, provider); | ||
28 | } | ||
29 | |||
30 | function getStandardCargoTasks(target: WorkspaceFolder): Task[] { | ||
31 | return [ | ||
32 | { command: 'build', group: TaskGroup.Build }, | ||
33 | { command: 'check', group: TaskGroup.Build }, | ||
34 | { command: 'test', group: TaskGroup.Test }, | ||
35 | { command: 'clean', group: TaskGroup.Clean }, | ||
36 | { command: 'run', group: undefined }, | ||
37 | ] | ||
38 | .map(({ command, group }) => { | ||
39 | const vscodeTask = new Task( | ||
40 | // The contents of this object end up in the tasks.json entries. | ||
41 | { | ||
42 | type: TASK_TYPE, | ||
43 | command, | ||
44 | }, | ||
45 | // The scope of the task - workspace or specific folder (global | ||
46 | // is not supported). | ||
47 | target, | ||
48 | // The task name, and task source. These are shown in the UI as | ||
49 | // `${source}: ${name}`, e.g. `rust: cargo build`. | ||
50 | `cargo ${command}`, | ||
51 | 'rust', | ||
52 | // What to do when this command is executed. | ||
53 | new ShellExecution('cargo', [command]), | ||
54 | // Problem matchers. | ||
55 | ['$rustc'], | ||
56 | ); | ||
57 | vscodeTask.group = group; | ||
58 | return vscodeTask; | ||
59 | }); | ||
60 | } | ||