Skip to content

Commit 04c90a5

Browse files
folkedeathbeam
authored andcommitted
feat: enhanced header rendering in the chat window
1 parent 62091fa commit 04c90a5

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

lua/CopilotChat/chat.lua

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---@class CopilotChat.Chat
22
---@field bufnr number
33
---@field winnr number
4+
---@field separator string
45
---@field spinner CopilotChat.Spinner
56
---@field valid fun(self: CopilotChat.Chat)
67
---@field visible fun(self: CopilotChat.Chat)
@@ -33,11 +34,13 @@ end
3334

3435
local Chat = class(function(self, mark_ns, help, on_buf_create)
3536
self.mark_ns = mark_ns
37+
self.header_ns = vim.api.nvim_create_namespace('copilot-chat-headers')
3638
self.help = help
3739
self.on_buf_create = on_buf_create
3840
self.bufnr = nil
3941
self.winnr = nil
4042
self.spinner = nil
43+
self.separator = nil
4144

4245
self.buf_create = function()
4346
local bufnr = vim.api.nvim_create_buf(false, true)
@@ -66,6 +69,33 @@ function Chat:visible()
6669
and vim.api.nvim_win_get_buf(self.winnr) == self.bufnr
6770
end
6871

72+
function Chat:render()
73+
if not self:visible() then
74+
return
75+
end
76+
vim.api.nvim_buf_clear_namespace(self.bufnr, self.header_ns, 0, -1)
77+
local lines = vim.api.nvim_buf_get_lines(self.bufnr, 0, -1, false)
78+
for l, line in ipairs(lines) do
79+
if line:match(self.separator .. '$') then
80+
local sep = vim.fn.strwidth(line) - vim.fn.strwidth(self.separator)
81+
-- separator line
82+
vim.api.nvim_buf_set_extmark(self.bufnr, self.header_ns, l - 1, sep, {
83+
virt_text_win_col = sep,
84+
virt_text = { { string.rep(self.separator, vim.go.columns), 'CopilotChatSeparator' } },
85+
priority = 100,
86+
strict = false,
87+
})
88+
-- header hl group
89+
vim.api.nvim_buf_set_extmark(self.bufnr, self.header_ns, l - 1, 0, {
90+
end_col = sep + 1,
91+
hl_group = 'CopilotChatHeader',
92+
priority = 100,
93+
strict = false,
94+
})
95+
end
96+
end
97+
end
98+
6999
function Chat:active()
70100
return vim.api.nvim_get_current_win() == self.winnr
71101
end
@@ -101,11 +131,13 @@ function Chat:append(str)
101131
last_column,
102132
vim.split(str, '\n')
103133
)
134+
self:render()
104135
end
105136

106137
function Chat:clear()
107138
self:validate()
108139
vim.api.nvim_buf_set_lines(self.bufnr, 0, -1, false, {})
140+
self:render()
109141
end
110142

111143
function Chat:open(config)
@@ -161,6 +193,8 @@ function Chat:open(config)
161193
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
162194
end
163195

196+
self.separator = config.separator
197+
164198
vim.wo[self.winnr].wrap = true
165199
vim.wo[self.winnr].linebreak = true
166200
vim.wo[self.winnr].cursorline = true
@@ -173,6 +207,7 @@ function Chat:open(config)
173207
else
174208
vim.wo[self.winnr].foldcolumn = '0'
175209
end
210+
self:render()
176211
end
177212

178213
function Chat:close()

lua/CopilotChat/config.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ return {
8989
question_header = '## User ', -- Header to use for user questions
9090
answer_header = '## Copilot ', -- Header to use for AI answers
9191
error_header = '## Error ', -- Header to use for errors
92-
separator = '---', -- Separator to use in chat
92+
separator = '───', -- Separator to use in chat
9393

9494
show_folds = true, -- Shows folds for sections in chat
9595
show_help = true, -- Shows help message as virtual lines when waiting for user input

lua/CopilotChat/init.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,14 @@ function M.setup(config)
613613
vim.api.nvim_set_hl(hl_ns, '@diff.minus', { bg = blend_color_with_neovim_bg('DiffDelete', 20) })
614614
vim.api.nvim_set_hl(hl_ns, '@diff.delta', { bg = blend_color_with_neovim_bg('DiffChange', 20) })
615615
vim.api.nvim_set_hl(0, 'CopilotChatSelection', { link = 'Visual', default = true })
616+
vim.api.nvim_set_hl(0, 'CopilotChatHeader', {
617+
link = '@markup.heading.2.markdown',
618+
default = true,
619+
})
620+
vim.api.nvim_set_hl(0, 'CopilotChatSeparator', {
621+
link = '@punctuation.special.markdown',
622+
default = true,
623+
})
616624

617625
local overlay_help = ''
618626
if M.config.mappings.close then

0 commit comments

Comments
 (0)