From f8ea49cfe1f4f3ed30da160d346553f0949ec1de Mon Sep 17 00:00:00 2001 From: glepnir Date: Mon, 18 Sep 2023 14:50:27 +0800 Subject: [PATCH] fix(highlight): add force in nvim_set_hl --- runtime/doc/api.txt | 2 ++ runtime/lua/vim/_meta/api.lua | 2 ++ runtime/lua/vim/_meta/api_keysets.lua | 1 + src/nvim/api/keysets.h | 1 + src/nvim/api/vim.c | 1 + src/nvim/highlight_group.c | 3 +-- test/functional/api/highlight_spec.lua | 11 +++++++++++ 7 files changed, 19 insertions(+), 2 deletions(-) diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index ffd90ec3d7..d30bb4851c 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -1458,6 +1458,8 @@ nvim_set_hl({ns_id}, {name}, {*val}) *nvim_set_hl()* • cterm: cterm attribute map, like |highlight-args|. If not set, cterm attributes will match those from the attribute map documented above. + • force: if true force update the highlight group when it + exists. nvim_set_hl_ns({ns_id}) *nvim_set_hl_ns()* Set active namespace for highlights defined with |nvim_set_hl()|. This can diff --git a/runtime/lua/vim/_meta/api.lua b/runtime/lua/vim/_meta/api.lua index 6573c68493..3822e5ff5b 100644 --- a/runtime/lua/vim/_meta/api.lua +++ b/runtime/lua/vim/_meta/api.lua @@ -1857,6 +1857,8 @@ function vim.api.nvim_set_decoration_provider(ns_id, opts) end --- • cterm: cterm attribute map, like `highlight-args`. If not --- set, cterm attributes will match those from the attribute --- map documented above. +--- • force: if true force update the highlight group when it +--- exists. function vim.api.nvim_set_hl(ns_id, name, val) end --- Set active namespace for highlights defined with `nvim_set_hl()`. This can diff --git a/runtime/lua/vim/_meta/api_keysets.lua b/runtime/lua/vim/_meta/api_keysets.lua index 4d08563ce2..349c68f8f9 100644 --- a/runtime/lua/vim/_meta/api_keysets.lua +++ b/runtime/lua/vim/_meta/api_keysets.lua @@ -164,6 +164,7 @@ error('Cannot require a meta file') --- @field blend? integer --- @field fg_indexed? boolean --- @field bg_indexed? boolean +--- @field force? boolean --- @class vim.api.keyset.highlight_cterm --- @field bold? boolean diff --git a/src/nvim/api/keysets.h b/src/nvim/api/keysets.h index 4e5e7af619..a98cbe276f 100644 --- a/src/nvim/api/keysets.h +++ b/src/nvim/api/keysets.h @@ -168,6 +168,7 @@ typedef struct { Integer blend; Boolean fg_indexed; Boolean bg_indexed; + Boolean force; } Dict(highlight); typedef struct { diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 916409b973..9d08e9b6c7 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -151,6 +151,7 @@ Dictionary nvim_get_hl(Integer ns_id, Dict(get_highlight) *opts, Arena *arena, E /// - cterm: cterm attribute map, like |highlight-args|. If not set, /// cterm attributes will match those from the attribute map /// documented above. +/// - force: if true force update the highlight group when it exists. /// @param[out] err Error details, if any /// // TODO(bfredl): val should take update vs reset flag diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c index 84cf19ba69..eeed58a9ab 100644 --- a/src/nvim/highlight_group.c +++ b/src/nvim/highlight_group.c @@ -799,11 +799,10 @@ int lookup_color(const int idx, const bool foreground, TriState *const boldp) void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id) { int idx = id - 1; // Index is ID minus one. - bool is_default = attrs.rgb_ae_attr & HL_DEFAULT; // Return if "default" was used and the group already has settings - if (is_default && hl_has_settings(idx, true)) { + if (is_default && hl_has_settings(idx, true) && !dict->force) { return; } diff --git a/test/functional/api/highlight_spec.lua b/test/functional/api/highlight_spec.lua index 5d6aaa57e6..1a054741ef 100644 --- a/test/functional/api/highlight_spec.lua +++ b/test/functional/api/highlight_spec.lua @@ -625,4 +625,15 @@ describe('API: get highlight', function() eq('FooBarA xxx cterm=bold,italic guifg=#ffffff', exec_capture('highlight FooBarA')) end) + + it('can override exist highlight group by force #20323', function() + local white = tonumber('ffffff', 16) + local green = tonumber('00ff00', 16) + meths.set_hl(0, 'Foo', { fg=white }) + meths.set_hl(0, 'Foo', { fg=green, force = true }) + eq({ fg = green },meths.get_hl(0, {name = 'Foo'})) + meths.set_hl(0, 'Bar', {link = 'Comment', default = true}) + meths.set_hl(0, 'Bar', {link = 'Foo',default = true, force = true}) + eq({link ='Foo', default = true}, meths.get_hl(0, {name = 'Bar'})) + end) end)