Skip to content

Commit f694c8b

Browse files
feat: Expose last response and add example for buffer-local keymaps (#204)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 453b0d5 commit f694c8b

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ vim.api.nvim_create_autocmd('BufEnter', {
335335
pattern = 'copilot-*',
336336
callback = function()
337337
vim.opt_local.relativenumber = true
338+
339+
-- C-p to print last response
340+
vim.keymap.set('n', '<C-p>', function()
341+
print(require("CopilotChat").response())
342+
end, { buffer = true, remap = true })
338343
end
339344
})
340345
```

lua/CopilotChat/init.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ local plugin_name = 'CopilotChat.nvim'
1818
--- @field config CopilotChat.config?
1919
--- @field last_system_prompt string?
2020
--- @field last_code_output string?
21+
--- @field response string?
2122
--- @field diff CopilotChat.Overlay?
2223
--- @field system_prompt CopilotChat.Overlay?
2324
--- @field user_selection CopilotChat.Overlay?
@@ -31,6 +32,9 @@ local state = {
3132
last_system_prompt = nil,
3233
last_code_output = nil,
3334

35+
-- Response for mappings
36+
response = nil,
37+
3438
-- Overlays
3539
diff = nil,
3640
system_prompt = nil,
@@ -268,6 +272,11 @@ function M.toggle(config, source)
268272
end
269273
end
270274

275+
-- @returns string
276+
function M.response()
277+
return state.response
278+
end
279+
271280
--- Ask a question to the Copilot model.
272281
---@param prompt string
273282
---@param config CopilotChat.config|nil
@@ -342,6 +351,7 @@ function M.ask(prompt, config, source)
342351
on_done = function(response, token_count)
343352
vim.schedule(function()
344353
append('\n\n' .. config.separator .. '\n\n')
354+
state.response = response
345355
if tiktoken.available() and token_count and token_count > 0 then
346356
state.chat:finish(token_count .. ' tokens used')
347357
else
@@ -364,6 +374,7 @@ end
364374

365375
--- Reset the chat window and show the help message.
366376
function M.reset()
377+
state.response = nil
367378
state.copilot:reset()
368379
vim.schedule(function()
369380
state.chat:clear()

0 commit comments

Comments
 (0)