Skip to content

Commit 617b002

Browse files
committed
Support for rewrite/append/prepend with file
1 parent 486a061 commit 617b002

File tree

2 files changed

+52
-4
lines changed

2 files changed

+52
-4
lines changed

lua/gp/config.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,33 @@ local config = {
161161
template_rewrite = "I have the following from {{filename}}:"
162162
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
163163
.. "\n\nRespond exclusively with the snippet that should replace the selection above.",
164+
template_rewrite_with_file = "I have the following file {{filename}}:"
165+
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
166+
.. "\n\n I want to update the following code block"
167+
.. "\n\n```{{filetype}}\n{{selection}}\n```"
168+
.. "\n\nInstructions: "
169+
.. "\n- {{command}}"
170+
.. "\n- Respond exclusively with the snippet that should replace the selection above.",
164171
template_append = "I have the following from {{filename}}:"
165172
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
166173
.. "\n\nRespond exclusively with the snippet that should be appended after the selection above.",
174+
template_append_with_file = "I have the following file {{filename}}:"
175+
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
176+
.. "\n\n I want to append after the following code block"
177+
.. "\n\n```{{filetype}}\n{{selection}}\n```"
178+
.. "\n\nInstructions: "
179+
.. "\n- {{command}}"
180+
.. "\n- Respond exclusively with the snippet that should be appended after the selection above.",
167181
template_prepend = "I have the following from {{filename}}:"
168182
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
169183
.. "\n\nRespond exclusively with the snippet that should be prepended before the selection above.",
184+
template_prepend_with_file = "I have the following file {{filename}}:"
185+
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
186+
.. "\n\n I want to prepend before the following code block"
187+
.. "\n\n```{{filetype}}\n{{selection}}\n```"
188+
.. "\n\nInstructions: "
189+
.. "\n- {{command}}"
190+
.. "\n- Respond exclusively with the snippet that should be prepended after the selection above.",
170191
template_command = "{{command}}",
171192

172193
-- https://platform.openai.com/docs/guides/speech-to-text/quickstart

lua/gp/init.lua

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ M.template_render = function(template, command, selection, filetype, filename)
628628
["{{selection}}"] = selection,
629629
["{{filetype}}"] = filetype,
630630
["{{filename}}"] = filename,
631+
["{{file_content}}"] = vim.api.nvim_buf_get_lines(0, 0, -1, false)
631632
}
632633
return _H.template_render(template, key_value_pairs)
633634
end
@@ -916,6 +917,9 @@ M.Target = {
916917
append = 1, -- for appending after the selection, range or the current line
917918
prepend = 2, -- for prepending before the selection, range or the current line
918919
popup = 3, -- for writing into the popup window
920+
rewriteWithFile = 4, -- for replacing the selection, range or the current line with the full file as context
921+
appendWithFile = 5, -- for appending after the selection, range or the current line with the full file as context
922+
prependWithFile = 6, -- for appending after the selection, range or the current line with the full file as context
919923

920924
-- for writing into a new buffer
921925
---@param filetype nil | string # nil = same as the original buffer
@@ -966,12 +970,16 @@ M.prepare_commands = function()
966970
-- rewrite needs custom template
967971
if target == M.Target.rewrite then
968972
template = M.config.template_rewrite
969-
end
970-
if target == M.Target.append then
973+
elseif target == M.Target.rewriteWithFile then
974+
template = M.config.template_rewrite_with_file
975+
elseif target == M.Target.append then
971976
template = M.config.template_append
972-
end
973-
if target == M.Target.prepend then
977+
elseif target == M.Target.appendWithFile then
978+
template = M.config.template_append_with_file
979+
elseif target == M.Target.prepend then
974980
template = M.config.template_prepend
981+
elseif target == M.Target.prependWithFile then
982+
template = M.config.template_prepend_with_file
975983
end
976984
end
977985
M.Prompt(params, target, agent.cmd_prefix, agent.model, template, agent.system_prompt, whisper)
@@ -2684,20 +2692,39 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh
26842692
vim.api.nvim_buf_set_lines(buf, start_line - 1, end_line - 1, false, {})
26852693
-- prepare handler
26862694
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
2695+
elseif target == M.Target.rewriteWithFile then
2696+
-- delete selection
2697+
vim.api.nvim_buf_set_lines(buf, start_line - 1, end_line - 1, false, {})
2698+
-- prepare handler
2699+
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
26872700
elseif target == M.Target.append then
26882701
-- move cursor to the end of the selection
26892702
vim.api.nvim_win_set_cursor(0, { end_line, 0 })
26902703
-- put newline after selection
26912704
vim.api.nvim_put({ "" }, "l", true, true)
26922705
-- prepare handler
26932706
handler = M.create_handler(buf, win, end_line, true, prefix, cursor)
2707+
elseif target == M.Target.appendWithFile then
2708+
-- move cursor to the end of the selection
2709+
vim.api.nvim_win_set_cursor(0, { end_line, 0 })
2710+
-- put newline after selection
2711+
vim.api.nvim_put({ "" }, "l", true, true)
2712+
-- prepare handler
2713+
handler = M.create_handler(buf, win, end_line, true, prefix, cursor)
26942714
elseif target == M.Target.prepend then
26952715
-- move cursor to the start of the selection
26962716
vim.api.nvim_win_set_cursor(0, { start_line, 0 })
26972717
-- put newline before selection
26982718
vim.api.nvim_put({ "" }, "l", false, true)
26992719
-- prepare handler
27002720
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
2721+
elseif target == M.Target.prependWithFile then
2722+
-- move cursor to the start of the selection
2723+
vim.api.nvim_win_set_cursor(0, { start_line, 0 })
2724+
-- put newline before selection
2725+
vim.api.nvim_put({ "" }, "l", false, true)
2726+
-- prepare handler
2727+
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
27012728
elseif target == M.Target.popup then
27022729
M._toggle_close(M._toggle_kind.popup)
27032730
-- create a new buffer

0 commit comments

Comments
 (0)