Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create VS Code extension that provides custom problemMatcher?

I have project that uses custom problemMatcher. But I would like to extract it into an extension making it configurable. So eventually it could be used in tasks.json like

{
    "problemMatcher": "$myCustomProblemMatcher"
}

How to do that?

like image 896
mar3kk Avatar asked Jan 17 '17 19:01

mar3kk


People also ask

What is task json in VS Code?

Tasks in VS Code can be configured to run scripts and start processes so that many of these existing tools can be used from within VS Code without having to enter a command line or write new code. Workspace or folder specific tasks are configured from the tasks. json file in the . vscode folder for a workspace.

What language is used to create VS Code extensions?

VS Code extensions support two main languages: JavaScript and TypeScript.


1 Answers

As of VSCode 1.11.0 (March 2017), extensions can contribute problem matchers via package.json:

{
    "contributes": {
        "problemMatchers": [
            {
                "name": "gcc",
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        ]
    }
}

Tasks can then reference it with "problemMatcher": ["$name"] ($gcc in the case of this example).


Instead of defining a matcher's pattern inline, it can also contributed in problemPatterns so it's reusable (for instance if you want to use it in multiple matchers):

{
    "contributes": {
        "problemPatterns": [
            {
                "name": "gcc",
                "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                "file": 1,
                "line": 2,
                "column": 3,
                "severity": 4,
                "message": 5
            }
        ],
        "problemMatchers": [
            {
                "name": "gcc",
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceRoot}"
                ],
                "pattern": "$gcc"
            }
        ]
    }
}
like image 97
Gama11 Avatar answered Oct 06 '22 21:10

Gama11