Skip to content

Commit 7ec5e71

Browse files
author
Zhe Yu
committed
refactor(nvim): Use consistent error handling in jobrunner
1 parent fbf5029 commit 7ec5e71

3 files changed

Lines changed: 32 additions & 26 deletions

File tree

lua/vectorcode/cacher/default.lua

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
local M = {}
33
local vc_config = require("vectorcode.config")
44
local notify_opts = vc_config.notify_opts
5+
local jobrunner = require("vectorcode.jobrunner.cmd")
56

67
local logger = vc_config.logger
78

@@ -58,37 +59,35 @@ local function async_runner(query_message, buf_nr)
5859
)
5960
vim.list_extend(args, { "--project_root", project_root })
6061
end
62+
63+
if cache.options.single_job then
64+
kill_jobs(buf_nr)
65+
end
66+
6167
CACHE[buf_nr].job_count = CACHE[buf_nr].job_count + 1
6268
logger.debug("vectorcode default cacher job args: ", args)
63-
local job = require("plenary.job"):new({
64-
command = "vectorcode",
65-
args = args,
66-
detached = true,
67-
on_start = function()
68-
if cache.options.single_job then
69-
kill_jobs(buf_nr)
70-
end
71-
end,
72-
on_exit = function(self, code, signal)
69+
70+
-- jobrunner is assumed to be defined at the module level, e.g., local jobrunner = require("vectorcode.jobrunner.cmd")
71+
local job_pid
72+
job_pid = jobrunner.run_async(
73+
args,
74+
function(json_result, stderr_error, exit_code, signal)
7375
if not M.buf_is_registered(buf_nr) then
7476
return
7577
end
76-
logger.debug("vectorcode ", buf_name, " default cacher results: ", self:result())
78+
logger.debug("vectorcode ", buf_name, " default cacher results: ", json_result)
7779
CACHE[buf_nr].job_count = CACHE[buf_nr].job_count - 1
78-
CACHE[buf_nr].jobs[self.pid] = nil
79-
local ok, json = pcall(
80-
vim.json.decode,
81-
table.concat(self:result()) or "[]",
82-
{ array = true, object = true }
83-
)
84-
if not ok or code ~= 0 then
80+
assert(job_pid ~= nil)
81+
CACHE[buf_nr].jobs[job_pid] = nil
82+
83+
if exit_code ~= 0 then
8584
vim.schedule(function()
8685
if CACHE[buf_nr].options.notify then
8786
if signal == 15 then
8887
vim.notify("Retrieval aborted.", vim.log.levels.INFO, notify_opts)
8988
else
9089
vim.notify(
91-
"Retrieval failed:\n" .. table.concat(self:result()),
90+
"Retrieval failed:\\n" .. table.concat(stderr_error, "\n"),
9291
vim.log.levels.WARN,
9392
notify_opts
9493
)
@@ -98,7 +97,7 @@ local function async_runner(query_message, buf_nr)
9897
return
9998
end
10099
cache = CACHE[buf_nr]
101-
cache.retrieval = json or {}
100+
cache.retrieval = json_result or {}
102101
vim.schedule(function()
103102
if cache.options.notify then
104103
vim.notify(
@@ -109,12 +108,15 @@ local function async_runner(query_message, buf_nr)
109108
end
110109
end)
111110
end,
112-
})
113-
job:start()
111+
buf_nr
112+
)
113+
114114
---@type VectorCode.Cache
115115
cache = CACHE[buf_nr]
116-
cache.last_run = vim.uv.clock_gettime("realtime").sec
117-
cache.jobs[job.pid] = vim.uv.clock_gettime("realtime").sec
116+
if job_pid then
117+
cache.last_run = vim.uv.clock_gettime("realtime").sec
118+
cache.jobs[job_pid] = vim.uv.clock_gettime("realtime").sec
119+
end
118120
vim.schedule(function()
119121
if cache.options.notify then
120122
vim.notify(

lua/vectorcode/jobrunner/cmd.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ function runner.run_async(args, callback, bufnr)
4343
)
4444
end
4545
else
46-
callback({ result }, self:stderr_result())
46+
callback({ result }, self:stderr_result(), code, signal)
4747
logger.warn("cmd runner: failed to decode result:\n", result)
4848
end
4949
end

lua/vectorcode/jobrunner/lsp.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ function jobrunner.run_async(args, callback, bufnr)
100100
if err ~= nil and err.message ~= nil then
101101
err_message = { err.message }
102102
end
103-
vim.schedule_wrap(callback)(result, err_message, err.code)
103+
local code = 0
104+
if err and err.code then
105+
code = err.code
106+
end
107+
vim.schedule_wrap(callback)(result, err_message, code)
104108
if result then
105109
logger.debug(
106110
"lsp jobrunner result:\n",

0 commit comments

Comments
 (0)