Skip to content

Commit 0d64e26

Browse files
authored
feat(ui): improve keyword highlights accuracy and performance (#1260)
Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
1 parent 4fdf89a commit 0d64e26

3 files changed

Lines changed: 27 additions & 20 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,13 @@ Types of copilot highlights:
255255
- `CopilotChatHeader` - Header highlight in chat buffer
256256
- `CopilotChatSeparator` - Separator highlight in chat buffer
257257
- `CopilotChatStatus` - Status and spinner in chat buffer
258-
- `CopilotChatHelp` - Help messages in chat buffer (help, references)
258+
- `CopilotChatHelp` - Help text in chat buffer
259+
- `CopilotChatResource` - Resource highlight in chat buffer (e.g. `#file`, `#gitdiff`)
260+
- `CopilotChatTool` - Tool call highlight in chat buffer (e.g. `@copilot`)
261+
- `CopilotChatPrompt` - Prompt highlight in chat buffer (e.g. `/Explain`, `/Review`)
262+
- `CopilotChatModel` - Model highlight in chat buffer (e.g. `$gpt-4.1`)
263+
- `CopilotChatUri` - URI highlight in chat buffer (e.g. `##https://...`)
259264
- `CopilotChatSelection` - Selection highlight in source buffer
260-
- `CopilotChatKeyword` - Keyword highlight in chat buffer (e.g. prompts, tools)
261265
- `CopilotChatAnnotation` - Annotation highlight in chat buffer (file headers, tool call headers, tool call body)
262266

263267
## Prompts

lua/CopilotChat/ui/chat.lua

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,9 @@ function Chat:open(config)
340340
vim.wo[self.winnr].foldcolumn = '0'
341341
end
342342

343+
local ns = vim.api.nvim_create_namespace('copilot-chat-local-hl')
344+
vim.api.nvim_set_hl(ns, '@markup.quote.markdown', {})
345+
vim.api.nvim_win_set_hl_ns(self.winnr, ns)
343346
vim.api.nvim_win_set_buf(self.winnr, self.bufnr)
344347
self:render()
345348
end
@@ -678,22 +681,6 @@ function Chat:render()
678681
end
679682
end
680683

681-
-- Keywords
682-
if current_message and current_message.role == 'user' then
683-
-- FIXME: This is not optimal, but i cant figure out how to do it better as treesitter keeps overriding it
684-
local patterns = {
685-
'()#?#[^ ]+()',
686-
'()@[^ ]+()',
687-
'()%$[^ ]+()',
688-
'()/[^ ]+()',
689-
}
690-
for _, pattern in ipairs(patterns) do
691-
for s, e in line:gmatch(pattern) do
692-
vim.api.nvim_buf_add_highlight(self.bufnr, highlight_ns, 'CopilotChatKeyword', l - 1, s - 1, e - 1)
693-
end
694-
end
695-
end
696-
697684
-- If last line, finish last message
698685
if l == #lines and current_message then
699686
current_message.section.end_line = l

plugin/CopilotChat.lua

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ local function setup_highlights()
1616
vim.api.nvim_set_hl(0, 'CopilotChatSeparator', { link = '@punctuation.special.markdown', default = true })
1717
vim.api.nvim_set_hl(0, 'CopilotChatStatus', { link = 'DiagnosticHint', default = true })
1818
vim.api.nvim_set_hl(0, 'CopilotChatHelp', { link = 'DiagnosticInfo', default = true })
19-
vim.api.nvim_set_hl(0, 'CopilotChatKeyword', { link = 'Keyword', default = true })
19+
vim.api.nvim_set_hl(0, 'CopilotChatResource', { link = 'Constant', default = true })
20+
vim.api.nvim_set_hl(0, 'CopilotChatTool', { link = 'Function', default = true })
21+
vim.api.nvim_set_hl(0, 'CopilotChatPrompt', { link = 'Statement', default = true })
22+
vim.api.nvim_set_hl(0, 'CopilotChatModel', { link = 'Type', default = true })
23+
vim.api.nvim_set_hl(0, 'CopilotChatUri', { link = 'Underlined', default = true })
2024
vim.api.nvim_set_hl(0, 'CopilotChatSelection', { link = 'Visual', default = true })
21-
vim.api.nvim_set_hl(0, 'CopilotChatAnnotation', { link = 'ColorColumn', default = true })
2225

26+
vim.api.nvim_set_hl(0, 'CopilotChatAnnotation', { link = 'ColorColumn', default = true })
2327
local fg = vim.api.nvim_get_hl(0, { name = 'CopilotChatStatus', link = false }).fg
2428
local bg = vim.api.nvim_get_hl(0, { name = 'CopilotChatAnnotation', link = false }).bg
2529
vim.api.nvim_set_hl(0, 'CopilotChatAnnotationHeader', { fg = fg, bg = bg })
@@ -75,6 +79,18 @@ vim.api.nvim_create_user_command('CopilotChatReset', function()
7579
chat.reset()
7680
end, { force = true })
7781

82+
vim.api.nvim_create_autocmd('FileType', {
83+
pattern = 'copilot-chat',
84+
group = group,
85+
callback = vim.schedule_wrap(function()
86+
vim.cmd.syntax('match CopilotChatResource "#\\S\\+"')
87+
vim.cmd.syntax('match CopilotChatTool "@\\S\\+"')
88+
vim.cmd.syntax('match CopilotChatPrompt "/\\S\\+"')
89+
vim.cmd.syntax('match CopilotChatModel "\\$\\S\\+"')
90+
vim.cmd.syntax('match CopilotChatUri "##\\S\\+"')
91+
end),
92+
})
93+
7894
local function complete_load()
7995
local chat = require('CopilotChat')
8096
local options = vim.tbl_map(function(file)

0 commit comments

Comments
 (0)