Telescope plugin to load and run tasks in a project that conform to VS Code's Editor Tasks
<C-r>
Short Demo
With Plug:
Plug 'nvim-lua/popup.nvim'
Plug 'nvim-lua/plenary.nvim'
Plug 'nvim-telescope/telescope.nvim' " make sure you have telescope installed
Plug 'EthanJWright/vs-tasks.nvim'
With Packer:
use {
'EthanJWright/vs-tasks.nvim',
requires = {
'nvim-lua/popup.nvim',
'nvim-lua/plenary.nvim',
'nvim-telescope/telescope.nvim'
}
}
With Lazy:
{
"EthanJWright/vs-tasks.nvim",
dependencies = {
"nvim-lua/popup.nvim",
"nvim-lua/plenary.nvim",
"nvim-telescope/telescope.nvim",
},
}
Set up keybindings:
nnoremap <Leader>ta :lua require("telescope").extensions.vstask.tasks()<CR>
nnoremap <Leader>ti :lua require("telescope").extensions.vstask.inputs()<CR>
nnoremap <Leader>tj :lua require("telescope").extensions.vstask.jobs()<CR>
nnoremap <Leader>td :lua require("telescope").extensions.vstask.clear_inputs()<CR>
nnoremap <Leader>tc :lua require("telescope").extensions.vstask.cleanup_completed_jobs()<CR>
nnoremap <Leader>tl :lua require('telescope').extensions.vstask.launch()<cr>
nnoremap <Leader>tl :lua require('telescope').extensions.vstask.command()<cr>
:Telescope vstask tasks
.vscode/tasks.json
default_tasks
based on config<CR>
- Run in current window<C-v>
- Run in vertical split<C-p>
- Run in horizontal split<C-t>
- Run in new tab<C-b>
- Run in background terminal<C-w>
- Run as watched job (restarts on file save):Telescope vstask run
:Telescope vstask inputs
:Telescope vstask launch
.vscode/launch.json
<CR>
- Run in current window<C-v>
- Run in vertical split<C-p>
- Run in horizontal split<C-t>
- Run in new tab<C-b>
- Run in background terminal (hidden from buffers):Telescope vstask jobs
<CR>
- View output in current window<C-v>
- View output in vertical split<C-p>
- View output in horizontal split<C-w>
- Toggle watch mode (restart on save)<C-d>
- Kill task and remove from job list:Telescope vstask clear_inputs
:Telescope vstask cleanup_completed_jobs
VS Tasks can auto detect certain scripts from your package, such as npm scripts.
Make sure to load the plugin in your telescope config
require("telescope").load_extension("vstask")
lua <<EOF
require("vstask").setup({
cache_json_conf = true, -- don't read the json conf every time a task is ran
cache_strategy = "last", -- can be "most" or "last" (most used / last used)
config_dir = ".vscode", -- directory to look for tasks.json and launch.json
telescope_keys = { -- change the telescope bindings used to launch tasks
vertical = '<C-v>',
split = '<C-p>',
tab = '<C-t>',
current = '<CR>',
background = '<C-b>',
watch_job = '<C-w>',
kill_job = '<C-d>',
run = '<C-r>',
},
autodetect = { -- auto load scripts
npm = "on"
},
terminal = 'nvim',-- can be 'nvim' or 'toggleterm'
term_opts = {
vertical = {
direction = "vertical",
size = "80"
},
horizontal = {
direction = "horizontal",
size = "10"
},
current = {
direction = "float",
},
tab = {
direction = 'tab',
}
},
json_parser = vim.json.decode,
default_tasks = {
{
label = "๎ npm install",
type = "shell",
command = "npm i",
filetypes = { "typescript" }, -- remove field to always add
},
},
ignore_input_default = false -- always ignore an input default if `true`
})
EOF
VS Code uses json5 which allows use of comments and trailing commas.
If you want to use the same tasks as your teammates, and they leave trailing commas and comments in the project's task.json,
you will need another parser than the default vim.fn.json_decode
.
A proposed solution: Add the following to your dependencies.
lua <<EOF
{
'Joakker/lua-json5',
run = './install.sh'
}
EOF
And add the following option in the setup:
lua <<EOF
require("vstask").setup({
json_parser = require('json5').parse
})
EOF
In your project root set up .vscode/tasks.json
(default config directory set to .vscode
, but can be changed in setup)
{
"inputs": [
{
"default": "",
"description": "Some term",
"id": "phrase",
"type": "promptString"
},
{
"type": "command",
"id": "cowsay",
"command": "extension.commandvariable.pickStringRemember",
"args": {
"description": "what type of cow?",
"options": [
["normal cow", "mooooo"],
["imposture", "bark bark"]
]
}
}
],
"tasks": [
{
"command": "echo ${input:phrase} | cowsay",
"label": "๐ฎ Cowsay",
"problemMatcher": [],
"type": "shell"
},
{
"command": "echo ${input:cowsay} | cowsay",
"label": "๐ฎ Cowsay with arg list",
"problemMatcher": [],
"type": "shell"
},
{
"command": "echo ${relativeFile} | cowsay",
"label": "Relative File",
"problemMatcher": [],
"type": "shell"
},
{
"command": "echo ${relativeFileDirname} | cowsay",
"label": "Relative File Dirname",
"problemMatcher": [],
"type": "shell"
},
{
"args": ["hello", "world"],
"command": "echo ",
"label": "Arg Hello World",
"problemMatcher": [],
"type": "shell"
},
{
"command": "sleep 1 ; echo 'hello from subtask 1' > /tmp/tmp.txt",
"label": "subtask 1",
"type": "shell"
},
{
"command": "sleep 1 ; echo 'hello from subtask 2' >> /tmp/tmp.txt",
"label": "subtask 2",
"type": "shell"
},
{
"command": "cat /tmp/tmp.txt",
"label": "hello from subtask",
"type": "shell",
"dependsOrder": "sequence",
"dependsOn": ["subtask 1", "subtask 2"]
},
{
"command": "echo 'starting server' ; sleep 5 ; echo 'stopping server'",
"label": "server",
"type": "shell"
},
{
"command": "echo 'starting client' ; sleep 5 ; echo 'stopping client'",
"label": "client",
"type": "shell"
},
{
"label": "start server and client",
"dependsOn": ["server", "client"]
}
],
"version": "2.0.0"
}
lua require("telescope").extensions.vstask.tasks() -- open task list in telescope
lua require("telescope").extensions.vstask.inputs() -- open the input list, set new input
lua require("telescope").extensions.vstask.jobs() -- view and manage all jobs (running and completed)
lua require("telescope").extensions.vstask.history() -- search history of tasks
lua require("telescope").extensions.vstask.close() -- close the task runner (if toggleterm)
lua require("telescope").extensions.vstask.run() -- open menu to type cli cmd and run it with standard bindings
You can also configure themes and pass options to the picker
lua require("telescope").extensions.vstask.tasks(require('telescope.themes').get_dropdown()) -- open task list in telescope
You can also grab the last run task and do what you want with it, such as open it in a new terminal:
function _RUN_LAST_TASK()
local vstask_ok, vstask = pcall(require, "vstask")
if not vstask_ok then
return
end
local cmd = vstask.get_last()
vim.cmd("vsplit")
vim.cmd("term " .. cmd)
end
All variables available in VS Code should also work in this plugin, though they are not all tested.
At this point only the features I need professionally have been implemented. The implemented schema elements are as follows:
As I do not use VS Code, the current implementation are the elements that seem most immediately useful. In the future it may be good to look into implementing other schema elements such as problemMatcher and group.