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
16 changes: 15 additions & 1 deletion lua/opencode/ui/highlight.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local M = {}

---Define the plugin highlight groups for the current background.
function M.setup()
local function apply_highlights()
local is_light = vim.o.background == 'light'

if is_light then
Expand Down Expand Up @@ -153,4 +153,18 @@ function M.setup()
vim.api.nvim_set_hl(0, 'OpencodeCompactionFailed', { link = 'DiagnosticError', default = true })
end

function M.setup()
apply_highlights()

local group = vim.api.nvim_create_augroup('OpencodePluginHighlights', {
clear = true,
})

vim.api.nvim_create_autocmd('ColorScheme', {
group = group,
callback = apply_highlights,
desc = 'Restore opencode.nvim highlights after colorscheme changes',
})
end

return M
44 changes: 44 additions & 0 deletions tests/unit/highlight_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local highlight = require('opencode.ui.highlight')

describe('opencode highlight groups', function()
local name = 'OpencodeDialogOptionHover'

after_each(function()
pcall(vim.api.nvim_del_augroup_by_name, 'OpencodePluginHighlights')
vim.cmd.colorscheme('default')
vim.api.nvim_set_hl(0, name, {})
end)

it('restores highlights after a colorscheme change', function()
highlight.setup()

assert.is_not_nil(next(vim.api.nvim_get_hl(0, { name = name })))

vim.cmd.colorscheme('habamax')

assert.is_not_nil(next(vim.api.nvim_get_hl(0, { name = name })))
end)

it('preserves custom highlights', function()
highlight.setup()
vim.api.nvim_set_hl(0, name, { fg = '#123456' })

vim.api.nvim_exec_autocmds('ColorScheme', {
pattern = 'custom-theme',
})

assert.are.equal(0x123456, vim.api.nvim_get_hl(0, { name = name }).fg)
end)

it('registers only one ColorScheme handler', function()
highlight.setup()
highlight.setup()

local autocmds = vim.api.nvim_get_autocmds({
event = 'ColorScheme',
group = 'OpencodePluginHighlights',
})

assert.are.equal(1, #autocmds)
end)
end)
Loading