Skip to content

Instantly share code, notes, and snippets.

@joebew42
Last active December 27, 2022 21:30
Show Gist options
  • Save joebew42/b7460b17385dc9ed8f753b1271d70819 to your computer and use it in GitHub Desktop.
Save joebew42/b7460b17385dc9ed8f753b1271d70819 to your computer and use it in GitHub Desktop.

Visual Studio Code and Elixir

Here I wanted to share my own configuration of Visual Studio Code that I currently use when programming in Elixir. The section about Tasks and Shortcut is taken from this post: Running Elixir Tests in Visual Studio Code/ElixirLS.

Required extensions

Custom tasks

tasks.json

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "Build",
      "type": "shell",
      "command": "mix compile",
      "problemMatcher": [
        "$mixCompileError",
        "$mixCompileWarning"
      ],
      "group": {
        "kind": "build",
        "isDefault": true
      }
    },
    {
      "label": "Format Current File",
      "type": "shell",
      "command": "mix",
      "args": ["format", "${relativeFile}"],
      "options": {
        "cwd": "${workspaceRoot}"
      },
      "problemMatcher": "$mixTestFailure",
      "presentation": {
        "focus": false,
        "reveal": "never",
      }
    },
    {
      "label": "Run All Tests",
      "type": "shell",
      "command": "mix",
      "args": ["test"],
      "options": {
        "cwd": "${workspaceRoot}"
      },
      "problemMatcher": [
        "$mixCompileError",
        "$mixCompileWarning",
        "$mixTestFailure"
      ],
      "group": {
        "kind": "test",
        "isDefault": true
      },
      "presentation": {
        "focus": true,
      }
    },
    {
      "label": "Run Current Tests",
      "type": "shell",
      "command": "mix",
      "args": ["test", "${relativeFile}"],
      "options": {
        "cwd": "${workspaceRoot}"
      },
      "problemMatcher": [
        "$mixCompileError",
        "$mixCompileWarning",
        "$mixTestFailure"
      ],
      "presentation": {
        "focus": true,
      }
    },
    {
      "label": "Run Focused Test",
      "type": "shell",
      "command": "mix",
      "args": ["test", "${relativeFile}:${lineNumber}"],
      "options": {
        "cwd": "${workspaceRoot}"
      },
      "problemMatcher": [
        "$mixCompileError",
        "$mixCompileWarning",
        "$mixTestFailure"
      ],
      "presentation": {
        "focus": true,
      }
    }
  ]
}

Key Bindings

keybindings.json

[
  {
    "key": "ctrl+alt-b",
    "command": "workbench.action.tasks.runTask",
    "args": "Build"
  },
  {
    "key": "ctrl+alt-l",
    "command": "workbench.action.tasks.runTask",
    "args": "Format Current File"
  },
  {
    "key": "ctrl+alt+t",
    "command": "workbench.action.tasks.runTask",
    "args": "Run All Tests"
  },
  {
    "key": "ctrl+alt+c",
    "command": "workbench.action.tasks.runTask",
    "args": "Run Current Tests"
  },
  {
    "key": "ctrl+alt+f",
    "command": "workbench.action.tasks.runTask",
    "args": "Run Focused Test"
  }
]

How to use

  • Build the project: Ctrl + Alt + B (it will run mix compile)
  • Format Current File: Ctrl + Alt + L
  • Run All Tests: Ctrl + Alt + T
  • Run Current Tests: Ctrl + Alt + C
  • Run Focused Test: Ctrl + Alt + F
  • Close the terminal: Ctrl + `
@matisnape
Copy link

All of those shortcuts just open the dropdown with lists of tasks - what am I doing wrong?

@matisnape
Copy link

matisnape commented Feb 26, 2020

OK, figured this out - tasks.json should be in .vscode directory in project, not in ~/Library/Application Support/Code/User

It's sad that VSCode doesn't support global tasks.json config (as mentioned in microsoft/vscode#81496)

@maco
Copy link

maco commented Mar 8, 2022

Since vscode-elixir has been dead for 5 years, do you know if this works with ElixirLS?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment