fix(lua): don't override script ID from :source (#32626)

Problem:  When setting an option, mapping etc. from Lua without -V1, the
          script ID is set to SID_LUA even if there already is a script
          ID assigned by :source.
Solution: Don't set script ID to SID_LUA if it is already a Lua script.
          Also add _editor.lua to ignorelist to make script context more
          useful when using vim.cmd().
This commit is contained in:
zeertzjq
2025-02-26 07:40:21 +08:00
committed by GitHub
parent af0a2157ad
commit e2aca58bcc
8 changed files with 188 additions and 96 deletions

View File

@@ -6,6 +6,7 @@ local insert = n.insert
local eq = t.eq
local clear = n.clear
local api = n.api
local fn = n.fn
local feed = n.feed
local feed_command = n.feed_command
local write_file = t.write_file
@@ -169,8 +170,9 @@ describe(':source', function()
eq('4', exec_capture('echo luaeval("y")'))
end)
it('can source lua files', function()
local test_file = 'test.lua'
--- @param verbose boolean
local function test_source_lua_file(verbose)
local test_file = 'Xtest.lua'
write_file(
test_file,
[[
@@ -178,17 +180,32 @@ describe(':source', function()
vim.g.sfile_value = vim.fn.expand('<sfile>')
vim.g.stack_value = vim.fn.expand('<stack>')
vim.g.script_value = vim.fn.expand('<script>')
vim.g.script_id = tonumber(vim.fn.expand('<SID>'):match('<SNR>(%d+)_'))
vim.o.mouse = 'nv'
]]
)
command('set shellslash')
command('source ' .. test_file)
command(('%ssource %s'):format(verbose and 'verbose ' or '', test_file))
eq(1, eval('g:sourced_lua'))
matches([[/test%.lua$]], api.nvim_get_var('sfile_value'))
matches([[/test%.lua$]], api.nvim_get_var('stack_value'))
matches([[/test%.lua$]], api.nvim_get_var('script_value'))
matches([[/Xtest%.lua$]], api.nvim_get_var('sfile_value'))
matches([[/Xtest%.lua$]], api.nvim_get_var('stack_value'))
matches([[/Xtest%.lua$]], api.nvim_get_var('script_value'))
local expected_sid = fn.getscriptinfo({ name = test_file })[1].sid
local sid = api.nvim_get_var('script_id')
eq(expected_sid, sid)
eq(sid, api.nvim_get_option_info2('mouse', {}).last_set_sid)
os.remove(test_file)
end
it('can source lua files', function()
test_source_lua_file(false)
end)
it('with :verbose modifier can source lua files', function()
test_source_lua_file(true)
end)
describe('can source current buffer', function()
@@ -253,7 +270,7 @@ describe(':source', function()
end)
it("doesn't throw E484 for lua parsing/runtime errors", function()
local test_file = 'test.lua'
local test_file = 'Xtest.lua'
-- Does throw E484 for unreadable files
local ok, result = pcall(exec_capture, ':source ' .. test_file .. 'noexisting')

View File

@@ -25,6 +25,9 @@ local function last_set_lua_tests(cmd)
vim.api.nvim_set_option_value('hlsearch', false, {})
vim.bo.expandtab = true
vim.opt.number = true
vim.api.nvim_exec2('set numberwidth=2', {})
vim.cmd('set colorcolumn=+1')
vim.api.nvim_set_keymap('n', '<leader>key1', ':echo "test"<cr>', {noremap = true})
vim.keymap.set('n', '<leader>key2', ':echo "test"<cr>')
@@ -63,48 +66,34 @@ let &tw = s:return80()\
exec(cmd .. ' ' .. script_file)
end)
local option_checks = {
{ 'nvim_set_option_value', 'hlsearch', 'nohlsearch' },
{ 'vim.bo', 'expandtab', ' expandtab' },
{ 'vim.opt', 'number', ' number' },
{ 'nvim_exec2', 'numberwidth', ' numberwidth=2' },
{ 'vim.cmd', 'colorcolumn', ' colorcolumn=+1' },
}
teardown(function()
os.remove(script_file)
end)
it('"Last set" for option set by nvim_set_option_value', function()
local result = exec_capture(':verbose set hlsearch?')
eq(
string.format(
[[
nohlsearch
Last set from %s line 1]],
script_location
),
result
)
end)
it('"Last set" for option set by vim.o', function()
local result = exec_capture(':verbose set expandtab?')
eq(
string.format(
[[
expandtab
Last set from %s line 2]],
script_location
),
result
)
end)
it('"Last set" for option set by vim.opt', function()
local result = exec_capture(':verbose set number?')
eq(
string.format(
[[
number
Last set from %s line 3]],
script_location
),
result
)
end)
for linenr, check in ipairs(option_checks) do
it(('"Last set" for option set by %s'):format(check[1]), function()
local result = exec_capture((':verbose set %s?'):format(check[2]))
eq(
string.format(
[[
%s
Last set from %s line %d]],
check[3],
script_location,
linenr
),
result
)
end)
end
it('"Last set" for mapping set by nvim_set_keymap', function()
local result = exec_capture(':verbose map <leader>key1')
@@ -113,7 +102,7 @@ nohlsearch
[[
n \key1 * :echo "test"<CR>
Last set from %s line 4]],
Last set from %s line 7]],
script_location
),
result
@@ -127,7 +116,7 @@ n \key1 * :echo "test"<CR>
[[
n \key2 * :echo "test"<CR>
Last set from %s line 5]],
Last set from %s line 8]],
script_location
),
result
@@ -142,7 +131,7 @@ n \key2 * :echo "test"<CR>
--- Autocommands ---
test_group FileType
c setl cindent
Last set from %s line 7]],
Last set from %s line 10]],
script_location
),
result
@@ -157,7 +146,7 @@ test_group FileType
--- Autocommands ---
test_group FileType
cpp setl cindent
Last set from %s line 13]],
Last set from %s line 16]],
script_location
),
result
@@ -170,7 +159,7 @@ test_group FileType
string.format(
[[
TestHL1 xxx guibg=Blue
Last set from %s line 19]],
Last set from %s line 22]],
script_location
),
result
@@ -183,7 +172,7 @@ TestHL1 xxx guibg=Blue
string.format(
[[
TestHL2 xxx guibg=Green
Last set from %s line 20]],
Last set from %s line 23]],
script_location
),
result
@@ -200,7 +189,7 @@ TestHL2 xxx guibg=Green
[[
Name Args Address Complete Definition
Bdelete 0 :bd
Last set from %s line 22]],
Last set from %s line 25]],
script_location
),
result
@@ -214,7 +203,7 @@ TestHL2 xxx guibg=Green
[[
Name Args Address Complete Definition
TestCommand 0 :echo 'Hello'
Last set from %s line 23]],
Last set from %s line 26]],
script_location
),
result
@@ -227,7 +216,7 @@ TestHL2 xxx guibg=Green
string.format(
[[
function Close_Window() abort
Last set from %s line 25
Last set from %s line 28
1 wincmd -
endfunction]],
script_location
@@ -242,7 +231,7 @@ TestHL2 xxx guibg=Green
string.format(
[[
textwidth=80
Last set from %s line 31]],
Last set from %s line 34]],
script_location
),
result
@@ -250,41 +239,89 @@ TestHL2 xxx guibg=Green
end)
end
describe('lua :verbose when using :source', function()
describe('lua :verbose with -V1 when using :source', function()
last_set_lua_tests('source')
end)
describe('lua :verbose when using :luafile', function()
describe('lua :verbose with -V1 when using :luafile', function()
last_set_lua_tests('luafile')
end)
describe('lua verbose:', function()
local script_file
describe('lua :verbose without -V1', function()
local script_location, script_file
-- All test cases below use the same Nvim instance.
setup(function()
clear()
script_file = 'test_luafile.lua'
script_file = 'test_verbose_0.lua'
local current_dir = fn.getcwd()
current_dir = fn.fnamemodify(current_dir, ':~')
script_location = table.concat({ current_dir, n.get_pathsep(), script_file })
write_file(
script_file,
[[
vim.api.nvim_set_option_value('hlsearch', false, {})
]]
vim.api.nvim_set_option_value('hlsearch', false, {})
vim.bo.expandtab = true
vim.opt.number = true
vim.api.nvim_exec2('set numberwidth=2', {})
vim.cmd('set colorcolumn=+1')
]]
)
exec(':source ' .. script_file)
end)
local option_checks = {
{ 'nvim_set_option_value', 'hlsearch', 'nohlsearch' },
{ 'vim.bo', 'expandtab', ' expandtab' },
{ 'vim.opt', 'number', ' number' },
{ 'nvim_exec2', 'numberwidth', ' numberwidth=2' },
{ 'vim.cmd', 'colorcolumn', ' colorcolumn=+1' },
}
teardown(function()
os.remove(script_file)
end)
it('is disabled when verbose = 0', function()
local result = exec_capture(':verbose set hlsearch?')
eq(
[[
nohlsearch
describe('"Last set" shows file name when using :source', function()
setup(function()
exec(':source ' .. script_file)
end)
for _, check in ipairs(option_checks) do
it(('for option set by %s'):format(check[1]), function()
local result = exec_capture((':verbose set %s?'):format(check[2]))
eq(
string.format(
[[
%s
Last set from %s (run Nvim with -V1 for more details)]],
check[3],
script_location
),
result
)
end)
end
end)
describe('"Last set" suggests -V1 when using :luafile', function()
setup(function()
exec(':luafile ' .. script_file)
end)
for _, check in ipairs(option_checks) do
it(('for option set by %s'):format(check[1]), function()
local result = exec_capture((':verbose set %s?'):format(check[2]))
eq(
string.format(
[[
%s
Last set from Lua (run Nvim with -V1 for more details)]],
result
)
check[3]
),
result
)
end)
end
end)
end)

View File

@@ -5,6 +5,7 @@ local clear = n.clear
local eq = t.eq
local eval = n.eval
local exec = n.exec
local api = n.api
local fn = n.fn
local mkdir_p = n.mkdir_p
local rmdir = n.rmdir
@@ -15,9 +16,10 @@ describe('runtime:', function()
local sep = n.get_pathsep()
local init = 'dummy_init.lua'
-- All test cases below use the same Nvim instance.
setup(function()
io.open(init, 'w'):close() -- touch init file
clear { args = { '-u', init } }
clear({ args = { '-u', init } })
exec('set rtp+=' .. plug_dir)
exec([[
set shell=doesnotexist
@@ -39,6 +41,8 @@ describe('runtime:', function()
after_each(function()
rmdir(plug_dir)
exec('bwipe!')
exec('set rtp& pp&')
exec('set rtp+=' .. plug_dir)
end)
describe('colors', function()
@@ -404,6 +408,22 @@ describe('runtime:', function()
end)
end)
it('lua file loaded by :runtime has proper script ID #32598', function()
local test_file = 'Xtest_runtime_cmd.lua'
write_file(
table.concat({ plug_dir, test_file }, sep),
[[
vim.g.script_id = tonumber(vim.fn.expand('<SID>'):match('<SNR>(%d+)_'))
vim.o.mouse = 'nv'
]]
)
exec('runtime ' .. test_file)
local expected_sid = fn.getscriptinfo({ name = test_file })[1].sid
local sid = api.nvim_get_var('script_id')
eq(expected_sid, sid)
eq(sid, api.nvim_get_option_info2('mouse', {}).last_set_sid)
end)
it('cpp ftplugin loads c ftplugin #29053', function()
eq('', eval('&commentstring'))
eq('', eval('&omnifunc'))