Skip to content

Commit 379714d

Browse files
authored
feat: add GpChatLast command, to open the last chat (#234)
* Add GpChatLast command * Document GpChatLast * Correct typo
1 parent d02e5df commit 379714d

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

doc/gp.nvim.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,18 @@ subcommands for layout specification:
298298
- `:GpChatToggle popup` Toggle chat in a popup window.
299299

300300

301+
:GpChatLast *:GpChatLast*
302+
303+
Open the last active chat. Any visual selection or specified range will be
304+
pasted into the chat. This command also supports subcommands for layout
305+
specification:
306+
307+
- `:GpChatLast vsplit` Open the last chat in a vertical split window.
308+
- `:GpChatLast split` Open the last chat in a horizontal split window.
309+
- `:GpChatLast tabnew` Open the last chat in a new tab.
310+
- `:GpChatLast popup` Open the last chat in a popup window.
311+
312+
301313
:GpChatFinder *:GpChatFinder*
302314

303315
Open a dialog to search through chats.

lua/gp/init.lua

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ M.setup = function(opts)
193193
ChatNew = { "popup", "split", "vsplit", "tabnew" },
194194
ChatPaste = { "popup", "split", "vsplit", "tabnew" },
195195
ChatToggle = { "popup", "split", "vsplit", "tabnew" },
196+
ChatLast = { "popup", "split", "vsplit", "tabnew" },
196197
Context = { "popup", "split", "vsplit", "tabnew" },
197198
Agent = agent_completion,
198199
}
@@ -642,7 +643,7 @@ end
642643

643644
---@param file_name string
644645
---@param target number | nil # buf target
645-
---@param kind number # nil or a toggle kind
646+
---@param kind number | nil # nil or a toggle kind, must be toggle kind when toggle is true
646647
---@param toggle boolean # whether to toggle
647648
---@return number # buffer number
648649
M.open_buf = function(file_name, target, kind, toggle)
@@ -652,6 +653,7 @@ M.open_buf = function(file_name, target, kind, toggle)
652653
M._toggle_close(M._toggle_kind.popup)
653654

654655
if toggle then
656+
---@cast kind number
655657
M._toggle_close(kind)
656658
end
657659

@@ -865,6 +867,46 @@ M.cmd.ChatToggle = function(params, system_prompt, agent)
865867
M.new_chat(params, true, system_prompt, agent)
866868
end
867869

870+
---@param params table
871+
---@return number | nil # buffer number or nil if no last chat
872+
M.cmd.ChatLast = function(params)
873+
local toggle = false
874+
-- if the range is 2, we want to create a new chat file with the selection
875+
if M._toggle_close(M._toggle_kind.chat) then
876+
params.args = params.args or ""
877+
if params.args == "" then
878+
params.args = M.config.toggle_target
879+
end
880+
toggle = true
881+
end
882+
local last = M._state.last_chat
883+
if last and vim.fn.filereadable(last) == 1 then
884+
last = vim.fn.resolve(last)
885+
-- get current buffer, for pasting selection if necessary
886+
local cbuf = vim.api.nvim_get_current_buf()
887+
local buf = M.helpers.get_buffer(last)
888+
local win_found = false
889+
if buf then
890+
for _, w in ipairs(vim.api.nvim_list_wins()) do
891+
if vim.api.nvim_win_get_buf(w) == buf then
892+
vim.api.nvim_set_current_win(w)
893+
vim.api.nvim_set_current_buf(buf)
894+
win_found = true
895+
break
896+
end
897+
end
898+
end
899+
buf = win_found and buf or M.open_buf(last, M.resolve_buf_target(params), toggle and M._toggle_kind.chat or nil, toggle)
900+
-- if there is a selection, paste it
901+
if params.range == 2 then
902+
M.render.append_selection(params, cbuf, buf, M.config.template_selection)
903+
M.helpers.feedkeys("G", "xn")
904+
end
905+
return buf
906+
end
907+
return nil
908+
end
909+
868910
M.cmd.ChatPaste = function(params)
869911
-- if there is no selection, do nothing
870912
if params.range ~= 2 then

0 commit comments

Comments
 (0)