fix(highlight): setting 'winhl' doesn't work with global ns (#37868)

Problem:  Setting 'winhighlight' doesn't after setting global namespace
          using nvim_win_set_hl_ns().
Solution: Check if using another namespace when setting 'winhighlight'
          instead of disabling 'winhighlight' in nvim_win_set_hl_ns().
This commit is contained in:
zeertzjq
2026-02-15 08:03:44 +08:00
committed by GitHub
parent 8ab511bba5
commit f8d59cfab9
4 changed files with 37 additions and 18 deletions

View File

@@ -454,7 +454,6 @@ void nvim_win_set_hl_ns(Window window, Integer ns_id, Error *err)
} }
win->w_ns_hl = (NS)ns_id; win->w_ns_hl = (NS)ns_id;
win->w_ns_hl_winhl = -1;
win->w_hl_needs_update = true; win->w_hl_needs_update = true;
redraw_later(win, UPD_NOT_VALID); redraw_later(win, UPD_NOT_VALID);
} }

View File

@@ -1100,7 +1100,7 @@ struct window_S {
synblock_T *w_s; ///< for :ownsyntax synblock_T *w_s; ///< for :ownsyntax
int w_ns_hl; int w_ns_hl;
int w_ns_hl_winhl; ///< when set to -1, 'winhighlight' shouldn't be used int w_ns_hl_winhl;
int w_ns_hl_active; int w_ns_hl_active;
int *w_ns_hl_attr; int *w_ns_hl_attr;

View File

@@ -1730,12 +1730,6 @@ bool parse_winhl_opt(const char *winhl, win_T *wp)
p = wp->w_p_winhl; p = wp->w_p_winhl;
} }
if (wp != NULL && wp->w_ns_hl_winhl < 0) {
// 'winhighlight' shouldn't be used for this window.
// Only check that the value is valid.
wp = NULL;
}
if (!*p) { if (!*p) {
if (wp != NULL && wp->w_ns_hl_winhl > 0 && wp->w_ns_hl == wp->w_ns_hl_winhl) { if (wp != NULL && wp->w_ns_hl_winhl > 0 && wp->w_ns_hl == wp->w_ns_hl_winhl) {
wp->w_ns_hl = 0; wp->w_ns_hl = 0;
@@ -1754,8 +1748,10 @@ bool parse_winhl_opt(const char *winhl, win_T *wp)
DecorProvider *dp = get_decor_provider(wp->w_ns_hl_winhl, true); DecorProvider *dp = get_decor_provider(wp->w_ns_hl_winhl, true);
dp->hl_valid++; dp->hl_valid++;
} }
wp->w_ns_hl = wp->w_ns_hl_winhl; ns_hl = wp->w_ns_hl_winhl;
ns_hl = wp->w_ns_hl; if (wp->w_ns_hl <= 0) {
wp->w_ns_hl = wp->w_ns_hl_winhl;
}
} }
while (*p) { while (*p) {

View File

@@ -1,7 +1,7 @@
local t = require('test.testutil') local t = require('test.testutil')
local n = require('test.functional.testnvim')() local n = require('test.functional.testnvim')()
local clear, eq = n.clear, t.eq local clear, eq, neq = n.clear, t.eq, t.neq
local command = n.command local command = n.command
local exec_capture = n.exec_capture local exec_capture = n.exec_capture
local api = n.api local api = n.api
@@ -548,6 +548,8 @@ describe('API: get highlight', function()
end) end)
describe('API: set/get highlight namespace', function() describe('API: set/get highlight namespace', function()
before_each(clear)
it('set/get highlight namespace', function() it('set/get highlight namespace', function()
eq(0, api.nvim_get_hl_ns({})) eq(0, api.nvim_get_hl_ns({}))
local ns = api.nvim_create_namespace('') local ns = api.nvim_create_namespace('')
@@ -563,16 +565,38 @@ describe('API: set/get highlight namespace', function()
end) end)
it('setting namespace takes priority over &winhighlight', function() it('setting namespace takes priority over &winhighlight', function()
local win = api.nvim_get_current_win()
eq(-1, api.nvim_get_hl_ns({ winid = win }))
command('set winhighlight=Visual:Search') command('set winhighlight=Visual:Search')
local winhl_ns = api.nvim_get_hl_ns({ winid = win })
neq(0, winhl_ns)
eq('Search', api.nvim_get_hl(winhl_ns, { name = 'Visual' }).link)
n.insert('foobar') n.insert('foobar')
local ns = api.nvim_create_namespace('') local ns = api.nvim_create_namespace('')
api.nvim_win_set_hl_ns(0, ns) neq(winhl_ns, ns)
eq(ns, api.nvim_get_hl_ns({ winid = 0 })) api.nvim_win_set_hl_ns(win, ns)
command('enew') -- switching buffer keeps namespace #30904 eq(ns, api.nvim_get_hl_ns({ winid = win }))
eq(ns, api.nvim_get_hl_ns({ winid = 0 })) command('enew') -- Switching buffer keeps namespace. #30904
eq(ns, api.nvim_get_hl_ns({ winid = win }))
command('set winhighlight=') command('set winhighlight=')
eq(ns, api.nvim_get_hl_ns({ winid = 0 })) eq(ns, api.nvim_get_hl_ns({ winid = win }))
command('set winhighlight=Visual:Search') -- Setting 'winhighlight' changes its namespace even when not using it.
eq(ns, api.nvim_get_hl_ns({ winid = 0 })) command('set winhighlight=Visual:IncSearch')
eq('IncSearch', api.nvim_get_hl(winhl_ns, { name = 'Visual' }).link)
eq(ns, api.nvim_get_hl_ns({ winid = win }))
-- Setting 'winhighlight' works with global namespace. #37865
api.nvim_win_set_hl_ns(win, 0)
eq(0, api.nvim_get_hl_ns({ winid = win }))
command('set winhighlight=Visual:IncSearch')
eq('IncSearch', api.nvim_get_hl(winhl_ns, { name = 'Visual' }).link)
eq(winhl_ns, api.nvim_get_hl_ns({ winid = win }))
command('set winhighlight=')
eq(0, api.nvim_get_hl_ns({ winid = win }))
-- 'winhighlight' keeps the same namespace.
api.nvim_win_set_hl_ns(win, winhl_ns)
eq(winhl_ns, api.nvim_get_hl_ns({ winid = win }))
command('set winhighlight=Visual:Substitute')
eq('Substitute', api.nvim_get_hl(winhl_ns, { name = 'Visual' }).link)
eq(winhl_ns, api.nvim_get_hl_ns({ winid = win }))
end) end)
end) end)