fix(highlight): add force in nvim_set_hl

This commit is contained in:
glepnir 2023-09-18 14:50:27 +08:00
parent 71530cc972
commit f8ea49cfe1
7 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -168,6 +168,7 @@ typedef struct {
Integer blend;
Boolean fg_indexed;
Boolean bg_indexed;
Boolean force;
} Dict(highlight);
typedef struct {

View File

@ -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

View File

@ -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;
}

View File

@ -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)