扩展API-任务提供程序-构建任务示例 [英] Extension API - Task Provider - Build Task example
问题描述
我为使用的编程语言构建了扩展,并创建了热键快捷方式,用于使用当前打开的文档的URI调用编译器可执行文件.我想将其转换为扩展程序中的构建任务.我已经用构建任务制作了一个tasks.json
文件,该文件可以工作并捕获错误等,但是只有将其放在当前工作空间中后,该文件才能工作.
I've built an extension for a programming language that I use and I've created hotkey shortcuts for calling the compiler executable with the currently open document's URI. I want to convert that to a build task in my extension. I have made a tasks.json
file with a build task that works and catches errors and such, but it only works if I put it in the current workspace.
绝对没有在任何地方添加构建任务的示例,并且 API文档用于任务提供程序专门用于Ruby Rakefiles或其他东西.我只是想做一个带有问题匹配器的shell可执行构建任务.谁能给我一个例子吗?
There are absolutely no examples of adding a build task anywhere and the API documentation for task providers is specifically for Ruby Rakefiles or something. I'm just wanting to make a shell executable build task with a problem matcher. Can anyone give me an example of that?
推荐答案
这是最小的 TaskProvider
实现,只需在外壳中运行echo "Hello World"
:
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
var type = "exampleProvider";
vscode.tasks.registerTaskProvider(type, {
provideTasks(token?: vscode.CancellationToken) {
var execution = new vscode.ShellExecution("echo \"Hello World\"");
var problemMatchers = ["$myProblemMatcher"];
return [
new vscode.Task({type: type}, vscode.TaskScope.Workspace,
"Build", "myExtension", execution, problemMatchers)
];
},
resolveTask(task: vscode.Task, token?: vscode.CancellationToken) {
return task;
}
});
}
任务定义(new Task()
的第一个参数)需要通过package.json
和可以根据需要具有其他属性:
The task definition (first argument for new Task()
) needs to be contributed via package.json
and can have additional properties if needed:
"contributes": {
"taskDefinitions": [
{
"type": "exampleProvider"
}
]
}
执行 Tasks:Run Task 命令时,应激活带有任务提供程序的扩展:
Extensions with a task provider should activate when the Tasks: Run Task command is executed:
"activationEvents": [
"onCommand:workbench.action.tasks.runTask"
]
最后,要引用的问题匹配器需要在package.json
的contributes.problemMatchers
部分中提供.
And finally, the problem matcher(s) you want to reference need to be contributed in the package.json
's contributes.problemMatchers
section.
这篇关于扩展API-任务提供程序-构建任务示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!