Skip to content

Commit 8050123

Browse files
authored
feat(nvim): use codecompanion extension API. (#99)
* feat(nvim): use codecompanion extension API. * add default options for extension, as well as slash command. * add duplicate-detection and logging.
1 parent 397b6f6 commit 8050123

File tree

1 file changed

+74
-0
lines changed
  • lua/codecompanion/_extensions/vectorcode

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
---@module "codecompanion"
2+
3+
---@class VectorCode.CodeCompanion.ExtensionOpts
4+
---@field add_tools boolean|string|nil
5+
---@field tool_opts VectorCode.CodeCompanion.ToolOpts
6+
---@field add_slash_command boolean
7+
8+
local vc_config = require("vectorcode.config")
9+
local logger = vc_config.logger
10+
11+
---@type CodeCompanion.Extension
12+
local M = {
13+
---@param opts VectorCode.CodeCompanion.ExtensionOpts
14+
setup = vc_config.check_cli_wrap(function(opts)
15+
opts = vim.tbl_deep_extend(
16+
"force",
17+
{ add_tool = true, add_slash_command = false },
18+
opts or {}
19+
)
20+
logger.info("Received codecompanion extension opts:\n", opts)
21+
local cc_config = require("codecompanion.config").config
22+
local cc_integration = require("vectorcode.integrations").codecompanion.chat
23+
if opts.add_tool then
24+
local tool_name = "vectorcode"
25+
if type(opts.add_tool) == "string" then
26+
tool_name = tostring(opts.add_tool)
27+
end
28+
if cc_config.strategies.chat.tools[tool_name] ~= nil then
29+
vim.notify(
30+
"There's an existing tool named `vectorcode`. Please either remove it or rename it.",
31+
vim.log.levels.ERROR,
32+
vc_config.notify_opts
33+
)
34+
logger.warn(
35+
string.format(
36+
"Not creating a tool because there is an existing tool named %s.",
37+
tool_name
38+
)
39+
)
40+
else
41+
cc_config.strategies.chat.tools[tool_name] = {
42+
description = "Run VectorCode to retrieve the project context.",
43+
callback = cc_integration.make_tool(opts.tool_opts),
44+
}
45+
logger.info(string.format("%s tool has been created.", tool_name))
46+
end
47+
end
48+
if opts.add_slash_command then
49+
local command_name = "vectorcode"
50+
if type(opts.add_slash_command) == "string" then
51+
command_name = tostring(opts.add_slash_command)
52+
end
53+
if cc_config.strategies.chat.slash_commands[command_name] ~= nil then
54+
vim.notify(
55+
"There's an existing slash command named `vectorcode`. Please either remove it or rename it.",
56+
vim.log.levels.ERROR,
57+
vc_config.notify_opts
58+
)
59+
logger.warn(
60+
string.format(
61+
"Not creating a command because there is an existing slash command named %s.",
62+
command_name
63+
)
64+
)
65+
else
66+
cc_config.strategies.chat.slash_commands[command_name] =
67+
cc_integration.make_slash_command()
68+
logger.info(string.format("%s command has been created.", command_name))
69+
end
70+
end
71+
end),
72+
}
73+
74+
return M

0 commit comments

Comments
 (0)