Skip to content

Commit 69a2020

Browse files
committed
feat(preset): adds ability to use dynamic highlights for winhl and hl options
Tries to close feature request #15
1 parent 81f9761 commit 69a2020

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

lua/reactive/snapshot.lua

+19-4
Original file line numberDiff line numberDiff line change
@@ -254,21 +254,26 @@ end
254254
local merge_handlers = {
255255
winhl = function(highlights, opts)
256256
Util.each(highlights, function(hl_group, hl_val)
257+
-- if a group is already applied, then we won't overwrite it
258+
-- meaning that it had a higher priority
257259
if M.snapshot.winhl[hl_group] then
258260
return
259261
end
260262

261-
local key = opts.preset_name .. opts.scope .. hl_group
263+
local key = opts.scope .. hl_group
262264
local cached_hl = M.cache.transformed_winhl[key]
263265

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

273278
M.snapshot.winhl[hl_group] = rhs
274279
else
@@ -303,7 +308,17 @@ function M:form_snapshot(preset_name, highlights, scope, constraints)
303308
return
304309
end
305310

306-
merge_handlers[value](highlights[value], opts)
311+
local highlight_values = highlights[value]
312+
313+
-- highlights can be passed as a funtion and thus be dynamic
314+
-- in that case we won't cache their values in the `winhl` option
315+
-- because they can always be different
316+
if type(highlights[value]) == 'function' then
317+
opts.nocache = true
318+
highlight_values = highlights[value]()
319+
end
320+
321+
merge_handlers[value](highlight_values, opts)
307322
end)
308323
end
309324

0 commit comments

Comments
 (0)