mirror of
https://github.com/neovim/neovim.git
synced 2024-12-31 17:13:26 -07:00
feat(:source): source current ft=lua buffer as Lua code (#23802)
This commit is contained in:
parent
36fd2fcaae
commit
4b60267f82
@ -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
|
• |LspRequest| autocmd callbacks now contain additional information about the LSP
|
||||||
request status update that occurred.
|
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*
|
REMOVED FEATURES *news-removed*
|
||||||
|
|
||||||
|
@ -183,7 +183,10 @@ For writing a Vim script, see chapter 41 of the user manual |usr_41.txt|.
|
|||||||
|
|
||||||
*:so* *:source* *load-vim-script*
|
*:so* *:source* *load-vim-script*
|
||||||
:[range]so[urce] [file] Runs |Ex| commands or Lua code (".lua" files) from
|
:[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.
|
Triggers the |SourcePre| autocommand.
|
||||||
*:source!*
|
*:source!*
|
||||||
:[range]so[urce]! {file}
|
:[range]so[urce]! {file}
|
||||||
|
@ -1926,8 +1926,8 @@ static void cmd_source_buffer(const exarg_T *const eap)
|
|||||||
.buf = ga.ga_data,
|
.buf = ga.ga_data,
|
||||||
.offset = 0,
|
.offset = 0,
|
||||||
};
|
};
|
||||||
if (curbuf->b_fname
|
if (strequal(curbuf->b_p_ft, "lua")
|
||||||
&& path_with_extension(curbuf->b_fname, "lua")) {
|
|| (curbuf->b_fname && path_with_extension(curbuf->b_fname, "lua"))) {
|
||||||
nlua_source_using_linegetter(get_str_line, (void *)&cookie, ":source (no file)");
|
nlua_source_using_linegetter(get_str_line, (void *)&cookie, ":source (no file)");
|
||||||
} else {
|
} else {
|
||||||
source_using_linegetter((void *)&cookie, get_str_line, ":source (no file)");
|
source_using_linegetter((void *)&cookie, get_str_line, ":source (no file)");
|
||||||
|
@ -7,6 +7,7 @@ local meths = helpers.meths
|
|||||||
local feed = helpers.feed
|
local feed = helpers.feed
|
||||||
local feed_command = helpers.feed_command
|
local feed_command = helpers.feed_command
|
||||||
local write_file = helpers.write_file
|
local write_file = helpers.write_file
|
||||||
|
local tmpname = helpers.tmpname
|
||||||
local exec = helpers.exec
|
local exec = helpers.exec
|
||||||
local exc_exec = helpers.exc_exec
|
local exc_exec = helpers.exc_exec
|
||||||
local exec_lua = helpers.exec_lua
|
local exec_lua = helpers.exec_lua
|
||||||
@ -179,56 +180,65 @@ describe(':source', function()
|
|||||||
os.remove(test_file)
|
os.remove(test_file)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('can source selected region in lua file', function()
|
describe('can source current buffer', function()
|
||||||
local test_file = 'test.lua'
|
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, [[
|
feed('ggjV')
|
||||||
vim.g.b = 5
|
feed_command(':source')
|
||||||
vim.g.b = 6
|
eq(6, eval('g:b'))
|
||||||
vim.g.b = 7
|
|
||||||
a = [=[
|
|
||||||
"\ a
|
|
||||||
\ b]=]
|
|
||||||
]])
|
|
||||||
|
|
||||||
command('edit '..test_file)
|
feed('GVkk')
|
||||||
|
feed_command(':source')
|
||||||
|
eq(' "\\ a\n \\ b', exec_lua('return _G.a'))
|
||||||
|
end)
|
||||||
|
|
||||||
feed('ggjV')
|
it('whole buffer', function()
|
||||||
feed_command(':source')
|
insert([[
|
||||||
eq(6, eval('g:b'))
|
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>')
|
||||||
|
vim.g.script_value = vim.fn.expand('<script>')
|
||||||
|
]])
|
||||||
|
feed('dd')
|
||||||
|
|
||||||
feed('GVkk')
|
feed_command(':source')
|
||||||
feed_command(':source')
|
|
||||||
eq(' "\\ a\n \\ b', exec_lua('return _G.a'))
|
|
||||||
|
|
||||||
os.remove(test_file)
|
eq(12, eval('g:c'))
|
||||||
end)
|
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'))
|
||||||
|
eq(':source (no file)', meths.get_var('script_value'))
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
it('can source current lua buffer without argument', function()
|
describe('with ft=lua', function()
|
||||||
local test_file = 'test.lua'
|
before_each(function()
|
||||||
|
command('setlocal ft=lua')
|
||||||
|
end)
|
||||||
|
test_source_lua_curbuf()
|
||||||
|
end)
|
||||||
|
|
||||||
write_file(test_file, [[
|
describe('with .lua extension', function()
|
||||||
vim.g.c = 10
|
before_each(function()
|
||||||
vim.g.c = 11
|
command('edit ' .. tmpname() .. '.lua')
|
||||||
vim.g.c = 12
|
end)
|
||||||
a = [=[
|
test_source_lua_curbuf()
|
||||||
\ 1
|
end)
|
||||||
"\ 2]=]
|
|
||||||
vim.g.sfile_value = vim.fn.expand('<sfile>')
|
|
||||||
vim.g.stack_value = vim.fn.expand('<stack>')
|
|
||||||
vim.g.script_value = vim.fn.expand('<script>')
|
|
||||||
]])
|
|
||||||
|
|
||||||
command('edit '..test_file)
|
|
||||||
feed_command(':source')
|
|
||||||
|
|
||||||
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'))
|
|
||||||
eq(':source (no file)', meths.get_var('script_value'))
|
|
||||||
|
|
||||||
os.remove(test_file)
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("doesn't throw E484 for lua parsing/runtime errors", function()
|
it("doesn't throw E484 for lua parsing/runtime errors", function()
|
||||||
|
Loading…
Reference in New Issue
Block a user