Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 2 additions & 13 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -729,19 +729,6 @@ function M.open(config)
utils.return_to_normal_mode()

M.chat:open(config)

local message = M.chat:get_message('user')
if message then
local prompt = insert_sticky(message.content, config)
if prompt then
M.chat:add_message({
role = 'user',
content = '\n' .. prompt,
}, true)
M.chat:finish()
end
end

M.chat:follow()
M.chat:focus()
end
Expand Down Expand Up @@ -869,6 +856,8 @@ function M.ask(prompt, config)
M.open(config)
end

M.chat:start()

local sticky = {}
local in_code_block = false
for _, line in ipairs(vim.split(prompt, '\n')) do
Expand Down
35 changes: 20 additions & 15 deletions lua/CopilotChat/ui/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,21 @@ function Chat:follow()
vim.api.nvim_win_set_cursor(self.winnr, { last_line + 1, last_column })
end

--- Prepare the chat window for writing.
function Chat:start()
self:validate()

if self:focused() then
utils.return_to_normal_mode()
end

if self.spinner then
self.spinner:start()
end

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

--- Finish writing to the chat window.
function Chat:finish()
if not self.spinner then
Expand Down Expand Up @@ -414,9 +429,7 @@ function Chat:add_message(message, replace)
self:append(message.content)
elseif replace and current_message then
-- Replace the content of the current message
self:append('')
self:render()

current_message.content = message.content
local section = current_message.section

Expand All @@ -430,9 +443,8 @@ function Chat:add_message(message, replace)
vim.split(message.content, '\n')
)
vim.bo[self.bufnr].modifiable = false
self:append('')
end

self:append('')
else
-- Append to the current message
current_message.content = current_message.content .. message.content
Expand Down Expand Up @@ -476,15 +488,6 @@ end
---@param str string
function Chat:append(str)
self:validate()
vim.bo[self.bufnr].modifiable = true

if self:focused() then
utils.return_to_normal_mode()
end

if self.spinner then
self.spinner:start()
end

-- Decide if we should follow cursor after appending text.
local should_follow_cursor = self.config.auto_follow_cursor
Expand All @@ -496,13 +499,14 @@ function Chat:append(str)
end

local last_line, last_column, _ = self:last()

vim.bo[self.bufnr].modifiable = true
vim.api.nvim_buf_set_text(self.bufnr, last_line, last_column, last_line, last_column, vim.split(str, '\n'))
vim.bo[self.bufnr].modifiable = false

if should_follow_cursor then
self:follow()
end

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

--- Clear the chat window.
Expand Down Expand Up @@ -548,6 +552,7 @@ end
--- Render the chat window.
---@protected
function Chat:render()
self:validate()
vim.api.nvim_buf_clear_namespace(self.bufnr, self.header_ns, 0, -1)
local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)

Expand Down
Loading