Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: legendary.nvim extension #17

Merged
merged 5 commits into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions lua/codecompanion/commands.lua
Original file line number Diff line number Diff line change
@@ -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" },
},
}
40 changes: 40 additions & 0 deletions lua/legendary/extensions/codecompanion.lua
Original file line number Diff line number Diff line change
@@ -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
31 changes: 4 additions & 27 deletions plugin/codecompanion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading