diff options
author | Sindre T. Strøm <sindrets@gmail.com> | 2023-03-31 12:52:53 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-31 12:52:53 +0200 |
commit | b34097fe6d2ea5c84bcec65a834a430d9f58eb64 (patch) | |
tree | 5437aa065dab04d1772ab15573ae47be584b778e | |
parent | ed10e4ef60c63d924b9969abdf77adaad506b676 (diff) | |
download | rneovim-b34097fe6d2ea5c84bcec65a834a430d9f58eb64.tar.gz rneovim-b34097fe6d2ea5c84bcec65a834a430d9f58eb64.tar.bz2 rneovim-b34097fe6d2ea5c84bcec65a834a430d9f58eb64.zip |
fix(api): return both link and attributes with nvim_get_hl (#22824)
Problem: No way to get the actual highlight attributes for a linked
group through |nvim_get_hl()| (not the attributes from the link target).
Solution: Return the actual attributes as well as the link target name.
-rw-r--r-- | runtime/doc/api.txt | 8 | ||||
-rw-r--r-- | src/nvim/api/vim.c | 7 | ||||
-rw-r--r-- | src/nvim/highlight_group.c | 24 | ||||
-rw-r--r-- | test/functional/api/highlight_spec.lua | 14 |
4 files changed, 39 insertions, 14 deletions
diff --git a/runtime/doc/api.txt b/runtime/doc/api.txt index b51da8d56f..844bf772f1 100644 --- a/runtime/doc/api.txt +++ b/runtime/doc/api.txt @@ -941,6 +941,10 @@ nvim_get_hl({ns_id}, {*opts}) *nvim_get_hl()* map as in |nvim_set_hl()|, or only a single highlight definition map if requested by name or id. + Note: + When the `link` attribute is defined in the highlight definition map, + other attributes will not be taking effect (see |:hi-link|). + nvim_get_hl_id_by_name({name}) *nvim_get_hl_id_by_name()* Gets a highlight group by name @@ -1388,6 +1392,10 @@ nvim_set_hl({ns_id}, {name}, {*val}) *nvim_set_hl()* values of the Normal group. If the Normal group has not been defined, using these values results in an error. + Note: + If `link` is used in combination with other attributes; only the + `link` will take effect (see |:hi-link|). + Parameters: ~ • {ns_id} Namespace id for this highlight |nvim_create_namespace()|. Use 0 to set a highlight group globally |:highlight|. diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index ca5dd97020..9812313b7b 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -95,6 +95,9 @@ Integer nvim_get_hl_id_by_name(String name) /// @param[out] err Error details, if any. /// @return Highlight groups as a map from group name to a highlight definition map as in |nvim_set_hl()|, /// or only a single highlight definition map if requested by name or id. +/// +/// @note When the `link` attribute is defined in the highlight definition +/// map, other attributes will not be taking effect (see |:hi-link|). Dictionary nvim_get_hl(Integer ns_id, Dict(get_highlight) *opts, Arena *arena, Error *err) FUNC_API_SINCE(11) { @@ -113,6 +116,10 @@ Dictionary nvim_get_hl(Integer ns_id, Dict(get_highlight) *opts, Arena *arena, E /// values of the Normal group. If the Normal group has not been defined, /// using these values results in an error. /// +/// +/// @note If `link` is used in combination with other attributes; only the +/// `link` will take effect (see |:hi-link|). +/// /// @param ns_id Namespace id for this highlight |nvim_create_namespace()|. /// Use 0 to set a highlight group globally |:highlight|. /// Highlights from non-global namespaces are not active by default, use diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c index 057447c9f4..56fa206247 100644 --- a/src/nvim/highlight_group.c +++ b/src/nvim/highlight_group.c @@ -797,9 +797,9 @@ void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id) } HlGroup *g = &hl_table[idx]; + g->sg_cleared = false; if (link_id > 0) { - g->sg_cleared = false; g->sg_link = link_id; g->sg_script_ctx = current_sctx; g->sg_script_ctx.sc_lnum += SOURCING_LNUM; @@ -809,11 +809,10 @@ void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id) g->sg_deflink_sctx = current_sctx; g->sg_deflink_sctx.sc_lnum += SOURCING_LNUM; } - goto update; + } else { + g->sg_link = 0; } - g->sg_cleared = false; - g->sg_link = 0; g->sg_gui = attrs.rgb_ae_attr; g->sg_rgb_fg = attrs.rgb_fg_color; @@ -865,7 +864,6 @@ void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id) } } -update: if (!updating_screen) { redraw_all_later(UPD_NOT_VALID); } @@ -1533,17 +1531,15 @@ static bool hlgroup2dict(Dictionary *hl, NS ns_id, int hl_id, Arena *arena) } HlAttrs attr = syn_attr2entry(ns_id == 0 ? sgp->sg_attr : ns_get_hl(&ns_id, hl_id, false, sgp->sg_set)); + *hl = arena_dict(arena, HLATTRS_DICT_SIZE + 1); if (link > 0) { - *hl = arena_dict(arena, 1); PUT_C(*hl, "link", STRING_OBJ(cstr_as_string(hl_table[link - 1].sg_name))); - } else { - *hl = arena_dict(arena, HLATTRS_DICT_SIZE); - Dictionary hl_cterm = arena_dict(arena, HLATTRS_DICT_SIZE); - hlattrs2dict(hl, NULL, attr, true, true); - hlattrs2dict(hl, &hl_cterm, attr, false, true); - if (kv_size(hl_cterm)) { - PUT_C(*hl, "cterm", DICTIONARY_OBJ(hl_cterm)); - } + } + Dictionary hl_cterm = arena_dict(arena, HLATTRS_DICT_SIZE); + hlattrs2dict(hl, NULL, attr, true, true); + hlattrs2dict(hl, &hl_cterm, attr, false, true); + if (kv_size(hl_cterm)) { + PUT_C(*hl, "cterm", DICTIONARY_OBJ(hl_cterm)); } return true; } diff --git a/test/functional/api/highlight_spec.lua b/test/functional/api/highlight_spec.lua index 65b13bebf7..a4bd574a56 100644 --- a/test/functional/api/highlight_spec.lua +++ b/test/functional/api/highlight_spec.lua @@ -561,4 +561,18 @@ describe('API: get highlight', function() eq({ link = 'String' }, meths.get_hl(0, { name = '@string' })) eq({ fg = 10937249 }, meths.get_hl(0, { name = '@string.cpp', link = false })) end) + + it('can get all attributes for a linked group', function() + command('hi Bar guifg=red') + command('hi Foo guifg=#00ff00 gui=bold,underline') + command('hi! link Foo Bar') + eq({ link = 'Bar', fg = tonumber('00ff00', 16), bold = true, underline = true }, meths.get_hl(0, { name = 'Foo', link = true })) + end) + + it('can set link as well as other attributes', function() + command('hi Bar guifg=red') + local hl = { link = 'Bar', fg = tonumber('00ff00', 16), bold = true, cterm = { bold = true } } + meths.set_hl(0, 'Foo', hl) + eq(hl, meths.get_hl(0, { name = 'Foo', link = true })) + end) end) |