Skip to content

Commit

Permalink
Support for rewrite/append/prepend with file
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyengstrom committed Jun 28, 2024
1 parent 486a061 commit 617b002
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 4 deletions.
21 changes: 21 additions & 0 deletions lua/gp/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,33 @@ local config = {
template_rewrite = "I have the following from {{filename}}:"
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
.. "\n\nRespond exclusively with the snippet that should replace the selection above.",
template_rewrite_with_file = "I have the following file {{filename}}:"
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
.. "\n\n I want to update the following code block"
.. "\n\n```{{filetype}}\n{{selection}}\n```"
.. "\n\nInstructions: "
.. "\n- {{command}}"
.. "\n- Respond exclusively with the snippet that should replace the selection above.",
template_append = "I have the following from {{filename}}:"
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
.. "\n\nRespond exclusively with the snippet that should be appended after the selection above.",
template_append_with_file = "I have the following file {{filename}}:"
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
.. "\n\n I want to append after the following code block"
.. "\n\n```{{filetype}}\n{{selection}}\n```"
.. "\n\nInstructions: "
.. "\n- {{command}}"
.. "\n- Respond exclusively with the snippet that should be appended after the selection above.",
template_prepend = "I have the following from {{filename}}:"
.. "\n\n```{{filetype}}\n{{selection}}\n```\n\n{{command}}"
.. "\n\nRespond exclusively with the snippet that should be prepended before the selection above.",
template_prepend_with_file = "I have the following file {{filename}}:"
.. "\n\n```{{filetype}}\n{{file_content}}\n```"
.. "\n\n I want to prepend before the following code block"
.. "\n\n```{{filetype}}\n{{selection}}\n```"
.. "\n\nInstructions: "
.. "\n- {{command}}"
.. "\n- Respond exclusively with the snippet that should be prepended after the selection above.",
template_command = "{{command}}",

-- https://platform.openai.com/docs/guides/speech-to-text/quickstart
Expand Down
35 changes: 31 additions & 4 deletions lua/gp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,7 @@ M.template_render = function(template, command, selection, filetype, filename)
["{{selection}}"] = selection,
["{{filetype}}"] = filetype,
["{{filename}}"] = filename,
["{{file_content}}"] = vim.api.nvim_buf_get_lines(0, 0, -1, false)
}
return _H.template_render(template, key_value_pairs)
end
Expand Down Expand Up @@ -916,6 +917,9 @@ M.Target = {
append = 1, -- for appending after the selection, range or the current line
prepend = 2, -- for prepending before the selection, range or the current line
popup = 3, -- for writing into the popup window
rewriteWithFile = 4, -- for replacing the selection, range or the current line with the full file as context
appendWithFile = 5, -- for appending after the selection, range or the current line with the full file as context
prependWithFile = 6, -- for appending after the selection, range or the current line with the full file as context

-- for writing into a new buffer
---@param filetype nil | string # nil = same as the original buffer
Expand Down Expand Up @@ -966,12 +970,16 @@ M.prepare_commands = function()
-- rewrite needs custom template
if target == M.Target.rewrite then
template = M.config.template_rewrite
end
if target == M.Target.append then
elseif target == M.Target.rewriteWithFile then
template = M.config.template_rewrite_with_file
elseif target == M.Target.append then
template = M.config.template_append
end
if target == M.Target.prepend then
elseif target == M.Target.appendWithFile then
template = M.config.template_append_with_file
elseif target == M.Target.prepend then
template = M.config.template_prepend
elseif target == M.Target.prependWithFile then
template = M.config.template_prepend_with_file
end
end
M.Prompt(params, target, agent.cmd_prefix, agent.model, template, agent.system_prompt, whisper)
Expand Down Expand Up @@ -2684,20 +2692,39 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh
vim.api.nvim_buf_set_lines(buf, start_line - 1, end_line - 1, false, {})
-- prepare handler
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
elseif target == M.Target.rewriteWithFile then
-- delete selection
vim.api.nvim_buf_set_lines(buf, start_line - 1, end_line - 1, false, {})
-- prepare handler
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
elseif target == M.Target.append then
-- move cursor to the end of the selection
vim.api.nvim_win_set_cursor(0, { end_line, 0 })
-- put newline after selection
vim.api.nvim_put({ "" }, "l", true, true)
-- prepare handler
handler = M.create_handler(buf, win, end_line, true, prefix, cursor)
elseif target == M.Target.appendWithFile then
-- move cursor to the end of the selection
vim.api.nvim_win_set_cursor(0, { end_line, 0 })
-- put newline after selection
vim.api.nvim_put({ "" }, "l", true, true)
-- prepare handler
handler = M.create_handler(buf, win, end_line, true, prefix, cursor)
elseif target == M.Target.prepend then
-- move cursor to the start of the selection
vim.api.nvim_win_set_cursor(0, { start_line, 0 })
-- put newline before selection
vim.api.nvim_put({ "" }, "l", false, true)
-- prepare handler
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
elseif target == M.Target.prependWithFile then
-- move cursor to the start of the selection
vim.api.nvim_win_set_cursor(0, { start_line, 0 })
-- put newline before selection
vim.api.nvim_put({ "" }, "l", false, true)
-- prepare handler
handler = M.create_handler(buf, win, start_line - 1, true, prefix, cursor)
elseif target == M.Target.popup then
M._toggle_close(M._toggle_kind.popup)
-- create a new buffer
Expand Down

0 comments on commit 617b002

Please sign in to comment.