summary refs log tree commit diff
path: root/.vim/lua/internal/job.lua
diff options
context:
space:
mode:
Diffstat (limited to '.vim/lua/internal/job.lua')
-rw-r--r--.vim/lua/internal/job.lua65
1 files changed, 32 insertions, 33 deletions
diff --git a/.vim/lua/internal/job.lua b/.vim/lua/internal/job.lua
index c85fd67..5e3b6e1 100644
--- a/.vim/lua/internal/job.lua
+++ b/.vim/lua/internal/job.lua
@@ -9,6 +9,21 @@ 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',
+    }
+  })
+end
 
 --create a new job and register
 --@return ~/.local/share/nvim/site/pack/packer/start/plenary.nvim/lua/plenary/job.lua
@@ -18,14 +33,16 @@ function M.jobstart(opts)
   -- default options
   -- opts.enable_recording = true
   opts.enable_spinner = (opts.enable_spinner or true) and spinner_ok
-  opts.populate_quickfix = opts.populate_quickfix or true
-
-  local title
-  if opts.format_title then
-    title = opts.format_title(opts.command, opts.args)
-    opts.format_title = nil
+  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
-    title = table.concat({opts.command, unpack(opts.args)}, ' ')
+    error(string.format('Invalid field "format_title" of type %s', type(opts.format_title)))
   end
 
   opts.on_start = vim.schedule_wrap(function(j)
@@ -35,7 +52,7 @@ function M.jobstart(opts)
       vim.fn.setqflist({}, 'r') -- clear quickfix
     end
     if opts.enable_spinner then
-      spinner.on_attach(j.pid, title, vim.fn.bufnr())
+      spinner.on_attach(j.pid, opts.format_title, vim.fn.bufnr())
       spinner.on_progress('begin', j.pid, j.pid)
     end
   end)
@@ -46,13 +63,13 @@ function M.jobstart(opts)
     if not (code == 0) then
       vim.notify(string.format('[%d] exit %d', j.pid, code), vim.log.levels.ERROR)
     end
-    if opts.populate_quickfix then
-      vim.cmd.doautocmd [[QuickFixCmdPost]]
-    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)
@@ -60,17 +77,8 @@ function M.jobstart(opts)
       vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR)
     end -- handle error?
 
-    if opts.populate_quickfix and data ~= nil then
-      vim.fn.setqflist({}, 'a', {
-        title = title,
-        lines = {strip_ansi(data)},
-        efm = '%m',
-        context = {
-          cmd = {opts.command, unpack(opts.args)},
-          pid = j.pid,
-          source = 'jobstart',
-        }
-      })
+    if opts.populate_quickfix and opts.populate_quickfix ~= 'onerror' then
+      setqf(opts, j.pid, data)
     end
   end)
 
@@ -79,17 +87,8 @@ function M.jobstart(opts)
       vim.notify(string.format('[%d] error: %s', j.pid, err), vim.log.levels.ERROR)
     end -- handle error?
 
-    if opts.populate_quickfix and data ~= nil then
-      vim.fn.setqflist({}, 'a', {
-        title = title,
-        lines = {strip_ansi(data)},
-        efm = '%m',
-        context = {
-          cmd = {opts.command, unpack(opts.args)},
-          pid = j.pid,
-          source = 'jobstart',
-        }
-      })
+    if opts.populate_quickfix then
+      setqf(opts, j.pid, data)
     end
   end)