Skip to content

Commit 2c33375

Browse files
authored
fix(nvim): Handle nil results in check (#292)
* fix(nvim): Handle `nil` results * refactor(nvim): Move `flatten_table_to_string` to `vectorcode.utils` * fix(nvim): remove unused `require`
1 parent 8638df3 commit 2c33375

File tree

9 files changed

+57
-33
lines changed

9 files changed

+57
-33
lines changed

lua/vectorcode/init.lua

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local M = {}
22

33
local vc_config = require("vectorcode.config")
4+
local utils = require("vectorcode.utils")
45
local logger = vc_config.logger
56
local get_config = vc_config.get_user_config
67
local notify_opts = vc_config.notify_opts
@@ -60,7 +61,7 @@ M.query = vc_config.check_cli_wrap(
6061
else
6162
jobrunner.run_async(args, function(result, error)
6263
logger.debug(result)
63-
callback(result)
64+
callback(result or {})
6465
if error then
6566
logger.warn(vim.inspect(error))
6667
end
@@ -191,8 +192,8 @@ function M.check(check_item, stdout_cb)
191192
return_code = code
192193
if type(stdout_cb) == "function" then
193194
stdout_cb({
194-
stdout = table.concat(vim.iter(result):flatten(math.huge):totable()),
195-
stderr = table.concat(vim.iter(error):flatten(math.huge):totable()),
195+
stdout = utils.flatten_table_to_string(result),
196+
stderr = utils.flatten_table_to_string(error),
196197
code = code,
197198
signal = signal,
198199
})

lua/vectorcode/integrations/codecompanion/common.lua

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,17 @@ local TOOL_RESULT_SOURCE = "VectorCodeToolResult"
1010
return {
1111
tool_result_source = TOOL_RESULT_SOURCE,
1212

13-
---@param t table|string
13+
---@param t table|string|nil
1414
---@return string
1515
flatten_table_to_string = function(t)
16-
if type(t) == "string" then
17-
return t
18-
end
19-
20-
-- Handle empty tables or tables with empty strings
21-
local flattened = vim
22-
.iter(t)
23-
:flatten(math.huge)
24-
:filter(function(item)
25-
return type(item) == "string" and vim.trim(item) ~= ""
26-
end)
27-
:totable()
28-
29-
if #flattened == 0 then
30-
return "Unknown error occurred"
31-
end
32-
33-
return table.concat(flattened, "\n")
16+
vim.deprecate(
17+
"vectorcode.integrations.codecompanion.common.flatten_table_to_string",
18+
"vectorcode.utils.flatten_table_to_string",
19+
"1.0.0",
20+
"vectorcode",
21+
true
22+
)
23+
return require("vectorcode.utils").flatten_table_to_string(t)
3424
end,
3525

3626
---@param use_lsp boolean

lua/vectorcode/integrations/codecompanion/files_ls_tool.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
local cc_common = require("vectorcode.integrations.codecompanion.common")
44
local vc_config = require("vectorcode.config")
5+
local utils = require("vectorcode.utils")
56

67
local default_opts = {
78
use_lsp = vc_config.get_user_config().async_backend == "lsp",
@@ -43,7 +44,7 @@ return function(opts)
4344
cb({ status = "success", data = result })
4445
else
4546
if type(error) == "table" then
46-
error = cc_common.flatten_table_to_string(error)
47+
error = utils.flatten_table_to_string(error)
4748
end
4849
cb({
4950
status = "error",

lua/vectorcode/integrations/codecompanion/ls_tool.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---@module "codecompanion"
22

3-
local cc_common = require("vectorcode.integrations.codecompanion.common")
43
local vc_config = require("vectorcode.config")
4+
local utils = require("vectorcode.utils")
55
local logger = vc_config.logger
66

77
---@type VectorCode.CodeCompanion.LsToolOpts
@@ -45,7 +45,7 @@ return function(opts)
4545
cb({ status = "success", data = result })
4646
else
4747
if type(error) == "table" then
48-
error = cc_common.flatten_table_to_string(error)
48+
error = utils.flatten_table_to_string(error)
4949
end
5050
cb({
5151
status = "error",

lua/vectorcode/integrations/codecompanion/prompts/init.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ function M.register_prompt(opts)
6868

6969
assert(type(opts.name) == "string", "`name` cannot be `nil`.")
7070

71-
local cc_common = require("vectorcode.integrations.codecompanion.common")
7271
local constants = require("codecompanion.config").config.constants
7372
local prompts = {}
7473

@@ -132,7 +131,7 @@ Here's my question:
132131
vc_config.notify_opts
133132
)
134133
elseif err ~= nil then
135-
err = cc_common.flatten_table_to_string(err)
134+
err = utils.flatten_table_to_string(err)
136135
if err ~= "" then
137136
vim.schedule_wrap(vim.notify)(
138137
err,

lua/vectorcode/integrations/codecompanion/query_tool.lua

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ local cc_config = require("codecompanion.config").config
55
local cc_schema = require("codecompanion.schema")
66
local http_client = require("codecompanion.http")
77
local vc_config = require("vectorcode.config")
8+
local utils = require("vectorcode.utils")
89
local check_cli_wrap = vc_config.check_cli_wrap
910
local logger = vc_config.logger
1011

@@ -463,9 +464,14 @@ return check_cli_wrap(function(opts)
463464
)
464465

465466
job_runner.run_async(args, function(result, error, code)
466-
local err_string = cc_common.flatten_table_to_string(error)
467+
local err_string = utils.flatten_table_to_string(error)
467468

468-
if vim.islist(result) and #result > 0 and result[1].path ~= nil then ---@cast result VectorCode.QueryResult[]
469+
if
470+
result ~= nil
471+
and vim.islist(result)
472+
and #result > 0
473+
and result[1].path ~= nil
474+
then ---@cast result VectorCode.QueryResult[]
469475
local summary_opts = vim.deepcopy(opts.summarise) or {}
470476
if type(summary_opts.enabled) == "function" then
471477
summary_opts.enabled = summary_opts.enabled(tools.chat, result) --[[@as boolean]]
@@ -598,7 +604,7 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
598604
vim.inspect(stderr)
599605
)
600606
)
601-
stderr = cc_common.flatten_table_to_string(stderr)
607+
stderr = utils.flatten_table_to_string(stderr)
602608
if string.find(stderr, "InvalidCollectionException") then
603609
if cmd.project_root then
604610
tools.chat:add_tool_output(

lua/vectorcode/integrations/codecompanion/vectorise_tool.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
local cc_common = require("vectorcode.integrations.codecompanion.common")
44
local vc_config = require("vectorcode.config")
5+
local utils = require("vectorcode.utils")
56
local logger = vc_config.logger
67

78
---@alias VectoriseToolArgs { paths: string[], project_root: string? }
@@ -129,7 +130,7 @@ The value should be one of the following:
129130
vim.inspect(stderr)
130131
)
131132
)
132-
stderr = cc_common.flatten_table_to_string(stderr)
133+
stderr = utils.flatten_table_to_string(stderr)
133134
tools.chat:add_tool_output(
134135
self,
135136
string.format("**VectorCode `vectorise` Tool: %s", stderr)

lua/vectorcode/jobrunner/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
local utils = require("vectorcode.utils")
22

3-
---@alias VectorCode.JobRunner.Callback fun(result: table, error: table, code:integer, signal: integer?)
3+
---@alias VectorCode.JobRunner.Callback fun(result: table|nil, error: table|nil, code:integer, signal: integer?)
44

55
--- A class for calling vectorcode commands that aims at providing a unified API for both LSP and command-line backend.
66
--- Implementations exist for both direct command-line execution (`cmd.lua`) and LSP (`lsp.lua`).
@@ -24,7 +24,7 @@ local utils = require("vectorcode.utils")
2424
--- - `error`: error messages, if any.
2525
--- - `code`: exit code (or error code) for the process.
2626
--- - `signal`: _for cmd runner only_, the shell signal sent to the process.
27-
---@field run fun(args: string[], timeout_ms: integer?, bufnr: integer):(result:table, error:table, code:integer, signal: integer?)
27+
---@field run fun(args: string[], timeout_ms: integer?, bufnr: integer):(result:table|nil, error:table|nil, code:integer, signal: integer?)
2828
--- Checks if a job associated with the given handle is currently running.
2929
--- Returns true if the job is running, false otherwise.
3030
---@field is_job_running fun(job_handle: integer):boolean

lua/vectorcode/utils.lua

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,4 +173,30 @@ function M.is_directory(f)
173173
return stats and (stats.type == "directory") or false
174174
end
175175

176+
---@param t table|string|nil
177+
---@return string
178+
M.flatten_table_to_string = function(t)
179+
if t == nil then
180+
return ""
181+
end
182+
if type(t) == "string" then
183+
return t
184+
end
185+
186+
-- Handle empty tables or tables with empty strings
187+
local flattened = vim
188+
.iter(t)
189+
:flatten(math.huge)
190+
:filter(function(item)
191+
return type(item) == "string" and vim.trim(item) ~= ""
192+
end)
193+
:totable()
194+
195+
if #flattened == 0 then
196+
return "Unknown error occurred"
197+
end
198+
199+
return table.concat(flattened, "\n")
200+
end
201+
176202
return M

0 commit comments

Comments
 (0)