Skip to content

Commit a803746

Browse files
author
Zhe Yu
committed
refactor(nvim): Move flatten_table_to_string to vectorcode.utils
1 parent 0639d1e commit a803746

8 files changed

Lines changed: 47 additions & 29 deletions

File tree

lua/vectorcode/init.lua

Lines changed: 3 additions & 2 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
@@ -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 or {}):flatten(math.huge):totable()),
195-
stderr = table.concat(vim.iter(error or {}):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: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,14 @@ return {
1313
---@param t table|string|nil
1414
---@return string
1515
flatten_table_to_string = function(t)
16-
if t == nil then
17-
return ""
18-
end
19-
if type(t) == "string" then
20-
return t
21-
end
22-
23-
-- Handle empty tables or tables with empty strings
24-
local flattened = vim
25-
.iter(t)
26-
:flatten(math.huge)
27-
:filter(function(item)
28-
return type(item) == "string" and vim.trim(item) ~= ""
29-
end)
30-
:totable()
31-
32-
if #flattened == 0 then
33-
return "Unknown error occurred"
34-
end
35-
36-
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)
3724
end,
3825

3926
---@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 & 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
---@type VectorCode.CodeCompanion.LsToolOpts
@@ -45,7 +46,7 @@ return function(opts)
4546
cb({ status = "success", data = result })
4647
else
4748
if type(error) == "table" then
48-
error = cc_common.flatten_table_to_string(error)
49+
error = utils.flatten_table_to_string(error)
4950
end
5051
cb({
5152
status = "error",

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ Here's my question:
132132
vc_config.notify_opts
133133
)
134134
elseif err ~= nil then
135-
err = cc_common.flatten_table_to_string(err)
135+
err = utils.flatten_table_to_string(err)
136136
if err ~= "" then
137137
vim.schedule_wrap(vim.notify)(
138138
err,

lua/vectorcode/integrations/codecompanion/query_tool.lua

Lines changed: 3 additions & 2 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,7 +464,7 @@ 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

468469
if
469470
result ~= nil
@@ -603,7 +604,7 @@ DO NOT MODIFY UNLESS INSTRUCTED BY THE USER, OR A PREVIOUS QUERY RETURNED NO RES
603604
vim.inspect(stderr)
604605
)
605606
)
606-
stderr = cc_common.flatten_table_to_string(stderr)
607+
stderr = utils.flatten_table_to_string(stderr)
607608
if string.find(stderr, "InvalidCollectionException") then
608609
if cmd.project_root then
609610
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/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)