fix(api): set script context when using nvim_set_hl (#28123)

This commit is contained in:
zeertzjq 2024-03-31 20:21:47 +08:00 committed by GitHub
parent e1ff2c51ca
commit b25753381c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 67 additions and 12 deletions

View File

@ -167,7 +167,7 @@ Dictionary nvim_get_hl(Integer ns_id, Dict(get_highlight) *opts, Arena *arena, E
/// @param[out] err Error details, if any
///
// TODO(bfredl): val should take update vs reset flag
void nvim_set_hl(Integer ns_id, String name, Dict(highlight) *val, Error *err)
void nvim_set_hl(uint64_t channel_id, Integer ns_id, String name, Dict(highlight) *val, Error *err)
FUNC_API_SINCE(7)
{
int hl_id = syn_check_group(name.data, name.size);
@ -184,7 +184,9 @@ void nvim_set_hl(Integer ns_id, String name, Dict(highlight) *val, Error *err)
HlAttrs attrs = dict2hlattrs(val, true, &link_id, err);
if (!ERROR_SET(err)) {
ns_hl_def((NS)ns_id, hl_id, attrs, link_id, val);
WITH_SCRIPT_CONTEXT(channel_id, {
ns_hl_def((NS)ns_id, hl_id, attrs, link_id, val);
});
}
}

View File

@ -889,11 +889,13 @@ void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id)
g->sg_link = link_id;
g->sg_script_ctx = current_sctx;
g->sg_script_ctx.sc_lnum += SOURCING_LNUM;
nlua_set_sctx(&g->sg_script_ctx);
g->sg_set |= SG_LINK;
if (is_default) {
g->sg_deflink = link_id;
g->sg_deflink_sctx = current_sctx;
g->sg_deflink_sctx.sc_lnum += SOURCING_LNUM;
nlua_set_sctx(&g->sg_deflink_sctx);
}
} else {
g->sg_link = 0;
@ -934,6 +936,7 @@ void set_hl_group(int id, HlAttrs attrs, Dict(highlight) *dict, int link_id)
g->sg_script_ctx = current_sctx;
g->sg_script_ctx.sc_lnum += SOURCING_LNUM;
nlua_set_sctx(&g->sg_script_ctx);
g->sg_attr = hl_get_syn_attr(0, id, attrs);

View File

@ -32,21 +32,30 @@ vim.api.nvim_exec2("augroup test_group\
augroup END\
", {})
vim.api.nvim_create_autocmd('FileType', {
group = 'test_group',
pattern = 'cpp',
command = 'setl cindent',
})
vim.api.nvim_exec2(':highlight TestHL1 guibg=Blue', {})
vim.api.nvim_set_hl(0, 'TestHL2', { bg = 'Green' })
vim.api.nvim_command("command Bdelete :bd")
vim.api.nvim_create_user_command("TestCommand", ":echo 'Hello'", {})
vim.api.nvim_exec ("\
vim.api.nvim_exec2 ("\
function Close_Window() abort\
wincmd -\
endfunction\
", false)
", {})
local ret = vim.api.nvim_exec ("\
local ret = vim.api.nvim_exec2 ("\
function! s:return80()\
return 80\
endfunction\
let &tw = s:return80()\
", true)
", {})
]]
)
exec(cmd .. ' ' .. script_file)
@ -109,7 +118,7 @@ n \key1 * :echo "test"<CR>
)
end)
it('"Last set" for mapping set by vim.keymap', function()
it('"Last set" for mapping set by vim.keymap.set', function()
local result = exec_capture(':verbose map <leader>key2')
eq(
string.format(
@ -123,7 +132,7 @@ n \key2 * :echo "test"<CR>
)
end)
it('"Last set" for autocmd by vim.api.nvim_exec', function()
it('"Last set" for autocmd set by nvim_exec2', function()
local result = exec_capture(':verbose autocmd test_group Filetype c')
eq(
string.format(
@ -138,6 +147,47 @@ test_group FileType
)
end)
it('"Last set" for autocmd set by nvim_create_autocmd', function()
local result = exec_capture(':verbose autocmd test_group Filetype cpp')
eq(
string.format(
[[
--- Autocommands ---
test_group FileType
cpp setl cindent
Last set from %s line 13]],
script_location
),
result
)
end)
it('"Last set" for highlight group set by nvim_exec2', function()
local result = exec_capture(':verbose highlight TestHL1')
eq(
string.format(
[[
TestHL1 xxx guibg=Blue
Last set from %s line 19]],
script_location
),
result
)
end)
it('"Last set" for highlight group set by nvim_set_hl', function()
local result = exec_capture(':verbose highlight TestHL2')
eq(
string.format(
[[
TestHL2 xxx guibg=Green
Last set from %s line 20]],
script_location
),
result
)
end)
it('"Last set" for command defined by nvim_command', function()
if cmd == 'luafile' then
pending('nvim_command does not set the script context')
@ -148,7 +198,7 @@ test_group FileType
[[
Name Args Address Complete Definition
Bdelete 0 :bd
Last set from %s line 13]],
Last set from %s line 22]],
script_location
),
result
@ -162,7 +212,7 @@ test_group FileType
[[
Name Args Address Complete Definition
TestCommand 0 :echo 'Hello'
Last set from %s line 14]],
Last set from %s line 23]],
script_location
),
result
@ -175,7 +225,7 @@ test_group FileType
string.format(
[[
function Close_Window() abort
Last set from %s line 16
Last set from %s line 25
1 wincmd -
endfunction]],
script_location
@ -190,7 +240,7 @@ test_group FileType
string.format(
[[
textwidth=80
Last set from %s line 22]],
Last set from %s line 31]],
script_location
),
result