VSCode

Integration with VSCode is accomplished using the task interface. This interface allows VSCode to run VSG in a shell and extract the results.

Configuring

The task interface uses the task.json file to tell VSCode how to execute a task. The file can be generated by choosing the menu option Terminal->Configure Tasks…

../_images/vscode_terminal_configure_tasks.png

…then choosing Create tasks.json file from template

../_images/vscode_create_json_file.png

…and finally choosing the Others option.

../_images/vscode_others_options.png

This will create a template task.json file:

../_images/vscode_template_json_file.png

Replace the contents of the template file with the following…

{
  // See https://go.microsoft.com/fwlink/?LinkId=733558
  // for the documentation about the tasks.json format
  "version": "2.0.0",
  "tasks": [
    {
      "label": "vsg-check",
      "type": "shell",
      "command": "vsg -f ${relativeFile}",
      "problemMatcher": {
        "owner": "vsg",
        "fileLocation": ["autoDetect", "${workspaceFolder}"],
        "pattern": [
          {
            "regexp": "^File:  ([^\\s]*)\\s*$",
            "file": 1
          },
          { // match =======
            "regexp": "^.*$"
          },
          { // match Phase
            "regexp": "^.*$"
          },
          { // match Total Rules Checked
            "regexp": "^.*$"
          },
          { // match Total Violations
            "regexp": "^.*$"
          },
          { // match Error count
            "regexp": "^.*$"
          },
          { // match Warning count
            "regexp": "^.*$"
          },
          { // match ------
            "regexp": "^.*$"
          },
          { // match Rule/severity/line/solution header
            "regexp": "^.*$"
          },
          { // match ------
            "regexp": "^.*$"
          },
          {
            "regexp": "^  ([^\\s]*_\\d*)\\s*. ([^\\s]*)\\s*.\\s*([^\\s]*) . (.*)$",
            "code": 1,
            "severity": 2,
            "location": 3,
            "message": 4,
            "loop": true
          }
        ]
      }
    },
    {
      "label": "vsg-fix",
      "type": "shell",
      "command": "vsg -f ${relativeFile} --fix",
      "problemMatcher": {
        "owner": "vsg",
        "fileLocation": ["autodetect", "${workspaceFolder}"],
        "pattern": [
          {
            "regexp": "^File:  ([^\\s]*)\\s*$",
            "file": 1
          },
          { // match =======
            "regexp": "^.*$"
          },
          { // match Phase
            "regexp": "^.*$"
          },
          { // match Total Rules Checked
            "regexp": "^.*$"
          },
          { // match Total Violations
            "regexp": "^.*$"
          },
          { // match Error count
            "regexp": "^.*$"
          },
          { // match Warning count
            "regexp": "^.*$"
          },
          { // match ------
            "regexp": "^.*$"
          },
          { // match Rule/severity/line/solution header
            "regexp": "^.*$"
          },
          { // match ------
            "regexp": "^.*$"
          },
          {
            "regexp": "^  ([^\\s]*_\\d*)\\s*. ([^\\s]*)\\s*.\\s*([^\\s]*) . (.*)$",
            "code": 1,
            "severity": 2,
            "location": 3,
            "message": 4,
            "loop": true
          }
        ]
      }
    }
  ]
}

…and save the file:

Executing Task

The two tasks vsg-check and vsg-fix are available under the Terminal=>Run Task… menu option…

../_images/vscode_terminal_run_task.png

Keybindings

A keybinding can be created to make it easier to run the vsg-check and vsg-fix tasks. This involves creating a keybindings.json file. This file is located in the same directory as the tasks.json file.

Copy the following into the keybindings.json file…

[
    {"key": "ctrl+f", "command": "workbench.action.tasks.runTask", "args": "vsg-fix"},
    {"key": "ctrl+i", "command": "workbench.action.tasks.runTask", "args": "vsg-check"}
]

…and save it to the same directory as the task.json file.

Now the open file buffer can be checked for errors using ctrl+i.

../_images/vscode_file_with_errors.png

The violations are shown in the Problems tab:

../_images/vscode_problems_tab.png

The violations can be fixed manually, or by using ctrl+f

../_images/vscode_file_fixed.png