Skip to content

Commit 4a45e69

Browse files
authored
perf(chat): simplify last line/column calculation (#1402)
Replaces Chat:last() method with a standalone last() function for retrieving the last line and column of the chat buffer. Updates references to use the new function and removes redundant logic. This improves code clarity and maintainability.
1 parent f49df19 commit 4a45e69

1 file changed

Lines changed: 17 additions & 24 deletions

File tree

lua/CopilotChat/ui/chat.lua

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ local function match_block_header(header)
5252
end
5353
end
5454

55+
--- Get the last line and column of the chat window.
56+
---@param bufnr number
57+
---@return number, number
58+
---@protected
59+
local function last(bufnr)
60+
local line_count = vim.api.nvim_buf_line_count(bufnr)
61+
if line_count == 0 then
62+
return 0, 0
63+
end
64+
local last_line = line_count - 1
65+
local last_line_content = vim.api.nvim_buf_get_lines(bufnr, last_line, last_line + 1, false)
66+
local last_column = last_line_content[1] and #last_line_content[1] or 0
67+
return last_line, last_column
68+
end
69+
5570
---@class CopilotChat.ui.chat.Header
5671
---@field filename string
5772
---@field start_line number
@@ -426,11 +441,7 @@ function Chat:follow()
426441
return
427442
end
428443

429-
local last_line, last_column, line_count = self:last()
430-
if line_count == 0 then
431-
return
432-
end
433-
444+
local last_line, last_column = last(self.bufnr)
434445
vim.api.nvim_win_set_cursor(self.winnr, { last_line + 1, last_column })
435446
end
436447

@@ -557,7 +568,7 @@ function Chat:append(str)
557568
should_follow_cursor = current_pos[1] >= line_count - 1
558569
end
559570

560-
local last_line, last_column, _ = self:last()
571+
local last_line, last_column, _ = last(self.bufnr)
561572

562573
local modifiable = vim.bo[self.bufnr].modifiable
563574
vim.bo[self.bufnr].modifiable = true
@@ -884,22 +895,4 @@ function Chat:render()
884895
end
885896
end
886897

887-
--- Get the last line and column of the chat window.
888-
---@return number, number, number
889-
---@protected
890-
function Chat:last()
891-
self:validate()
892-
local line_count = vim.api.nvim_buf_line_count(self.bufnr)
893-
local last_line = line_count - 1
894-
if last_line < 0 then
895-
return 0, 0, line_count
896-
end
897-
local last_line_content = vim.api.nvim_buf_get_lines(self.bufnr, -2, -1, false)
898-
if not last_line_content or #last_line_content == 0 then
899-
return last_line, 0, line_count
900-
end
901-
local last_column = #last_line_content[1]
902-
return last_line, last_column, line_count
903-
end
904-
905898
return Chat

0 commit comments

Comments
 (0)