From 4b60267f82ef1947f8a583c02eaf502cf1db69ca Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 2 Jun 2023 21:00:55 +0800 Subject: [PATCH] feat(:source): source current ft=lua buffer as Lua code (#23802) --- runtime/doc/news.txt | 3 + runtime/doc/repeat.txt | 5 +- src/nvim/runtime.c | 4 +- test/functional/ex_cmds/source_spec.lua | 96 ++++++++++++++----------- 4 files changed, 62 insertions(+), 46 deletions(-) diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 3fd2d4d40e..31f3ff2cd1 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -92,6 +92,9 @@ The following changes to existing APIs or features add new behavior. • |LspRequest| autocmd callbacks now contain additional information about the LSP request status update that occurred. +• `:source` without arguments treats a buffer with its 'filetype' set to "lua" + as Lua code regardless of its extension. + ============================================================================== REMOVED FEATURES *news-removed* diff --git a/runtime/doc/repeat.txt b/runtime/doc/repeat.txt index b6cb126c3b..071b062957 100644 --- a/runtime/doc/repeat.txt +++ b/runtime/doc/repeat.txt @@ -183,7 +183,10 @@ For writing a Vim script, see chapter 41 of the user manual |usr_41.txt|. *:so* *:source* *load-vim-script* :[range]so[urce] [file] Runs |Ex| commands or Lua code (".lua" files) from - [file], or current buffer if no [file]. + [file]. + If no [file], the current buffer is used, and it is + treated as Lua code if its 'filetype' is "lua" or its + file name ends with ".lua". Triggers the |SourcePre| autocommand. *:source!* :[range]so[urce]! {file} diff --git a/src/nvim/runtime.c b/src/nvim/runtime.c index 4b27067fb8..964e2d0933 100644 --- a/src/nvim/runtime.c +++ b/src/nvim/runtime.c @@ -1926,8 +1926,8 @@ static void cmd_source_buffer(const exarg_T *const eap) .buf = ga.ga_data, .offset = 0, }; - if (curbuf->b_fname - && path_with_extension(curbuf->b_fname, "lua")) { + if (strequal(curbuf->b_p_ft, "lua") + || (curbuf->b_fname && path_with_extension(curbuf->b_fname, "lua"))) { nlua_source_using_linegetter(get_str_line, (void *)&cookie, ":source (no file)"); } else { source_using_linegetter((void *)&cookie, get_str_line, ":source (no file)"); diff --git a/test/functional/ex_cmds/source_spec.lua b/test/functional/ex_cmds/source_spec.lua index 02f5bff021..24987354a4 100644 --- a/test/functional/ex_cmds/source_spec.lua +++ b/test/functional/ex_cmds/source_spec.lua @@ -7,6 +7,7 @@ local meths = helpers.meths local feed = helpers.feed local feed_command = helpers.feed_command local write_file = helpers.write_file +local tmpname = helpers.tmpname local exec = helpers.exec local exc_exec = helpers.exc_exec local exec_lua = helpers.exec_lua @@ -179,56 +180,65 @@ describe(':source', function() os.remove(test_file) end) - it('can source selected region in lua file', function() - local test_file = 'test.lua' + describe('can source current buffer', function() + local function test_source_lua_curbuf() + it('selected region', function() + insert([[ + vim.g.b = 5 + vim.g.b = 6 + vim.g.b = 7 + a = [=[ + "\ a + \ b]=] + ]]) + feed('dd') - write_file (test_file, [[ - vim.g.b = 5 - vim.g.b = 6 - vim.g.b = 7 - a = [=[ - "\ a - \ b]=] - ]]) + feed('ggjV') + feed_command(':source') + eq(6, eval('g:b')) - command('edit '..test_file) + feed('GVkk') + feed_command(':source') + eq(' "\\ a\n \\ b', exec_lua('return _G.a')) + end) - feed('ggjV') - feed_command(':source') - eq(6, eval('g:b')) + it('whole buffer', function() + insert([[ + vim.g.c = 10 + vim.g.c = 11 + vim.g.c = 12 + a = [=[ + \ 1 + "\ 2]=] + vim.g.sfile_value = vim.fn.expand('') + vim.g.stack_value = vim.fn.expand('') + vim.g.script_value = vim.fn.expand('