From d4ed51eb4492d4358eb4ab0c939802f2a5fdc636 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sat, 9 Oct 2021 22:13:11 +0100 Subject: feat(:source, nvim_exec): support script-local variables Based on #13143 (and #11507) with changes: - Omit script_type_E. Use sn_name == NULL to determine anon items. - Keep SID_STR. Used by anon :source for .lua files (no item). - Show SID in get_scriptname output (:verbose set). - Factor item creation into new_script_item. - Leave sc_seq = 0 (anon scripts don't re-use the same item when re-sourced). - Add tests for anon :source. Co-authored-by: Vikram Pal Co-authored-by: Justin M. Keyes --- test/functional/api/vim_spec.lua | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'test/functional/api') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 6b644ed519..f878a50a26 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -89,7 +89,7 @@ describe('API', function() it(':verbose set {option}?', function() nvim('exec', 'set nowrap', false) - eq('nowrap\n\tLast set from anonymous :source', + eq('nowrap\n\tLast set from anonymous :source (script id 1)', nvim('exec', 'verbose set wrap?', true)) end) @@ -132,6 +132,29 @@ describe('API', function() -- try no spaces before continuations to catch off-by-one error nvim('exec', 'let ab = #{\n\\a: 98,\n"\\ b: 2\n\\}', false) eq({a = 98}, request('nvim_eval', 'g:ab')) + + -- Script scope (s:) + eq('ahoy! script-scoped varrrrr', nvim('exec', [[ + let s:pirate = 'script-scoped varrrrr' + function! s:avast_ye_hades(s) abort + return a:s .. ' ' .. s:pirate + endfunction + echo avast_ye_hades('ahoy!') + ]], true)) + + eq('ahoy! script-scoped varrrrr', nvim('exec', [[ + let s:pirate = 'script-scoped varrrrr' + function! Avast_ye_hades(s) abort + return a:s .. ' ' .. s:pirate + endfunction + echo nvim_exec('echo Avast_ye_hades(''ahoy!'')', 1) + ]], true)) + + eq('Vim(call):E5555: API call: Vim(echo):E121: Undefined variable: s:pirate', + pcall_err(request, 'nvim_exec', [[ + let s:pirate = 'script-scoped varrrrr' + call nvim_exec('echo s:pirate', 1) + ]], false)) end) it('non-ASCII input', function() -- cgit From da9b0abc67a936021a4ecf7634395db860edcab1 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Thu, 14 Oct 2021 00:40:46 +0100 Subject: feat(:source, nvim_exec): defer script item creation until s:var access For anonymous scripts, defer the creation of script items until an attempt to access a script-local variable is made. This dramatically reduces the number of script items created when using lots of vim.cmd and nvim_exec especially. This will mean usage fails until a script-local variable access is first made. --- test/functional/api/vim_spec.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'test/functional/api') diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index f878a50a26..d8914a3ab7 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -89,6 +89,14 @@ describe('API', function() it(':verbose set {option}?', function() nvim('exec', 'set nowrap', false) + eq('nowrap\n\tLast set from anonymous :source', + nvim('exec', 'verbose set wrap?', true)) + + -- Using script var to force creation of a script item + nvim('exec', [[ + let s:a = 1 + set nowrap + ]], false) eq('nowrap\n\tLast set from anonymous :source (script id 1)', nvim('exec', 'verbose set wrap?', true)) end) @@ -155,6 +163,20 @@ describe('API', function() let s:pirate = 'script-scoped varrrrr' call nvim_exec('echo s:pirate', 1) ]], false)) + + -- Script items are created only on script var access + eq('1\n0', nvim('exec', [[ + echo expand("")->empty() + let s:a = 123 + echo expand("")->empty() + ]], true)) + + eq('1\n0', nvim('exec', [[ + echo expand("")->empty() + function s:a() abort + endfunction + echo expand("")->empty() + ]], true)) end) it('non-ASCII input', function() -- cgit