forked from Davidyz/VectorCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd.lua
More file actions
68 lines (61 loc) · 1.47 KB
/
Copy pathcmd.lua
File metadata and controls
68 lines (61 loc) · 1.47 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
---@type VectorCode.JobRunner
local runner = {}
local Job = require("plenary.job")
---@type {integer: Job}
local jobs = {}
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)
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, self:stderr_result())
else
callback({ result }, self:stderr_result())
end
end
end,
})
jobs[job.pid] = job
job:start()
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
jobs[pid]:wait(timeout_ms)
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