@@ -200,11 +200,17 @@ M.setup = function(opts)
200
200
201
201
local ft_completion = M .macro .build_completion ({
202
202
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" ),
203
208
})
204
209
205
210
M .logger .debug (" ft_completion done" )
206
211
207
212
local do_completion = M .macro .build_completion ({
213
+ require (" gp.macros.agent" ),
208
214
require (" gp.macros.target" ),
209
215
require (" gp.macros.target_filetype" ),
210
216
require (" gp.macros.target_filename" ),
@@ -213,6 +219,7 @@ M.setup = function(opts)
213
219
M .logger .debug (" do_completion done" )
214
220
215
221
M .command_parser = M .macro .build_parser ({
222
+ require (" gp.macros.agent" ),
216
223
require (" gp.macros.target" ),
217
224
require (" gp.macros.target_filetype" ),
218
225
require (" gp.macros.target_filename" ),
@@ -231,6 +238,10 @@ M.setup = function(opts)
231
238
New = ft_completion ,
232
239
Vnew = ft_completion ,
233
240
Tabnew = ft_completion ,
241
+ Rewrite = base_completion ,
242
+ Prepend = base_completion ,
243
+ Append = base_completion ,
244
+ Popup = base_completion ,
234
245
}
235
246
236
247
local updates = {
@@ -286,6 +297,9 @@ M.setup = function(opts)
286
297
full_path = vim .fn .resolve (full_path )
287
298
M .buffer_state .set (buf , " context_dir" , full_path )
288
299
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 )
289
303
end ,
290
304
})
291
305
@@ -396,6 +410,32 @@ M.Target = {
396
410
end ,
397
411
}
398
412
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
+
399
439
-- creates prompt commands for each target
400
440
M .prepare_commands = function ()
401
441
for name , target in pairs (M .Target ) do
@@ -1828,13 +1868,15 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
1828
1868
local filetype = M .helpers .get_filetype (buf )
1829
1869
local filename = vim .api .nvim_buf_get_name (buf )
1830
1870
1831
- local state = {}
1871
+ local state = M . buffer_state . get ( buf )
1832
1872
local response = M .command_parser (command , {}, state )
1833
1873
if response then
1834
1874
command = M .render .template (response .template , response .artifacts )
1835
1875
state = response .state
1836
1876
end
1837
1877
1878
+ agent = state .agent or agent
1879
+
1838
1880
local sys_prompt = M .render .prompt_template (agent .system_prompt , command , selection , filetype , filename )
1839
1881
sys_prompt = sys_prompt or " "
1840
1882
table.insert (messages , { role = " system" , content = sys_prompt })
@@ -1964,13 +2006,20 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
1964
2006
return
1965
2007
end
1966
2008
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 )
1974
2023
end )
1975
2024
end
1976
2025
0 commit comments