-
-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathcmd.lua
More file actions
87 lines (81 loc) · 2.17 KB
/
Copy pathcmd.lua
File metadata and controls
87 lines (81 loc) · 2.17 KB
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
---@type VectorCode.JobRunner
local runner = {}
local Job = require("plenary.job")
---@type {integer: Job}
local jobs = {}
local logger = require("vectorcode.config").logger
function runner.run_async(args, callback, bufnr)
if type(callback) == "function" then
callback = vim.schedule_wrap(callback)
else
callback = nil
end
local cmd = { "vectorcode" }
args = require("vectorcode.jobrunner").find_root(args, bufnr)
vim.list_extend(cmd, args)
logger.debug(
("cmd jobrunner for buffer %s args: %s"):format(bufnr, vim.inspect(args))
)
---@diagnostic disable-next-line: missing-fields
local job = Job:new({
command = "vectorcode",
args = args,
on_exit = function(self, _, _)
jobs[self.pid] = nil
local result = self:result()
local ok, decoded = pcall(vim.json.decode, table.concat(result, ""))
if callback ~= nil then
if ok then
callback(decoded or {}, self:stderr_result())
if vim.islist(result) then
logger.debug(
"cmd jobrunner result:\n",
vim.tbl_map(function(item)
if type(item) == "table" then
item.document = nil
item.chunk = nil
end
return item
end, vim.deepcopy(result))
)
end
else
callback({ result }, self:stderr_result())
logger.warn("cmd runner: failed to decode result:\n", result)
end
end
end,
})
job:start()
jobs[job.pid] = job
return tonumber(job.pid)
end
function runner.run(args, timeout_ms, bufnr)
if timeout_ms == nil or timeout_ms < 0 then
timeout_ms = 2 ^ 31 - 1
end
local res, err
local pid = runner.run_async(args, function(result, error)
res = result
err = error
end, bufnr)
if pid ~= nil then
vim.wait(timeout_ms, function()
return res ~= nil or err ~= nil
end)
jobs[pid] = nil
return res, err
else
return {}, err
end
end
function runner.is_job_running(job)
return jobs[job] ~= nil
end
function runner.stop_job(job_handle)
local job = jobs[job_handle]
if job ~= nil then
job:shutdown(1, 15)
end
end
return runner