Skip to content

Commit

Permalink
feat: Add method and command to stop current copilot output (#308)
Browse files Browse the repository at this point in the history
See #307

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
  • Loading branch information
deathbeam authored May 8, 2024
1 parent 9410233 commit 8b67145
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ See @deathbeam for [configuration](https://github.com/deathbeam/dotfiles/blob/ma
- `:CopilotChatOpen` - Open chat window
- `:CopilotChatClose` - Close chat window
- `:CopilotChatToggle` - Toggle chat window
- `:CopilotChatStop` - Stop current copilot output
- `:CopilotChatReset` - Reset chat window
- `:CopilotChatSave <name>?` - Save chat history to file
- `:CopilotChatLoad <name>?` - Load chat history from file
Expand Down
39 changes: 30 additions & 9 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ function M.ask(prompt, config, source)
M.open(config, source, true)

if config.clear_chat_on_new_prompt then
M.reset(true)
M.stop(true, true)
end

state.last_system_prompt = system_prompt
Expand Down Expand Up @@ -442,10 +442,11 @@ function M.ask(prompt, config, source)
})
end

--- Reset the chat window and show the help message.
function M.reset(no_insert)
--- Stop current copilot output and optionally reset the chat ten show the help message.
---@param reset boolean?
function M.stop(reset, no_insert)
state.response = nil
local stopped = state.copilot:reset()
local stopped = reset and state.copilot:reset() or state.copilot:stop()
local wrap = vim.schedule
if not stopped then
wrap = function(fn)
Expand All @@ -454,7 +455,11 @@ function M.reset(no_insert)
end

wrap(function()
state.chat:clear()
if reset then
state.chat:clear()
else
append('\n\n')
end
append(M.config.question_header .. M.config.separator .. '\n\n')
state.chat:finish()
state.chat:follow()
Expand All @@ -465,6 +470,11 @@ function M.reset(no_insert)
end)
end

--- Reset the chat window and show the help message.
function M.reset()
M.stop(true)
end

--- Save the chat history to a file.
---@param name string?
---@param history_path string?
Expand Down Expand Up @@ -815,10 +825,21 @@ function M.setup(config)
range = true,
})

vim.api.nvim_create_user_command('CopilotChatOpen', M.open, { force = true })
vim.api.nvim_create_user_command('CopilotChatClose', M.close, { force = true })
vim.api.nvim_create_user_command('CopilotChatToggle', M.toggle, { force = true })
vim.api.nvim_create_user_command('CopilotChatReset', M.reset, { force = true })
vim.api.nvim_create_user_command('CopilotChatOpen', function()
M.open()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatClose', function()
M.close()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatToggle', function()
M.toggle()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatStop', function()
M.stop()
end, { force = true })
vim.api.nvim_create_user_command('CopilotChatReset', function()
M.reset()
end, { force = true })

local function complete_load()
local options = vim.tbl_map(function(file)
Expand Down

0 comments on commit 8b67145

Please sign in to comment.