|
| 1 | +import * 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. |
| 5 | +const TASK_TYPE = 'cargo'; |
| 6 | + |
| 7 | +export 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 | + |
| 22 | +function 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 | +} |
0 commit comments