From 92791fcfe4cbbf5e88b0d55bbaa0eb9a9290c6b9 Mon Sep 17 00:00:00 2001 From: Iron-E Date: Wed, 25 Nov 2020 01:25:57 -0500 Subject: [PATCH] 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. --- lua/highlite.lua | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/lua/highlite.lua b/lua/highlite.lua index 8afe262..0313d03 100644 --- a/lua/highlite.lua +++ b/lua/highlite.lua @@ -159,6 +159,19 @@ end return setmetatable(highlite, { ['__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 local color_name = vim.g.colors_name @@ -179,12 +192,8 @@ return setmetatable(highlite, { self.highlight('Normal', normal) -- Highlight everything else. - for highlight_group, attributes in pairs(highlights) do - if type(attributes) == 'function' then - attributes = attributes(highlights) - end - - self.highlight(highlight_group, attributes) + for highlight_group, _ in pairs(highlights) do + self.highlight(highlight_group, resolve(highlights, highlight_group)) end -- Set the terminal highlight colors.