This note walks through setting up user-scoped keybindings and project-scoped tasks for Elixir in VSCode.
As a prerequiste, note that mix allows you to specify file and line number optionally via mix test [filename][:line]
(see mix help test
for more).
Project Tasks
You can create a .vscode/tasks.json
file manually or via the command palette via "Tasks: Configure Task" and selecting "Other" to configure a manual task.
${file}
and${lineNumber}
are vscode task variablesrunOptions -> reevaluateOnRerun: false
is a particular task run behavior that does not re-substitute the variables when you rerun the test. The normal behavior is to detect the file and line number that your cursor is on, and supply those values to the task variables. But, if you've previously ran a test, it has failed, and you want to go edit the source code and then rerun the test, you need to tell vscode to just run the previously ran command. In effect, you can rerun the test without worrying about the location of your cursor.
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "all",
"type": "shell",
"command": "mix test"
},
{
"label": "file",
"type": "shell",
"command": "mix test ${file}",
"runOptions": {
"reevaluateOnRerun": false
}
},
{
"label": "line",
"type": "shell",
"command": "mix test ${file}:${lineNumber}",
"runOptions": {
"reevaluateOnRerun": false
}
}
]
}
User Keybindings
You can access these in the command palette via "Preferences: Open Keyboard Shortcuts (JSON)". This file is deep inside system dirs and can be controlled by settings sync.
- The last keybinding below,
reRunTask
, is the command associated with thereevaluateOnRerun: false
setting from above. So when I pressalt+r
, I will rerun the previously ran test, no matter where my cursor is. (The location of the cursor determines the${file}
and${lineNumber}
task varaibles.)
[
{
"key": "alt+a",
"command": "workbench.action.tasks.runTask",
"args": "all"
},
{
"key": "alt+f",
"command": "workbench.action.tasks.runTask",
"args": "file"
},
{
"key": "alt+l",
"command": "workbench.action.tasks.runTask",
"args": "line"
},
{
"key": "alt+r",
"command": "workbench.action.tasks.reRunTask"
}
]
Now, for example, if your cursor is inside a particular test, you can press alt+l
to run that test. Also, these user-scoped keybindings can be associated with any project-level task with the same name, so in theory these keybindings work across languages and frameworks, etc.