summary refs log tree commit diff
path: root/.vim/lua/internal/job.lua
blob: 2e97f1662c8ac162c9f2e32b03a34cb72bfd1722 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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 {}

  opts = vim.tbl_extend('keep', opts or {}, {
    enable_spinner = vim.g.job_enable_spinner,
    populate_quickfix = vim.g.job_populate_quickfix,
    attach = vim.g.job_attach,
  })

  -- vim.notify_once(vim.inspect(opts), vim.log.levels.DEBUG)

  if opts.attach then
    return vim.schedule_wrap(function()
      vim.cmd.terminal({ args = { opts.command, unpack(opts.args) } })
    end)()
  end

  local spinner = {}
  if opts.enable_spinner then
    local ok = false
    ok, spinner = pcall(require, 'spinner.core')
    if not ok then
      opts.enable_spinner = false
    end
  end

  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.sh(command, opts)
  return M.jobstart(vim.tbl_extend('keep', opts or {}, {
    command = vim.env.SHELL,
    args = { '-c', command },
    -- 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.make(opts)
  opts = opts or {}

  local makeprg = vim.fn.expandcmd(vim.opt.makeprg:get())
  local cwd = vim.fn.expand('%:p:h')

  if not (string.match(makeprg, 'make') == nil) then
    -- detect makefile
    opts.cwd = require('lspconfig.util').root_pattern('Makefile')(cwd)
  end

  return M.sh(makeprg, opts)
end

function M.list()
  require('telescope._extensions').manager['jobs']['jobs']()
end

function M.setup(opts)
  opts = vim.tbl_extend('keep', opts, { telescope = true, mappings = true })

  -- defaults
  vim.g.job_enable_spinner = true
  vim.g.job_populate_quickfix = 'onerror'
  vim.g.job_attach = false

  if opts.telescope then
    require('telescope').load_extension('jobs')
    vim.api.nvim_create_user_command('Jobs', M.list, { desc = 'list jobs managed by internal.job' })
    if opts.mappings then
      vim.keymap.set('n', '<leader>jl', M.list, { desc = 'list jobs managed by internal.job' })
    end
  end

  vim.api.nvim_create_user_command('Make', function(a)
    require('internal.job').make(a.fargs)
  end, { nargs = '*', desc = 'run make target or &makeprg with internal.job' })

  vim.api.nvim_create_user_command('Sh', function(a)
    require('internal.job').sh(table.concat(a.fargs, ' '))
  end, { nargs = '*', desc = 'run shell command with internal.job' })

  if opts.mappings then
    vim.keymap.set('n', '<leader>jm', M.make, { desc = 'run &makeprg with internal.job' })
    vim.keymap.set('n', '<leader>js', ':Sh ', { desc = 'run shell commands with internal.job' })
  end
end

return M