diff --git a/lua/gp/config.lua b/lua/gp/config.lua index 4ff72a4..9c38f6e 100644 --- a/lua/gp/config.lua +++ b/lua/gp/config.lua @@ -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 diff --git a/lua/gp/init.lua b/lua/gp/init.lua index 4cc668c..005274f 100644 --- a/lua/gp/init.lua +++ b/lua/gp/init.lua @@ -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 @@ -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 @@ -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) @@ -2684,6 +2692,11 @@ 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 }) @@ -2691,6 +2704,13 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh 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 }) @@ -2698,6 +2718,13 @@ M.Prompt = function(params, target, prompt, model, template, system_template, wh 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