Skip to content

Commit

Permalink
feat(preset): adds ability to use dynamic highlights for winhl and …
Browse files Browse the repository at this point in the history
…`hl` options

Tries to close feature request #15
  • Loading branch information
rasulomaroff committed Apr 15, 2024
1 parent 81f9761 commit 69a2020
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions lua/reactive/snapshot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -254,21 +254,26 @@ end
local merge_handlers = {
winhl = function(highlights, opts)
Util.each(highlights, function(hl_group, hl_val)
-- if a group is already applied, then we won't overwrite it
-- meaning that it had a higher priority
if M.snapshot.winhl[hl_group] then
return
end

local key = opts.preset_name .. opts.scope .. hl_group
local key = opts.scope .. hl_group
local cached_hl = M.cache.transformed_winhl[key]

if not cached_hl then
local rhs = Util.transform_winhl(hl_group, hl_val, opts.scope)
-- collecting all transformed highlights so that we can clear them
-- if the "ReactiveDisable" autocmd is fired
M.cache.applied_hl[rhs] = true
-- since we don't want to transform a highlight group over and over again,
-- since we don't want to transform a highlight group and set a highlight value over and over again,
-- it's better to cache it once and then extract from the cache
M.cache.transformed_winhl[key] = rhs
-- but we won't cache it if this highlight is dynamic
if not opts.nocache then
M.cache.transformed_winhl[key] = rhs
end

M.snapshot.winhl[hl_group] = rhs
else
Expand Down Expand Up @@ -303,7 +308,17 @@ function M:form_snapshot(preset_name, highlights, scope, constraints)
return
end

merge_handlers[value](highlights[value], opts)
local highlight_values = highlights[value]

-- highlights can be passed as a funtion and thus be dynamic
-- in that case we won't cache their values in the `winhl` option
-- because they can always be different
if type(highlights[value]) == 'function' then
opts.nocache = true
highlight_values = highlights[value]()
end

merge_handlers[value](highlight_values, opts)
end)
end

Expand Down

0 comments on commit 69a2020

Please sign in to comment.