From 8a7b3efb5d232e817a5c7dac608e09f7c7740467 Mon Sep 17 00:00:00 2001 From: Frederick Zhang Date: Sun, 12 Feb 2023 01:04:00 +1100 Subject: [PATCH 1/2] fix(lib/highlight): Ignore invalid highlight keys If a user sets colour scheme to default after Neovim starts, nvim_get_hl_by_name('HydraRed', true) returns { [true] = 6 }. This causes an 'invalid key' error later when it's used in nvim_set_hl(). It is quite a rare case, and it should be fine as long as we can avoid the error, so that Hydra still functions. --- lua/hydra/lib/highlight.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lua/hydra/lib/highlight.lua b/lua/hydra/lib/highlight.lua index e3ab2f4..4e0d99a 100644 --- a/lua/hydra/lib/highlight.lua +++ b/lua/hydra/lib/highlight.lua @@ -3,7 +3,13 @@ local api = vim.api local function get_hl(name) ---@type boolean local rgb = api.nvim_get_option('termguicolors') - return api.nvim_get_hl_by_name(name, rgb) + local result = {} + for key, value in pairs(api.nvim_get_hl_by_name(name, rgb)) do + if type(key) == 'string' then + result[key] = value + end + end + return result end local name, settings From e8756fbb1ae48e86cfbabcaafb664e1da76a3b64 Mon Sep 17 00:00:00 2001 From: benlubas Date: Sun, 31 Dec 2023 14:42:23 -0500 Subject: [PATCH 2/2] switch to nvim_get_hl api --- lua/hydra/lib/highlight.lua | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/lua/hydra/lib/highlight.lua b/lua/hydra/lib/highlight.lua index 4e0d99a..5b36a08 100644 --- a/lua/hydra/lib/highlight.lua +++ b/lua/hydra/lib/highlight.lua @@ -1,22 +1,10 @@ local api = vim.api -local function get_hl(name) - ---@type boolean - local rgb = api.nvim_get_option('termguicolors') - local result = {} - for key, value in pairs(api.nvim_get_hl_by_name(name, rgb)) do - if type(key) == 'string' then - result[key] = value - end - end - return result -end - local name, settings for _, color in ipairs({ 'Red', 'Blue', 'Amaranth', 'Teal', 'Pink' }) do settings = vim.tbl_deep_extend('force', - get_hl('StatusLine'), - get_hl(string.format('Hydra%s', color)) + api.nvim_get_hl(0, { name = 'StatusLine', link = false }), + api.nvim_get_hl(0, { name = string.format('Hydra%s', color), link = false }) ) name = string.format('HydraStatusLine%s', color) api.nvim_set_hl(0, name, settings)