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
35 changes: 35 additions & 0 deletions lua/CopilotChat/chat.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---@class CopilotChat.Chat
---@field bufnr number
---@field winnr number
---@field separator string
---@field spinner CopilotChat.Spinner
---@field valid fun(self: CopilotChat.Chat)
---@field visible fun(self: CopilotChat.Chat)
Expand Down Expand Up @@ -33,11 +34,13 @@ end

local Chat = class(function(self, mark_ns, help, on_buf_create)
self.mark_ns = mark_ns
self.header_ns = vim.api.nvim_create_namespace('copilot-chat-headers')
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a namespace returns the existing one when it already exists, so no need to create it during setup and then pass it down.

self.help = help
self.on_buf_create = on_buf_create
self.bufnr = nil
self.winnr = nil
self.spinner = nil
self.separator = nil

self.buf_create = function()
local bufnr = vim.api.nvim_create_buf(false, true)
Expand Down Expand Up @@ -66,6 +69,33 @@ function Chat:visible()
and vim.api.nvim_win_get_buf(self.winnr) == self.bufnr
end

function Chat:render()
if not self:visible() then
return
end
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)
Comment thread
deathbeam marked this conversation as resolved.
for l, line in ipairs(lines) do
if line:match(self.separator .. '$') then
local sep = vim.fn.strwidth(line) - vim.fn.strwidth(self.separator)
-- separator line
vim.api.nvim_buf_set_extmark(self.bufnr, self.header_ns, l - 1, sep, {
virt_text_win_col = sep,
virt_text = { { string.rep(self.separator, vim.go.columns), 'CopilotChatSeparator' } },
priority = 100,
strict = false,
})
-- header hl group
vim.api.nvim_buf_set_extmark(self.bufnr, self.header_ns, l - 1, 0, {
end_col = sep + 1,
hl_group = 'CopilotChatHeader',
priority = 100,
strict = false,
})
end
end
end

function Chat:active()
return vim.api.nvim_get_current_win() == self.winnr
end
Expand Down Expand Up @@ -101,11 +131,13 @@ function Chat:append(str)
last_column,
vim.split(str, '\n')
)
self:render()
end

function Chat:clear()
self:validate()
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {})
self:render()
end

function Chat:open(config)
Expand Down Expand Up @@ -161,6 +193,8 @@ function Chat:open(config)
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
end

self.separator = config.separator

vim.wo[self.winnr].wrap = true
vim.wo[self.winnr].linebreak = true
vim.wo[self.winnr].cursorline = true
Expand All @@ -173,6 +207,7 @@ function Chat:open(config)
else
vim.wo[self.winnr].foldcolumn = '0'
end
self:render()
end

function Chat:close()
Expand Down
2 changes: 1 addition & 1 deletion lua/CopilotChat/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ return {
question_header = '## User ', -- Header to use for user questions
answer_header = '## Copilot ', -- Header to use for AI answers
error_header = '## Error ', -- Header to use for errors
separator = '---', -- Separator to use in chat
separator = '───', -- Separator to use in chat

show_folds = true, -- Shows folds for sections in chat
show_help = true, -- Shows help message as virtual lines when waiting for user input
Expand Down
8 changes: 8 additions & 0 deletions lua/CopilotChat/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,14 @@ function M.setup(config)
vim.api.nvim_set_hl(hl_ns, '@diff.minus', { bg = blend_color_with_neovim_bg('DiffDelete', 20) })
vim.api.nvim_set_hl(hl_ns, '@diff.delta', { bg = blend_color_with_neovim_bg('DiffChange', 20) })
vim.api.nvim_set_hl(0, 'CopilotChatSelection', { link = 'Visual', default = true })
vim.api.nvim_set_hl(0, 'CopilotChatHeader', {
link = '@markup.heading.2.markdown',
default = true,
})
vim.api.nvim_set_hl(0, 'CopilotChatSeparator', {
link = '@punctuation.special.markdown',
default = true,
})

local overlay_help = ''
if M.config.mappings.close then
Expand Down