diff options
Diffstat (limited to 'editors/code/src/tasks.ts')
-rw-r--r-- | editors/code/src/tasks.ts | 54 |
1 files changed, 41 insertions, 13 deletions
diff --git a/editors/code/src/tasks.ts b/editors/code/src/tasks.ts index 9748824df..e2c43fdd4 100644 --- a/editors/code/src/tasks.ts +++ b/editors/code/src/tasks.ts | |||
@@ -1,11 +1,14 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as toolchain from "./toolchain"; | 2 | import * as toolchain from "./toolchain"; |
3 | import { Config } from './config'; | ||
4 | import { log } from './util'; | ||
3 | 5 | ||
4 | // This ends up as the `type` key in tasks.json. RLS also uses `cargo` and | 6 | // This ends up as the `type` key in tasks.json. RLS also uses `cargo` and |
5 | // our configuration should be compatible with it so use the same key. | 7 | // our configuration should be compatible with it so use the same key. |
6 | const TASK_TYPE = 'cargo'; | 8 | export const TASK_TYPE = 'cargo'; |
9 | export const TASK_SOURCE = 'rust'; | ||
7 | 10 | ||
8 | interface CargoTaskDefinition extends vscode.TaskDefinition { | 11 | export interface CargoTaskDefinition extends vscode.TaskDefinition { |
9 | command?: string; | 12 | command?: string; |
10 | args?: string[]; | 13 | args?: string[]; |
11 | cwd?: string; | 14 | cwd?: string; |
@@ -14,9 +17,11 @@ interface CargoTaskDefinition extends vscode.TaskDefinition { | |||
14 | 17 | ||
15 | class CargoTaskProvider implements vscode.TaskProvider { | 18 | class CargoTaskProvider implements vscode.TaskProvider { |
16 | private readonly target: vscode.WorkspaceFolder; | 19 | private readonly target: vscode.WorkspaceFolder; |
20 | private readonly config: Config; | ||
17 | 21 | ||
18 | constructor(target: vscode.WorkspaceFolder) { | 22 | constructor(target: vscode.WorkspaceFolder, config: Config) { |
19 | this.target = target; | 23 | this.target = target; |
24 | this.config = config; | ||
20 | } | 25 | } |
21 | 26 | ||
22 | provideTasks(): vscode.Task[] { | 27 | provideTasks(): vscode.Task[] { |
@@ -58,29 +63,52 @@ class CargoTaskProvider implements vscode.TaskProvider { | |||
58 | }); | 63 | }); |
59 | } | 64 | } |
60 | 65 | ||
61 | resolveTask(task: vscode.Task): vscode.Task | undefined { | 66 | async resolveTask(task: vscode.Task): Promise<vscode.Task | undefined> { |
62 | // VSCode calls this for every cargo task in the user's tasks.json, | 67 | // VSCode calls this for every cargo task in the user's tasks.json, |
63 | // we need to inform VSCode how to execute that command by creating | 68 | // we need to inform VSCode how to execute that command by creating |
64 | // a ShellExecution for it. | 69 | // a ShellExecution for it. |
65 | 70 | ||
66 | const definition = task.definition as CargoTaskDefinition; | 71 | const definition = task.definition as CargoTaskDefinition; |
67 | 72 | ||
68 | if (definition.type === 'cargo' && definition.command) { | 73 | if (definition.type === TASK_TYPE && definition.command) { |
69 | const args = [definition.command].concat(definition.args ?? []); | 74 | const args = [definition.command].concat(definition.args ?? []); |
70 | 75 | ||
71 | return new vscode.Task( | 76 | return await buildCargoTask(definition, task.name, args, this.config.cargoRunner); |
72 | definition, | ||
73 | task.name, | ||
74 | 'rust', | ||
75 | new vscode.ShellExecution('cargo', args, definition), | ||
76 | ); | ||
77 | } | 77 | } |
78 | 78 | ||
79 | return undefined; | 79 | return undefined; |
80 | } | 80 | } |
81 | } | 81 | } |
82 | 82 | ||
83 | export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable { | 83 | export async function buildCargoTask(definition: CargoTaskDefinition, name: string, args: string[], customRunner?: string): Promise<vscode.Task> { |
84 | const provider = new CargoTaskProvider(target); | 84 | if (customRunner) { |
85 | const runnerCommand = `${customRunner}.createCargoTask`; | ||
86 | try { | ||
87 | const runnerArgs = { name, args, cwd: definition.cwd, env: definition.env, source: TASK_SOURCE }; | ||
88 | const task = await vscode.commands.executeCommand(runnerCommand, runnerArgs); | ||
89 | |||
90 | if (task instanceof vscode.Task) { | ||
91 | return task; | ||
92 | } else if (task) { | ||
93 | log.debug("Invalid cargo task", task); | ||
94 | throw `Invalid task!`; | ||
95 | } | ||
96 | // fallback to default processing | ||
97 | |||
98 | } catch (e) { | ||
99 | throw `Cargo runner '${customRunner}' failed! ${e}`; | ||
100 | } | ||
101 | } | ||
102 | |||
103 | return new vscode.Task( | ||
104 | definition, | ||
105 | name, | ||
106 | TASK_SOURCE, | ||
107 | new vscode.ShellExecution(toolchain.cargoPath(), args, definition), | ||
108 | ); | ||
109 | } | ||
110 | |||
111 | export function activateTaskProvider(target: vscode.WorkspaceFolder, config: Config): vscode.Disposable { | ||
112 | const provider = new CargoTaskProvider(target, config); | ||
85 | return vscode.tasks.registerTaskProvider(TASK_TYPE, provider); | 113 | return vscode.tasks.registerTaskProvider(TASK_TYPE, provider); |
86 | } | 114 | } |