local M = { current_jobs = {}, } local strip_ansi = function(line) line, _ = line:gsub(string.char(27) .. '[[0-9;]*m', '') return line end local setqf = function(opts, pid, data) if data == nil then return end vim.fn.setqflist({}, 'a', { title = opts.format_title, lines = { strip_ansi(data) }, efm = '%m', context = { cmd = { opts.command, unpack(opts.args) }, pid = pid, source = 'jobstart', }, }) vim.cmd.doautocmd('QuickFixCmdPost') end --create a new job and register --@return ~/.local/share/nvim/site/pack/packer/start/plenary.nvim/lua/plenary/job.lua function M.jobstart(opts) opts = opts or {} local spinner_ok, spinner = pcall(require, 'spinner.core') -- default options opts.enable_spinner = (opts.enable_spinner or true) and spinner_ok opts.populate_quickfix = opts.populate_quickfix or 'onerror' if opts.format_title == nil then opts.format_title = table.concat({ opts.command, unpack(opts.args) }, ' ') elseif type(opts.format_title) == 'function' then opts.format_title = opts.format_title(opts.command, opts.args) elseif type(opts.format_title) == 'string' then -- noop else error(string.format('Invalid field "format_title" of type %s', type(opts.format_title))) end opts.on_start = vim.schedule_wrap(function(j) M.current_jobs[j.pid] = j if opts.populate_quickfix then vim.fn.setqflist({}, 'r') -- clear quickfix end if opts.enable_spinner then spinner.on_attach(j.pid, opts.format_title, vim.fn.bufnr()) spinner.on_progress('begin', j.pid, j.pid) end end) opts.on_exit = vim.schedule_wrap(function(j, code) M.current_jobs[j.pid] = nil if not (code == 0) then vim.notify(string.format('[%d] exit %d', j.pid, code), vim.log.levels.ERROR) end if opts.enable_spinner then spinner.on_progress('end', j.pid, j.pid) spinner.on_exit(nil, nil, j.pid) end if opts.populate_quickfix then vim.cmd.doautocmd('QuickFixCmdPost') end end) opts.on_stdout = vim.schedule_wrap(function(err, data, j) if err ~= nil then vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR) end -- handle error? if opts.populate_quickfix and opts.populate_quickfix ~= 'onerror' then setqf(opts, j.pid, data) end end) opts.on_stderr = vim.schedule_wrap(function(err, data, j) if err ~= nil then vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR) end -- handle error? if opts.populate_quickfix then setqf(opts, j.pid, data) end end) -- run vim.fn.expand() on all args for i = 1, #opts.args do opts.args[i] = vim.fn.expandcmd(opts.args[i]) end local j = require('plenary.job'):new(opts) j:start() return j end function M.make(extra_args) local makeprg = vim.fn.expandcmd(vim.opt.makeprg:get()) -- split makeprg into command + args local args = vim.split(makeprg, ' ') local command = args[1] table.remove(args, 1) local cwd = vim.fn.expand('%:p:h') if not (string.match(makeprg, 'make') == nil) then -- detect makefile cwd = require('lspconfig.util').root_pattern('Makefile')(cwd) end for _, v in pairs(extra_args or {}) do table.insert(args, v) end -- table.foreach(extra_args or {}, function(_, v) end) return M.jobstart({ command = command, args = args, cwd = cwd, }) end function M.sh(command_string) return M.jobstart({ command = vim.env.SHELL, args = { '-c', command_string }, -- strip 'sh -c' from the title (used by progress reporting etc.) format_title = function(_, args) return table.concat({ unpack(args, 2, #args) }, ' ') end, }) end function M.list() require('telescope._extensions').manager['jobs']['jobs']() end function M.setup() require('telescope').load_extension('jobs') vim.keymap.set( 'n', 'J', require('internal.job').list, { desc = 'list jobs managed by internal.job' } ) vim.api.nvim_create_user_command('Jobs', M.list, { desc = 'list jobs managed by internal.job' }) end return M