@@ -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
@@ -1827,13 +1867,15 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
1827
1867
local filetype = M .helpers .get_filetype (buf )
1828
1868
local filename = vim .api .nvim_buf_get_name (buf )
1829
1869
1830
- local state = {}
1870
+ local state = M . buffer_state . get ( buf )
1831
1871
local response = M .command_parser (command , {}, state )
1832
1872
if response then
1833
1873
command = M .render .template (response .template , response .artifacts )
1834
1874
state = response .state
1835
1875
end
1836
1876
1877
+ agent = state .agent or agent
1878
+
1837
1879
local sys_prompt = M .render .prompt_template (agent .system_prompt , command , selection , filetype , filename )
1838
1880
sys_prompt = sys_prompt or " "
1839
1881
table.insert (messages , { role = " system" , content = sys_prompt })
@@ -1963,13 +2005,20 @@ M.Prompt = function(params, target, agent, template, prompt, whisper, callback)
1963
2005
return
1964
2006
end
1965
2007
1966
- -- if prompt is provided, ask the user to enter the command
1967
- vim .ui .input ({ prompt = prompt , default = whisper }, function (input )
1968
- if not input or input == " " then
1969
- return
1970
- end
1971
- cb (input )
1972
- end )
2008
+ -- old shortcuts might produce stuff like `:GpRewrite<cr>` this
2009
+ -- used to be handled by vim.ui.input, which has trouble with completion
2010
+ local command = " :" .. start_line .. " ," .. end_line .. " Gp"
2011
+ local targetName , filetype = M .get_target_name (target )
2012
+ targetName = targetName :gsub (" ^%l" , string.upper )
2013
+ command = command .. targetName
2014
+ command = command .. " @agent " .. agent .name
2015
+ filetype = filetype and " @target_filetype " .. filetype or " "
2016
+ command = command .. filetype
2017
+ whisper = whisper and " " .. whisper or " "
2018
+ command = command .. whisper
2019
+ command = command .. " "
2020
+
2021
+ vim .api .nvim_feedkeys (command , " n" , false )
1973
2022
end )
1974
2023
end
1975
2024
0 commit comments