neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

commit fe95037cdb9b0294ca24dbd0ff82c8462d051307
parent 4db77017fb7ab9321817674a5f5533df4cd03f3f
Author: bfredl <bjorn.linse@gmail.com>
Date:   Tue, 26 Sep 2023 14:20:10 +0200

Merge pull request #25229 from glepnir/20323

fix(highlight): add force in nvim_set_hl
Diffstat:
Mruntime/doc/api.txt | 2++
Mruntime/lua/vim/_meta/api.lua | 2++
Mruntime/lua/vim/_meta/api_keysets.lua | 1+
Msrc/nvim/api/keysets.h | 1+
Msrc/nvim/api/vim.c | 1+
Msrc/nvim/highlight_group.c | 3+--
Mtest/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 @@ -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 @@ -1866,6 +1866,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 @@ -165,6 +165,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 @@ -169,6 +169,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 @@ -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 @@ -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 @@ -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)