diff --git a/lua/codecompanion/commands.lua b/lua/codecompanion/commands.lua new file mode 100644 index 00000000..bd05cb38 --- /dev/null +++ b/lua/codecompanion/commands.lua @@ -0,0 +1,61 @@ +---@class CodeCompanionCommandOpts:table +---@field desc string + +---@class CodeCompanionCommand +---@field cmd string +---@field callback fun(args:table) +---@field opts CodeCompanionCommandOpts + +local codecompanion = require("codecompanion") + +---@type CodeCompanionCommand[] +return { + { + cmd = "CodeCompanion", + callback = function(opts) + if #vim.trim(opts.args or "") == 0 then + vim.ui.input({ prompt = "Prompt" }, function(input) + if #vim.trim(input or "") == 0 then + return + end + opts.args = input + codecompanion.inline(opts) + end) + else + codecompanion.inline(opts) + end + end, + opts = { + desc = "Trigger CodeCompanion inline", + range = true, + nargs = "*", + }, + }, + { + cmd = "CodeCompanionChat", + callback = function(opts) + codecompanion.chat(opts) + end, + opts = { + desc = "Open a CodeCompanion chat buffer", + range = true, + }, + }, + { + cmd = "CodeCompanionActions", + callback = function(opts) + codecompanion.actions(opts) + end, + opts = { + desc = "Open the CodeCompanion actions palette", + range = true, + }, + }, + { + cmd = "CodeCompanionToggle", + callback = function() + codecompanion.toggle() + end, + opts = { desc = "Toggle a CodeCompanion chat buffer" }, + }, +} diff --git a/lua/legendary/extensions/codecompanion.lua b/lua/legendary/extensions/codecompanion.lua new file mode 100644 index 00000000..d26bfadf --- /dev/null +++ b/lua/legendary/extensions/codecompanion.lua @@ -0,0 +1,40 @@ +local function to_legendary_keymap(key, keymap) + return { + key, + -- prefix makes it easier to search in legendary.nvim window + desc = string.format("codecompanion.nvim: %s", require("legendary.util").get_desc(keymap)), + -- keymaps are all for the chat buffer + filters = { filetype = "codecompanion" }, + } +end + +---@param cmd CodeCompanionCommand +local function to_legendary_cmd(cmd) + return { + cmd.cmd, + desc = cmd.opts.desc, + } +end + +return function() + require("legendary.extensions").pre_ui_hook(function() + local keys = require("codecompanion.config").options.keymaps + local legendary_keys = {} + for lhs, rhs in pairs(keys) do + if type(rhs) == "string" and vim.startswith(rhs, "keymaps.") then + rhs = require("codecompanion.keymaps")[vim.split(rhs, ".", { plain = true })[2]] + end + table.insert(legendary_keys, to_legendary_keymap(lhs, rhs)) + end + + local legendary_cmds = {} + for _, cmd in ipairs(require("codecompanion.commands")) do + table.insert(legendary_cmds, to_legendary_cmd(cmd)) + end + + local legendary = require("legendary") + legendary.keymaps(legendary_keys) + legendary.commands(legendary_cmds) + return true + end) +end diff --git a/plugin/codecompanion.lua b/plugin/codecompanion.lua index 56a84811..2085cb83 100644 --- a/plugin/codecompanion.lua +++ b/plugin/codecompanion.lua @@ -6,32 +6,9 @@ if vim.g.loaded_codecompanion then return end -local codecompanion = require("codecompanion") - -vim.api.nvim_create_user_command("CodeCompanion", function(opts) - if #vim.trim(opts.args or "") == 0 then - vim.ui.input({ prompt = "Prompt" }, function(input) - if #vim.trim(input or "") == 0 then - return - end - opts.args = input - codecompanion.inline(opts) - end) - else - codecompanion.inline(opts) - end -end, { desc = "Trigger CodeCompanion inline", range = true, nargs = "*" }) - -vim.api.nvim_create_user_command("CodeCompanionChat", function(opts) - codecompanion.chat(opts) -end, { desc = "Open a CodeCompanion chat buffer", range = true }) - -vim.api.nvim_create_user_command("CodeCompanionActions", function(opts) - codecompanion.actions(opts) -end, { desc = "Open the CodeCompanion actions palette", range = true }) - -vim.api.nvim_create_user_command("CodeCompanionToggle", function() - codecompanion.toggle() -end, { desc = "Toggle a CodeCompanion chat buffer" }) +local cmds = require("codecompanion.commands") +for _, cmd in ipairs(cmds) do + vim.api.nvim_create_user_command(cmd.cmd, cmd.callback, cmd.opts) +end vim.g.loaded_codecompanion = true