From 2f81311e1e3b4b79c756f7eb13d4ba89c9a6a0b1 Mon Sep 17 00:00:00 2001 From: Zhe Yu Date: Thu, 1 May 2025 11:32:15 +0800 Subject: [PATCH] fix(nvim): fix job management in cmd runner and `ls_on_start`. --- .../codecompanion/func_calling_tool.lua | 21 +++++++------ lua/vectorcode/jobrunner/cmd.lua | 30 +++++++++++-------- 2 files changed, 30 insertions(+), 21 deletions(-) diff --git a/lua/vectorcode/integrations/codecompanion/func_calling_tool.lua b/lua/vectorcode/integrations/codecompanion/func_calling_tool.lua index c799ccc..43a7a64 100644 --- a/lua/vectorcode/integrations/codecompanion/func_calling_tool.lua +++ b/lua/vectorcode/integrations/codecompanion/func_calling_tool.lua @@ -195,15 +195,18 @@ return check_cli_wrap(function(opts) if opts.ls_on_start then job_runner = cc_common.initialise_runner(opts.use_lsp) if job_runner ~= nil then - vim.list_extend(guidelines, { - " - The following projects are indexed by VectorCode and are available for you to search in:", - }) - vim.list_extend( - guidelines, - vim.tbl_map(function(s) - return string.format(" - %s", s["project-root"]) - end, job_runner.run({ "ls", "--pipe" }, -1, 0)) - ) + local projects = job_runner.run({ "ls", "--pipe" }, -1, 0) + if vim.islist(projects) and #projects > 0 then + vim.list_extend(guidelines, { + " - The following projects are indexed by VectorCode and are available for you to search in:", + }) + vim.list_extend( + guidelines, + vim.tbl_map(function(s) + return string.format(" - %s", s["project-root"]) + end, projects) + ) + end end end local root = vim.fs.root(0, { ".vectorcode", ".git" }) diff --git a/lua/vectorcode/jobrunner/cmd.lua b/lua/vectorcode/jobrunner/cmd.lua index df88886..a2e200f 100644 --- a/lua/vectorcode/jobrunner/cmd.lua +++ b/lua/vectorcode/jobrunner/cmd.lua @@ -28,24 +28,28 @@ function runner.run_async(args, callback, bufnr) local ok, decoded = pcall(vim.json.decode, table.concat(result, "")) if callback ~= nil then if ok then - logger.debug( - "cmd jobrunner result:\n", - vim.tbl_map(function(item) - item.document = nil - item.chunk = nil - return item - end, vim.deepcopy(result)) - ) - callback(decoded, self:stderr_result()) + 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 - logger.warn("cmd runner: failed to decode result:\n", result) callback({ result }, self:stderr_result()) + logger.warn("cmd runner: failed to decode result:\n", result) end end end, }) - jobs[job.pid] = job job:start() + jobs[job.pid] = job return tonumber(job.pid) end @@ -59,7 +63,9 @@ function runner.run(args, timeout_ms, bufnr) err = error end, bufnr) if pid ~= nil then - jobs[pid]:wait(timeout_ms) + vim.wait(timeout_ms, function() + return res ~= nil or err ~= nil + end) jobs[pid] = nil return res, err else