Extension API - Task Provider - Build Task example

喜夏-厌秋 提交于 2019-12-23 15:20:58

问题


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.

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?


回答1:


Here's a minimal TaskProvider implementation that simply runs echo "Hello World" in the shell:

'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;
        }
    });
}

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"
        }
    ]
}

Extensions with a task provider should activate when the Tasks: Run Task command is executed:

"activationEvents": [
    "onCommand:workbench.action.tasks.runTask"
]

And finally, the problem matcher(s) you want to reference need to be contributed in the package.json's contributes.problemMatchers section.



来源:https://stackoverflow.com/questions/55135876/extension-api-task-provider-build-task-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!