Skip to content

Commit f844a68

Browse files
authored
fix(chat): correct header highlighting for multi-byte characters (#1385)
The previous implementation used vim.fn.strwidth() to calculate the end column for header highlighting. This function returns the display width, which can differ from the byte length when multi-byte characters like emojis are present. Since the end_col for extmarks expects a byte-based index, this caused the highlighting to be applied incorrectly. This patch corrects the issue by using the byte length (#header_value) for the end_col of the highlight extmark, while still using vim.fn.strwidth() for virt_text_win_col to ensure correct visual alignment of UI elements. Fixes #1384
1 parent a657694 commit f844a68

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

lua/CopilotChat/ui/chat.lua

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -620,21 +620,21 @@ function Chat:render()
620620
if id then
621621
-- Draw the separator as virtual text over the header line, hiding the id and anything after the header
622622
if self.config.highlight_headers then
623-
local sep_col = vim.fn.strwidth(header_value)
624-
vim.api.nvim_buf_set_extmark(self.bufnr, highlight_ns, l - 1, sep_col, {
623+
local header_width = vim.fn.strwidth(header_value)
624+
vim.api.nvim_buf_set_extmark(self.bufnr, highlight_ns, l - 1, 0, {
625+
end_col = #header_value,
626+
hl_group = 'CopilotChatHeader',
627+
priority = 100,
628+
strict = false,
629+
})
630+
vim.api.nvim_buf_set_extmark(self.bufnr, highlight_ns, l - 1, #header_value, {
625631
virt_text = {
626632
{ string.rep(self.separator, vim.go.columns), 'CopilotChatSeparator' },
627633
},
628-
virt_text_win_col = sep_col,
634+
virt_text_win_col = header_width,
629635
priority = 200,
630636
strict = false,
631637
})
632-
vim.api.nvim_buf_set_extmark(self.bufnr, highlight_ns, l - 1, 0, {
633-
end_col = sep_col,
634-
hl_group = 'CopilotChatHeader',
635-
priority = 100,
636-
strict = false,
637-
})
638638
end
639639

640640
-- Finish previous message

0 commit comments

Comments
 (0)