|
| 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 | +} |
0 commit comments