diff options
author | Lewis Russell <lewis6991@gmail.com> | 2022-02-15 23:40:06 +0000 |
---|---|---|
committer | Lewis Russell <lewis6991@gmail.com> | 2022-02-16 20:22:09 +0000 |
commit | 876aaf2003d1a6eb8f0701cf11e1834751b28980 (patch) | |
tree | 7a5fb69585707ef94bf0c9eedcafbcad65cc980b | |
parent | 238b944e58d12a28245be996e69bf36a2a452a90 (diff) | |
download | rneovim-876aaf2003d1a6eb8f0701cf11e1834751b28980.tar.gz rneovim-876aaf2003d1a6eb8f0701cf11e1834751b28980.tar.bz2 rneovim-876aaf2003d1a6eb8f0701cf11e1834751b28980.zip |
fix(highlight): allow globals to be cleared
- and reduce heap allocations
Fixes #17420
-rw-r--r-- | src/nvim/highlight.c | 10 | ||||
-rw-r--r-- | src/nvim/syntax.c | 26 | ||||
-rw-r--r-- | test/functional/api/highlight_spec.lua | 22 |
3 files changed, 44 insertions, 14 deletions
diff --git a/src/nvim/highlight.c b/src/nvim/highlight.c index fcd91cdf16..4b80267087 100644 --- a/src/nvim/highlight.c +++ b/src/nvim/highlight.c @@ -904,15 +904,13 @@ HlAttrs dict2hlattrs(Dict(highlight) *dict, bool use_rgb, int *link_id, Error *e hlattrs.rgb_bg_color = bg; hlattrs.rgb_fg_color = fg; hlattrs.rgb_sp_color = sp; - hlattrs.cterm_bg_color = - ctermbg == -1 ? cterm_normal_bg_color : ctermbg + 1; - hlattrs.cterm_fg_color = - ctermfg == -1 ? cterm_normal_fg_color : ctermfg + 1; + hlattrs.cterm_bg_color = ctermbg == -1 ? 0 : ctermbg + 1; + hlattrs.cterm_fg_color = ctermfg == -1 ? 0 : ctermfg + 1; hlattrs.cterm_ae_attr = cterm_mask; } else { hlattrs.cterm_ae_attr = cterm_mask; - hlattrs.cterm_bg_color = bg == -1 ? cterm_normal_bg_color : bg + 1; - hlattrs.cterm_fg_color = fg == -1 ? cterm_normal_fg_color : fg + 1; + hlattrs.cterm_bg_color = bg == -1 ? 0 : bg + 1; + hlattrs.cterm_fg_color = fg == -1 ? 0 : fg + 1; } return hlattrs; diff --git a/src/nvim/syntax.c b/src/nvim/syntax.c index 119f6e811f..962a48822f 100644 --- a/src/nvim/syntax.c +++ b/src/nvim/syntax.c @@ -6758,16 +6758,26 @@ void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id) { NULL, -1, NIL }, }; + char hex_name[8]; + char *name; + for (int j = 0; cattrs[j].dest; j++) { - if (cattrs[j].val != -1) { + if (cattrs[j].val < 0) { + XFREE_CLEAR(*cattrs[j].dest); + continue; + } + + if (cattrs[j].name.type == kObjectTypeString && cattrs[j].name.data.string.size) { + name = cattrs[j].name.data.string.data; + } else { + snprintf(hex_name, sizeof(hex_name), "#%06x", cattrs[j].val); + name = hex_name; + } + + if (!*cattrs[j].dest + || STRCMP(*cattrs[j].dest, name) != 0) { xfree(*cattrs[j].dest); - if (cattrs[j].name.type == kObjectTypeString && cattrs[j].name.data.string.size) { - *cattrs[j].dest = xstrdup(cattrs[j].name.data.string.data); - } else { - char hex_name[8]; - snprintf(hex_name, sizeof(hex_name), "#%06x", cattrs[j].val); - *cattrs[j].dest = xstrdup(hex_name); - } + *cattrs[j].dest = xstrdup(name); } } diff --git a/test/functional/api/highlight_spec.lua b/test/functional/api/highlight_spec.lua index 46a3798dc4..a412a236a7 100644 --- a/test/functional/api/highlight_spec.lua +++ b/test/functional/api/highlight_spec.lua @@ -276,4 +276,26 @@ describe("API: set highlight", function() eq('Test_hl3 xxx guifg=bLue guibg=reD', exec_capture('highlight Test_hl3')) end) + + it ("can modify a highlight in the global namespace", function() + meths.set_hl(0, 'Test_hl3', { bg = 'red', fg = 'blue'}) + eq('Test_hl3 xxx guifg=blue guibg=red', + exec_capture('highlight Test_hl3')) + + meths.set_hl(0, 'Test_hl3', { bg = 'red' }) + eq('Test_hl3 xxx guibg=red', + exec_capture('highlight Test_hl3')) + + meths.set_hl(0, 'Test_hl3', { ctermbg = 9, ctermfg = 12}) + eq('Test_hl3 xxx ctermfg=12 ctermbg=9', + exec_capture('highlight Test_hl3')) + + meths.set_hl(0, 'Test_hl3', { ctermbg = 9 }) + eq('Test_hl3 xxx ctermbg=9', + exec_capture('highlight Test_hl3')) + + meths.set_hl(0, 'Test_hl3', {}) + eq('Test_hl3 xxx cleared', + exec_capture('highlight Test_hl3')) + end) end) |