Skip to content

Commit 19f1c03

Browse files
committed
feat: agent macro & rm ui.input for Prompt cmds
1 parent 5613541 commit 19f1c03

File tree

2 files changed

+100
-8
lines changed

2 files changed

+100
-8
lines changed

lua/gp/init.lua

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,17 @@ M.setup = function(opts)
200200

201201
local ft_completion = M.macro.build_completion({
202202
require("gp.macros.target_filetype"),
203+
require("gp.macros.agent"),
204+
})
205+
206+
local base_completion = M.macro.build_completion({
207+
require("gp.macros.agent"),
203208
})
204209

205210
M.logger.debug("ft_completion done")
206211

207212
local do_completion = M.macro.build_completion({
213+
require("gp.macros.agent"),
208214
require("gp.macros.target"),
209215
require("gp.macros.target_filetype"),
210216
require("gp.macros.target_filename"),
@@ -213,6 +219,7 @@ M.setup = function(opts)
213219
M.logger.debug("do_completion done")
214220

215221
M.command_parser = M.macro.build_parser({
222+
require("gp.macros.agent"),
216223
require("gp.macros.target"),
217224
require("gp.macros.target_filetype"),
218225
require("gp.macros.target_filename"),
@@ -231,6 +238,10 @@ M.setup = function(opts)
231238
New = ft_completion,
232239
Vnew = ft_completion,
233240
Tabnew = ft_completion,
241+
Rewrite = base_completion,
242+
Prepend = base_completion,
243+
Append = base_completion,
244+
Popup = base_completion,
234245
}
235246

236247
local updates = {
@@ -286,6 +297,9 @@ M.setup = function(opts)
286297
full_path = vim.fn.resolve(full_path)
287298
M.buffer_state.set(buf, "context_dir", full_path)
288299
end
300+
301+
local filename = vim.api.nvim_buf_get_name(buf)
302+
M.buffer_state.set(buf, "is_chat", M.not_chat(buf, filename) == nil)
289303
end,
290304
})
291305

@@ -396,6 +410,32 @@ M.Target = {
396410
end,
397411
}
398412

413+
---@param target number | table # target to get name for
414+
---@return string # name of the target
415+
---@return string | nil # filetype of the target, if applicable
416+
M.get_target_name = function(target)
417+
local names = {}
418+
for name, value in pairs(M.Target) do
419+
if type(value) == "number" then
420+
names[value] = name
421+
elseif type(value) == "function" then
422+
local result = value()
423+
if type(result) == "table" and result.type then
424+
names[result.type] = name
425+
end
426+
end
427+
end
428+
429+
if type(target) == "number" then
430+
return names[target] or "unknown"
431+
elseif type(target) == "table" and target.type then
432+
return names[target.type] or "unknown", target.filetype
433+
end
434+
435+
M.logger.error("Invalid target type: " .. vim.inspect(target))
436+
return "unknown"
437+
end
438+
399439
-- creates prompt commands for each target
400440
M.prepare_commands = function()
401441
for name, target in pairs(M.Target) do
@@ -1828,13 +1868,15 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
18281868
local filetype = M.helpers.get_filetype(buf)
18291869
local filename = vim.api.nvim_buf_get_name(buf)
18301870

1831-
local state = {}
1871+
local state = M.buffer_state.get(buf)
18321872
local response = M.command_parser(command, {}, state)
18331873
if response then
18341874
command = M.render.template(response.template, response.artifacts)
18351875
state = response.state
18361876
end
18371877

1878+
agent = state.agent or agent
1879+
18381880
local sys_prompt = M.render.prompt_template(agent.system_prompt, command, selection, filetype, filename)
18391881
sys_prompt = sys_prompt or ""
18401882
table.insert(messages, { role = "system", content = sys_prompt })
@@ -1964,13 +2006,20 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
19642006
return
19652007
end
19662008

1967-
-- if prompt is provided, ask the user to enter the command
1968-
vim.ui.input({ prompt = prompt, default = whisper }, function(input)
1969-
if not input or input == "" then
1970-
return
1971-
end
1972-
cb(input)
1973-
end)
2009+
-- old shortcuts might produce stuff like `:GpRewrite<cr>` this
2010+
-- used to be handled by vim.ui.input, which has trouble with completion
2011+
local command = ":" .. start_line .. "," .. end_line .. "Gp"
2012+
local targetName, filetype = M.get_target_name(target)
2013+
targetName = targetName:gsub("^%l", string.upper)
2014+
command = command .. targetName
2015+
command = command .. " @agent " .. agent.name
2016+
filetype = filetype and " @target_filetype " .. filetype or ""
2017+
command = command .. filetype
2018+
whisper = whisper and " " .. whisper or ""
2019+
command = command .. whisper
2020+
command = command .. " "
2021+
2022+
vim.api.nvim_feedkeys(command, "n", false)
19742023
end)
19752024
end
19762025

lua/gp/macros/agent.lua

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
local macro = require("gp.macro")
2+
local gp = require("gp")
3+
4+
local M = {}
5+
6+
---@type gp.Macro
7+
M = {
8+
name = "agent",
9+
description = "handles agent selection for commands",
10+
default = "",
11+
max_occurrences = 1,
12+
13+
triggered = function(params)
14+
return params.cropped_line:match("@agent%s+%S*$")
15+
end,
16+
17+
completion = function(params)
18+
if params.state.is_chat then
19+
return gp._chat_agents
20+
end
21+
return gp._command_agents
22+
end,
23+
24+
parser = function(result)
25+
local template = result.template
26+
local s, e, value = template:find("@agent%s+(%S+)")
27+
if not value then
28+
return result
29+
end
30+
31+
local placeholder = macro.generate_placeholder(M.name, value)
32+
result.template = template:sub(1, s - 2) .. placeholder .. template:sub(e + 1)
33+
if result.state.is_chat then
34+
result.state[M.name] = gp.get_chat_agent(value)
35+
else
36+
result.state[M.name] = gp.get_command_agent(value)
37+
end
38+
result.artifacts[placeholder] = ""
39+
return result
40+
end,
41+
}
42+
43+
return M

0 commit comments

Comments
 (0)