Merge pull request #25229 from glepnir/20323

fix(highlight): add force in nvim_set_hl
This commit is contained in:
bfredl 2023-09-26 14:20:10 +02:00 committed by GitHub
commit fe95037cdb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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

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

View File

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

View File

@ -169,6 +169,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)