Skip to content

Commit 4044c31

Browse files
committed
refactor: better create cmd helper
1 parent 6dca8ea commit 4044c31

File tree

2 files changed

+37
-20
lines changed

2 files changed

+37
-20
lines changed

lua/gp/helper.lua

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,16 +255,35 @@ end
255255

256256
---@param cmd_name string # name of the command
257257
---@param cmd_func function # function to be executed when the command is called
258-
---@param completion_func function | nil # optional function returning table for completion
259-
---@param desc string | nil # description of the command
260-
_H.create_user_command = function(cmd_name, cmd_func, completion_func, desc)
258+
---@param completion function | table | nil # optional function returning table for completion
259+
---@param desc string | nil # description of the command
260+
_H.create_user_command = function(cmd_name, cmd_func, completion, desc)
261261
logger.debug("creating user command: " .. cmd_name)
262262
vim.api.nvim_create_user_command(cmd_name, cmd_func, {
263-
nargs = "?",
263+
nargs = "*",
264264
range = true,
265265
desc = desc or "Gp.nvim command",
266-
complete = function()
267-
return completion_func and completion_func() or {}
266+
complete = function(arg_lead, cmd_line, cursor_pos)
267+
logger.debug(
268+
"completing user command: "
269+
.. cmd_name
270+
.. "\narg_lead: "
271+
.. arg_lead
272+
.. "\ncmd_line: "
273+
.. cmd_line
274+
.. "\ncursor_pos: "
275+
.. cursor_pos
276+
)
277+
if not completion then
278+
return {}
279+
end
280+
if type(completion) == "function" then
281+
return completion(arg_lead, cmd_line, cursor_pos) or {}
282+
end
283+
if type(completion) == "table" then
284+
return completion
285+
end
286+
return {}
268287
end,
269288
})
270289
end

lua/gp/init.lua

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ local M = {
3030
-- Module helper functions and variables
3131
--------------------------------------------------------------------------------
3232

33+
local agent_completion = function()
34+
local buf = vim.api.nvim_get_current_buf()
35+
local file_name = vim.api.nvim_buf_get_name(buf)
36+
if M.not_chat(buf, file_name) == nil then
37+
return M._chat_agents
38+
end
39+
return M._command_agents
40+
end
41+
3342
-- setup function
3443
M._setup_called = false
3544
---@param opts table | nil # table with options
@@ -175,25 +184,14 @@ M.setup = function(opts)
175184
ChatPaste = { "popup", "split", "vsplit", "tabnew" },
176185
ChatToggle = { "popup", "split", "vsplit", "tabnew" },
177186
Context = { "popup", "split", "vsplit", "tabnew" },
187+
Agent = agent_completion,
178188
}
179189

180190
-- register default commands
191+
M.helpers.create_user_command(M.config.cmd_prefix .. "Do", M.cmd.Do, do_completion)
181192
for cmd, _ in pairs(M.cmd) do
182193
if M.hooks[cmd] == nil then
183-
M.helpers.create_user_command(M.config.cmd_prefix .. cmd, M.cmd[cmd], function()
184-
if completions[cmd] then
185-
return completions[cmd]
186-
end
187-
if cmd == "Agent" then
188-
local buf = vim.api.nvim_get_current_buf()
189-
local file_name = vim.api.nvim_buf_get_name(buf)
190-
if M.not_chat(buf, file_name) == nil then
191-
return M._chat_agents
192-
end
193-
return M._command_agents
194-
end
195-
return {}
196-
end)
194+
M.helpers.create_user_command(M.config.cmd_prefix .. cmd, M.cmd[cmd], completions[cmd])
197195
end
198196
end
199197

0 commit comments

Comments
 (0)