aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2024-03-31 20:21:47 +0800
committerGitHub <noreply@github.com>2024-03-31 20:21:47 +0800
commitb25753381c6049132c5c8d02eb62df99f8a958fd (patch)
tree7acd3961648f9d7a9b76a4297f8384de5ba2ca51
parente1ff2c51cad755d0ddc04a23df23e317d77023ed (diff)
downloadrneovim-b25753381c6049132c5c8d02eb62df99f8a958fd.tar.gz
rneovim-b25753381c6049132c5c8d02eb62df99f8a958fd.tar.bz2
rneovim-b25753381c6049132c5c8d02eb62df99f8a958fd.zip
fix(api): set script context when using nvim_set_hl (#28123)
-rw-r--r--src/nvim/api/vim.c6
-rw-r--r--src/nvim/highlight_group.c3
-rw-r--r--test/functional/ex_cmds/verbose_spec.lua70
3 files changed, 67 insertions, 12 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c
index 2b3ebb7bfb..2fb8f3d554 100644
--- a/src/nvim/api/vim.c
+++ b/src/nvim/api/vim.c
@@ -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);
+ });
}
}
diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c
index 79960b51e6..05b6dfb896 100644
--- a/src/nvim/highlight_group.c
+++ b/src/nvim/highlight_group.c
@@ -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);
diff --git a/test/functional/ex_cmds/verbose_spec.lua b/test/functional/ex_cmds/verbose_spec.lua
index 026c70be8a..ba90793a37 100644
--- a/test/functional/ex_cmds/verbose_spec.lua
+++ b/test/functional/ex_cmds/verbose_spec.lua
@@ -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