Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension API - Task Provider - Build Task example

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?

like image 854
sentry07 Avatar asked Mar 13 '19 06:03

sentry07


People also ask

What is workspaceFolder in VS code?

${workspaceFolder} - the path of the folder opened in VS Code. ${workspaceFolderBasename} - the name of the folder opened in VS Code without any slashes (/)

How do I run a build code in Visual Studio?

Now that we have a simple C++ program, let's build it. Select the Terminal > Run Build Task command (Ctrl+Shift+B) from the main menu. This will display a dropdown with various compiler task options. If you are using a GCC toolset like MinGW, you would choose C/C++: g++.exe build active file.

Where can I find C_cpp_properties json?

The c_cpp_properties. json is a file specific to the Microsoft C/C++ extension and usually exists only in the workspace folder. This file is ... Open Folder projects that don't use CMake can store project configuration settings for IntelliSense in a CppProperties.


1 Answers

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.

like image 122
Gama11 Avatar answered Sep 28 '22 23:09

Gama11