Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GpChatLast command, to open the last chat #234

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add GpChatLast command
  • Loading branch information
myarcana committed Dec 15, 2024
commit 4b3c512aa02c1e3ed0b5e7680b28812a641ba20f
43 changes: 42 additions & 1 deletion lua/gp/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ end

---@param file_name string
---@param target number | nil # buf target
---@param kind number # nil or a toggle kind
---@param kind number | nil # nil or a toggle kind, must be toggle kind when toggle is true
---@param toggle boolean # whether to toggle
---@return number # buffer number
M.open_buf = function(file_name, target, kind, toggle)
Expand All @@ -652,6 +652,7 @@ M.open_buf = function(file_name, target, kind, toggle)
M._toggle_close(M._toggle_kind.popup)

if toggle then
---@cast kind number
M._toggle_close(kind)
end

Expand Down Expand Up @@ -865,6 +866,46 @@ M.cmd.ChatToggle = function(params, system_prompt, agent)
M.new_chat(params, true, system_prompt, agent)
end

---@param params table
---@return number | nil # buffer number or nil if no last chat
M.cmd.ChatLast = function(params)
local toggle = false
-- if the range is 2, we want to create a new chat file with the selection
if M._toggle_close(M._toggle_kind.chat) then
params.args = params.args or ""
if params.args == "" then
params.args = M.config.toggle_target
end
toggle = true
end
local last = M._state.last_chat
if last and vim.fn.filereadable(last) == 1 then
last = vim.fn.resolve(last)
-- get current buffer, for pasting selection if necessary
local cbuf = vim.api.nvim_get_current_buf()
local buf = M.helpers.get_buffer(last)
local win_found = false
if buf then
for _, w in ipairs(vim.api.nvim_list_wins()) do
if vim.api.nvim_win_get_buf(w) == buf then
vim.api.nvim_set_current_win(w)
vim.api.nvim_set_current_buf(buf)
win_found = true
break
end
end
end
buf = win_found and buf or M.open_buf(last, M.resolve_buf_target(params), toggle and M._toggle_kind.chat or nil, toggle)
-- if there is a selection, paste it
if params.range ~= 2 then
M.render.append_selection(params, cbuf, buf, M.config.template_selection)
M.helpers.feedkeys("G", "xn")
end
return buf
end
return nil
end

M.cmd.ChatPaste = function(params)
-- if there is no selection, do nothing
if params.range ~= 2 then
Expand Down