fix: resolve functions before indexing

Highlight groups defined as functions now exhibit the following
behavior:

- Results of function calls are cached.
- Unresolved highlight groups are resolved before indexing.
This commit is contained in:
Iron-E 2020-11-25 01:25:57 -05:00
parent b2ccdbccb7
commit 92791fcfe4
No known key found for this signature in database
GPG Key ID: 19B71B7B7B021D22
1 changed files with 15 additions and 6 deletions

View File

@ -159,6 +159,19 @@ end
return setmetatable(highlite, { return setmetatable(highlite, {
['__call'] = function(self, normal, highlights, terminal_ansi_colors) ['__call'] = function(self, normal, highlights, terminal_ansi_colors)
-- function to resolve function highlight groups being defined by function calls.
local function resolve(tbl, key)
local value = tbl[key]
if type(value) == 'function' then
-- lazily cache the result; next time, if it isn't a function this step will be skipped
tbl[key] = value(setmetatable({}, {
['__index'] = function(_, inner_key) return resolve(tbl, inner_key) end
}))
end
return tbl[key]
end
-- save the colors_name before syntax reset -- save the colors_name before syntax reset
local color_name = vim.g.colors_name local color_name = vim.g.colors_name
@ -179,12 +192,8 @@ return setmetatable(highlite, {
self.highlight('Normal', normal) self.highlight('Normal', normal)
-- Highlight everything else. -- Highlight everything else.
for highlight_group, attributes in pairs(highlights) do for highlight_group, _ in pairs(highlights) do
if type(attributes) == 'function' then self.highlight(highlight_group, resolve(highlights, highlight_group))
attributes = attributes(highlights)
end
self.highlight(highlight_group, attributes)
end end
-- Set the terminal highlight colors. -- Set the terminal highlight colors.