aboutsummaryrefslogtreecommitdiff
path: root/editors/code/src/tasks.ts
blob: fa1c4a951ddbb68dfaa45b4ef276d244eb97f15a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import * as vscode from 'vscode';

// This ends up as the `type` key in tasks.json. RLS also uses `cargo` and
// our configuration should be compatible with it so use the same key.
const TASK_TYPE = 'cargo';

export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
    const provider: vscode.TaskProvider = {
        // Detect Rust tasks. Currently we do not do any actual detection
        // of tasks (e.g. aliases in .cargo/config) and just return a fixed
        // set of tasks that always exist. These tasks cannot be removed in
        // tasks.json - only tweaked.
        provideTasks: () => getStandardCargoTasks(target),

        // We don't need to implement this.
        resolveTask: () => undefined,
    };

    return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
}

function getStandardCargoTasks(target: vscode.WorkspaceFolder): vscode.Task[] {
    return [
        { command: 'build', group: vscode.TaskGroup.Build },
        { command: 'check', group: vscode.TaskGroup.Build },
        { command: 'test', group: vscode.TaskGroup.Test },
        { command: 'clean', group: vscode.TaskGroup.Clean },
        { command: 'run', group: undefined },
    ]
        .map(({ command, group }) => {
            const vscodeTask = new vscode.Task(
                // The contents of this object end up in the tasks.json entries.
                {
                    type: TASK_TYPE,
                    command,
                },
                // The scope of the task - workspace or specific folder (global
                // is not supported).
                target,
                // The task name, and task source. These are shown in the UI as
                // `${source}: ${name}`, e.g. `rust: cargo build`.
                `cargo ${command}`,
                'rust',
                // What to do when this command is executed.
                new vscode.ShellExecution('cargo', [command]),
                // Problem matchers.
                ['$rustc'],
            );
            vscodeTask.group = group;
            return vscodeTask;
        });
}