Skip to content

Commit

Permalink
fix: improve chat buffer rendering and state handling
Browse files Browse the repository at this point in the history
- Make chat buffer non-modifiable by default and only allow modifications
  during content updates and after response is finished
- Join response progress undos together so user can undo whole ask/response at once
- Rerender headers in chat window on text change automatically, which means
  that when users accidentally edits the separators in buffer the highlights do not
  break until next question is asked
  • Loading branch information
deathbeam committed Nov 20, 2024
1 parent 79896c4 commit b6150bc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lua/CopilotChat/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,16 @@ local Chat = class(function(self, help, on_buf_create)
vim.bo[bufnr].filetype = self.name
vim.bo[bufnr].syntax = 'markdown'
vim.bo[bufnr].textwidth = 0
vim.bo[bufnr].modifiable = false

vim.api.nvim_create_autocmd({ 'TextChanged', 'TextChangedI' }, {
buffer = bufnr,
callback = function()
vim.defer_fn(function()
self:render()
end, 100)
end,
})

if not self.spinner then
self.spinner = Spinner(bufnr)
Expand Down Expand Up @@ -118,6 +128,7 @@ end

function Chat:append(str)
self:validate()
vim.bo[self.bufnr].modifiable = true

if self:active() then
utils.return_to_normal_mode()
Expand Down Expand Up @@ -145,17 +156,19 @@ function Chat:append(str)
last_column,
vim.split(str, '\n')
)
self:render()

if should_follow_cursor then
self:follow()
end

vim.bo[self.bufnr].modifiable = false
end

function Chat:clear()
self:validate()
vim.bo[self.bufnr].modifiable = true
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {})
self:render()
vim.bo[self.bufnr].modifiable = false
end

function Chat:open(config)
Expand Down Expand Up @@ -233,7 +246,6 @@ function Chat:open(config)
else
vim.wo[self.winnr].foldcolumn = '0'
end
self:render()
end

function Chat:close(bufnr)
Expand Down Expand Up @@ -295,6 +307,7 @@ function Chat:finish(msg, offset)
msg = self.help
end

vim.bo[self.bufnr].modifiable = true
self:show_help(msg, -offset)
if self.auto_insert and self:active() then
vim.cmd('startinsert')
Expand Down
1 change: 1 addition & 0 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,7 @@ function M.ask(prompt, config)
on_progress = function(token)
vim.schedule(function()
if not config.no_chat then
vim.cmd('undojoin')
state.chat:append(token)
end

Expand Down
1 change: 1 addition & 0 deletions lua/CopilotChat/overlay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local Overlay = class(function(self, name, help, on_buf_create)
self.buf_create = function()
local bufnr = vim.api.nvim_create_buf(false, true)
vim.bo[bufnr].filetype = name
vim.bo[bufnr].modifiable = false
vim.api.nvim_buf_set_name(bufnr, name)
return bufnr
end
Expand Down

0 comments on commit b6150bc

Please sign in to comment.