Skip to content

Commit

Permalink
fix: Validate if buffer is loaded and if buffer has lines
Browse files Browse the repository at this point in the history
Signed-off-by: Tomas Slusny <[email protected]>
  • Loading branch information
deathbeam committed Mar 12, 2024
1 parent 4b562ce commit 2a2abdd
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions lua/CopilotChat/chat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ local Chat = class(function(self, on_buf_create)
end)

function Chat:valid()
return self.bufnr and vim.api.nvim_buf_is_valid(self.bufnr)
return self.bufnr
and vim.api.nvim_buf_is_valid(self.bufnr)
and vim.api.nvim_buf_is_loaded(self.bufnr)
end

function Chat:visible()
Expand All @@ -57,34 +59,35 @@ function Chat:validate()
if self:valid() then
return
end

self.bufnr = create_buf()
if not self.spinner then
self.spinner = Spinner(self.bufnr, 'copilot-chat')
else
self.spinner.bufnr = self.bufnr
end

self:close()
self.on_buf_create(self.bufnr)
end

function Chat:last()
self:validate()
local last_line = vim.api.nvim_buf_line_count(self.bufnr) - 1
local line_count = vim.api.nvim_buf_line_count(self.bufnr)
local last_line = line_count - 1
if last_line < 0 then
return 0, 0
return 0, 0, line_count
end
local last_line_content = vim.api.nvim_buf_get_lines(self.bufnr, -2, -1, false)
if not last_line_content or #last_line_content == 0 then
return last_line, 0
return last_line, 0, line_count
end
local last_column = #last_line_content[1]
return last_line, last_column
return last_line, last_column, line_count
end

function Chat:append(str)
self:validate()
local last_line, last_column = self:last()
local last_line, last_column, _ = self:last()
vim.api.nvim_buf_set_text(
self.bufnr,
last_line,
Expand Down Expand Up @@ -172,6 +175,7 @@ function Chat:close()
self.spinner:finish()
if self:visible() then
vim.api.nvim_win_close(self.winnr, true)
self.winnr = nil
end
end

Expand All @@ -186,7 +190,11 @@ function Chat:follow()
return
end

local last_line, last_column = self:last()
local last_line, last_column, line_count = self:last()
if line_count == 0 then
return
end

vim.api.nvim_win_set_cursor(self.winnr, { last_line + 1, last_column })
end

Expand Down

0 comments on commit 2a2abdd

Please sign in to comment.