diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-08-16 06:04:09 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-16 06:04:09 +0800 |
commit | 63e4436d8e9d25548cdc7c5b7e334ef164c150fc (patch) | |
tree | 63219cc75b3495147873595053a62b8ee4792052 | |
parent | e954d62527a6dc081d8942ccac740f17442446be (diff) | |
parent | d1464d16d6897144b29c22f8113aad9b7210e14c (diff) | |
download | rneovim-63e4436d8e9d25548cdc7c5b7e334ef164c150fc.tar.gz rneovim-63e4436d8e9d25548cdc7c5b7e334ef164c150fc.tar.bz2 rneovim-63e4436d8e9d25548cdc7c5b7e334ef164c150fc.zip |
Merge pull request #19781 from zeertzjq/source-lua-estack
fix(source): fix expand('<sfile>') no longer works for Lua
-rw-r--r-- | src/nvim/lua/executor.c | 2 | ||||
-rw-r--r-- | src/nvim/runtime.c | 35 | ||||
-rw-r--r-- | src/nvim/testdir/test_expand_func.vim | 27 | ||||
-rw-r--r-- | test/functional/ex_cmds/source_spec.lua | 59 |
4 files changed, 87 insertions, 36 deletions
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index a86f23db8e..7cfd666f3e 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1318,7 +1318,7 @@ int nlua_source_using_linegetter(LineGetter fgetline, void *cookie, char *name) current_sctx.sc_sid = SID_STR; current_sctx.sc_seq = 0; current_sctx.sc_lnum = 0; - estack_push(ETYPE_SCRIPT, NULL, 0); + estack_push(ETYPE_SCRIPT, name, 0); garray_T ga; char_u *line = NULL; diff --git a/src/nvim/runtime.c b/src/nvim/runtime.c index 4eb38c2c9e..edcaa27e2b 100644 --- a/src/nvim/runtime.c +++ b/src/nvim/runtime.c @@ -1941,9 +1941,6 @@ int do_source(char *fname, int check_other, int is_vimrc) cookie.level = ex_nesting_level; - // Keep the sourcing name/lnum, for recursive calls. - estack_push(ETYPE_SCRIPT, fname_exp, 0); - // start measuring script load time if --startuptime was passed and // time_fd was successfully opened afterwards. proftime_T rel_time; @@ -1966,6 +1963,9 @@ int do_source(char *fname, int check_other, int is_vimrc) const sctx_T save_current_sctx = current_sctx; si = get_current_script_id(&fname_exp, ¤t_sctx); + // Keep the sourcing name/lnum, for recursive calls. + estack_push(ETYPE_SCRIPT, (char *)si->sn_name, 0); + if (l_do_profiling == PROF_YES) { bool forceit = false; @@ -1983,30 +1983,27 @@ int do_source(char *fname, int check_other, int is_vimrc) cookie.conv.vc_type = CONV_NONE; // no conversion - // Read the first line so we can check for a UTF-8 BOM. - firstline = (uint8_t *)getsourceline(0, (void *)&cookie, 0, true); - if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef - && firstline[1] == 0xbb && firstline[2] == 0xbf) { - // Found BOM; setup conversion, skip over BOM and recode the line. - convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc); - p = (char *)string_convert(&cookie.conv, (char_u *)firstline + 3, NULL); - if (p == NULL) { - p = xstrdup((char *)firstline + 3); - } - xfree(firstline); - firstline = (uint8_t *)p; - } - if (path_with_extension((const char *)fname_exp, "lua")) { const sctx_T current_sctx_backup = current_sctx; current_sctx.sc_sid = SID_LUA; current_sctx.sc_lnum = 0; - estack_push(ETYPE_SCRIPT, NULL, 0); // Source the file as lua nlua_exec_file((const char *)fname_exp); current_sctx = current_sctx_backup; - estack_pop(); } else { + // Read the first line so we can check for a UTF-8 BOM. + firstline = (uint8_t *)getsourceline(0, (void *)&cookie, 0, true); + if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef + && firstline[1] == 0xbb && firstline[2] == 0xbf) { + // Found BOM; setup conversion, skip over BOM and recode the line. + convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc); + p = (char *)string_convert(&cookie.conv, (char_u *)firstline + 3, NULL); + if (p == NULL) { + p = xstrdup((char *)firstline + 3); + } + xfree(firstline); + firstline = (uint8_t *)p; + } // Call do_cmdline, which will call getsourceline() to get the lines. do_cmdline((char *)firstline, getsourceline, (void *)&cookie, DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT); diff --git a/src/nvim/testdir/test_expand_func.vim b/src/nvim/testdir/test_expand_func.vim index fc0f7619c4..df01d84f19 100644 --- a/src/nvim/testdir/test_expand_func.vim +++ b/src/nvim/testdir/test_expand_func.vim @@ -41,7 +41,7 @@ func Test_expand_sfile_and_stack() call assert_match('test_expand_func\.vim$', s:sfile) let expected = 'script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack' call assert_match(expected .. '$', expand('<sfile>')) - call assert_match(expected .. '\[4\]' , expand('<stack>')) + call assert_match(expected .. '\[4\]$' , expand('<stack>')) " Call in script-local function call assert_match('script .*testdir/runtest.vim\[\d\+\]\.\.function RunTheTest\[\d\+\]\.\.Test_expand_sfile_and_stack\[7\]\.\.<SNR>\d\+_expand_sfile$', s:expand_sfile()) @@ -59,7 +59,32 @@ func Test_expand_sfile_and_stack() call writefile(lines, 'Xstack') source Xstack call assert_match('\<Xstack\[2\]$', g:stack_value) + unlet g:stack_value call delete('Xstack') + + if exists('+shellslash') + call mkdir('Xshellslash') + let lines =<< trim END + let g:stack1 = expand('<stack>') + set noshellslash + let g:stack2 = expand('<stack>') + set shellslash + let g:stack3 = expand('<stack>') + END + call writefile(lines, 'Xshellslash/Xstack') + " Test that changing 'shellslash' always affects the result of expand() + " when sourcing a script multiple times. + for i in range(2) + source Xshellslash/Xstack + call assert_match('\<Xshellslash/Xstack\[1\]$', g:stack1) + call assert_match('\<Xshellslash\\Xstack\[3\]$', g:stack2) + call assert_match('\<Xshellslash/Xstack\[5\]$', g:stack3) + unlet g:stack1 + unlet g:stack2 + unlet g:stack3 + endfor + call delete('Xshellslash', 'rf') + endif endfunc func Test_expand_slnum() diff --git a/test/functional/ex_cmds/source_spec.lua b/test/functional/ex_cmds/source_spec.lua index 4bc3355e9e..163ded43f9 100644 --- a/test/functional/ex_cmds/source_spec.lua +++ b/test/functional/ex_cmds/source_spec.lua @@ -48,21 +48,38 @@ describe(':source', function() pending("'shellslash' only works on Windows") return end + meths.set_option('shellslash', false) mkdir('Xshellslash') - local script = [[ - let g:result1 = expand('<stack>') + + write_file([[Xshellslash/Xstack.vim]], [[ + let g:stack1 = expand('<stack>') set shellslash - let g:result2 = expand('<stack>') + let g:stack2 = expand('<stack>') set noshellslash - let g:result3 = expand('<stack>') - ]] - write_file([[Xshellslash/Xexpand.vim]], script) + let g:stack3 = expand('<stack>') + ]]) - meths.set_option('shellslash', false) - command([[source Xshellslash/Xexpand.vim]]) - matches([[Xshellslash\Xexpand%.vim]], meths.get_var('result1')) - matches([[Xshellslash/Xexpand%.vim]], meths.get_var('result2')) - matches([[Xshellslash\Xexpand%.vim]], meths.get_var('result3')) + for _ = 1, 2 do + command([[source Xshellslash/Xstack.vim]]) + matches([[Xshellslash\Xstack%.vim]], meths.get_var('stack1')) + matches([[Xshellslash/Xstack%.vim]], meths.get_var('stack2')) + matches([[Xshellslash\Xstack%.vim]], meths.get_var('stack3')) + end + + write_file([[Xshellslash/Xstack.lua]], [[ + vim.g.stack1 = vim.fn.expand('<stack>') + vim.o.shellslash = true + vim.g.stack2 = vim.fn.expand('<stack>') + vim.o.shellslash = false + vim.g.stack3 = vim.fn.expand('<stack>') + ]]) + + for _ = 1, 2 do + command([[source Xshellslash/Xstack.lua]]) + matches([[Xshellslash\Xstack%.lua]], meths.get_var('stack1')) + matches([[Xshellslash/Xstack%.lua]], meths.get_var('stack2')) + matches([[Xshellslash\Xstack%.lua]], meths.get_var('stack3')) + end rmdir('Xshellslash') end) @@ -145,11 +162,18 @@ describe(':source', function() it('can source lua files', function() local test_file = 'test.lua' - write_file (test_file, [[vim.g.sourced_lua = 1]]) - - exec('source ' .. test_file) + write_file(test_file, [[ + vim.g.sourced_lua = 1 + vim.g.sfile_value = vim.fn.expand('<sfile>') + vim.g.stack_value = vim.fn.expand('<stack>') + ]]) + command('set shellslash') + command('source ' .. test_file) eq(1, eval('g:sourced_lua')) + matches([[/test%.lua$]], meths.get_var('sfile_value')) + matches([[/test%.lua$]], meths.get_var('stack_value')) + os.remove(test_file) end) @@ -181,13 +205,15 @@ describe(':source', function() it('can source current lua buffer without argument', function() local test_file = 'test.lua' - write_file (test_file, [[ + write_file(test_file, [[ vim.g.c = 10 vim.g.c = 11 vim.g.c = 12 a = [=[ \ 1 "\ 2]=] + vim.g.sfile_value = vim.fn.expand('<sfile>') + vim.g.stack_value = vim.fn.expand('<stack>') ]]) command('edit '..test_file) @@ -195,6 +221,9 @@ describe(':source', function() eq(12, eval('g:c')) eq(' \\ 1\n "\\ 2', exec_lua('return _G.a')) + eq(':source (no file)', meths.get_var('sfile_value')) + eq(':source (no file)', meths.get_var('stack_value')) + os.remove(test_file) end) |