tasks.json Support

Move the task provider anonymous class into a real class, as this seems
to be how Microsoft do this in their documentation.

resolveTask is now implemented, which is used by VSCode to determine how
to execute tasks that the user has defined in tasks.json.
This commit is contained in:
Jonathan Dickinson 2020-04-22 17:05:04 -07:00
parent 3f1f3a835a
commit 1d8c25b75c

View File

@ -4,22 +4,26 @@ import * as vscode from 'vscode';
// 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 = {
interface CargoTaskDefinition extends vscode.TaskDefinition {
command?: string;
args?: string[];
cwd?: string;
env?: { [key: string]: string };
}
class CargoTaskProvider implements vscode.TaskProvider {
private readonly target: vscode.WorkspaceFolder;
constructor(target: vscode.WorkspaceFolder) {
this.target = target;
}
provideTasks(): vscode.Task[] {
// 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 },
@ -36,7 +40,7 @@ function getStandardCargoTasks(target: vscode.WorkspaceFolder): vscode.Task[] {
},
// The scope of the task - workspace or specific folder (global
// is not supported).
target,
this.target,
// The task name, and task source. These are shown in the UI as
// `${source}: ${name}`, e.g. `rust: cargo build`.
`cargo ${command}`,
@ -50,3 +54,30 @@ function getStandardCargoTasks(target: vscode.WorkspaceFolder): vscode.Task[] {
return vscodeTask;
});
}
resolveTask(task: vscode.Task): vscode.Task | undefined {
// VSCode calls this for every cargo task in the user's tasks.json,
// we need to inform VSCode how to execute that command by creating
// a ShellExecution for it.
const definition = task.definition as CargoTaskDefinition;
if (definition.type === 'cargo' && definition.command) {
const args = [definition.command].concat(definition.args ?? []);
return new vscode.Task(
definition,
task.name,
'rust',
new vscode.ShellExecution('cargo', args, definition),
);
}
return undefined;
}
}
export function activateTaskProvider(target: vscode.WorkspaceFolder): vscode.Disposable {
const provider = new CargoTaskProvider(target);
return vscode.tasks.registerTaskProvider(TASK_TYPE, provider);
}