Skip to content

Commit

Permalink
fix(highlights): preserve colorscheme settings (#276)
Browse files Browse the repository at this point in the history
Highlights set by colorschemes are overwritten by `nvim-dap-ui` plugin
  in the `require("dapui.config").highlights.lua:setup()` method.
  Modified the method to set the defaults for highlight groups instead
  of overwriting previously set highlights (typically by colorschemes).
  Added a helper function to use the newer `nvim_get_hl` API function if
  NeoVim's version is atleast 0.9.
  • Loading branch information
dasupradyumna authored May 29, 2023
1 parent c693e80 commit c020f66
Showing 1 changed file with 29 additions and 12 deletions.
41 changes: 29 additions & 12 deletions lua/dapui/config/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,39 @@ function M.setup()
hi default link DapUIEndofBuffer EndofBuffer
]])

---gets the argument highlight group information, using the newer `nvim_get_hl` if available
---@param highlight string highlight group
---@return table hl highlight information
local function get_highlight(highlight)
local ok, hl
if vim.fn.has("nvim-0.9") == 1 then
ok, hl = pcall(vim.api.nvim_get_hl, 0, { name = highlight })
if not ok then -- highlight group is invalid
return vim.empty_dict()
end
else
ok, hl = pcall(vim.api.nvim_get_hl_by_name, highlight, true)
if not ok or hl[true] then -- highlight group is invalid or cleared
return vim.empty_dict()
end
-- change `nvim_get_hl_by_name` output into `nvim_get_hl` output format
hl.bg = hl.background
hl.fg = hl.foreground
end
return hl
end

-- Generate *NC variants of the control highlight groups
if vim.fn.has("nvim-0.8") == 1 then
local bg, bgNC
local exists, hl = pcall(vim.api.nvim_get_hl_by_name, "WinBar", true)
if exists then
bg = hl.background
end
exists, hl = pcall(vim.api.nvim_get_hl_by_name, "WinBarNC", true)
if exists then
bgNC = hl.background
end
local bg = get_highlight("WinBar").bg
local bgNC = get_highlight("WinBarNC").bg

for _, hl_group in pairs(control_hl_groups) do
local gui = vim.api.nvim_get_hl_by_name(hl_group, true)
if not gui[true] then -- https://github.com/rcarriga/nvim-dap-ui/issues/233
if gui.background ~= bg then
local gui = get_highlight(hl_group)
-- if highlight group is cleared or invalid, skip
if not vim.tbl_isempty(gui) then
gui.default = true
if gui.bg ~= bg then
gui.bg = bg
vim.api.nvim_set_hl(0, hl_group, gui)
end
Expand Down

0 comments on commit c020f66

Please sign in to comment.