mirror of
https://github.com/neovim/neovim
synced 2025-07-17 01:31:48 +00:00
fix(lua): always use vim.inspect() for :lua= (#32715)
This commit is contained in:
@ -289,7 +289,7 @@ arguments separated by " " (space) instead of "\t" (tab).
|
|||||||
:lua {chunk}
|
:lua {chunk}
|
||||||
Executes Lua chunk {chunk}. If {chunk} starts with "=" the rest of the
|
Executes Lua chunk {chunk}. If {chunk} starts with "=" the rest of the
|
||||||
chunk is evaluated as an expression and printed. `:lua =expr` and `:=expr`
|
chunk is evaluated as an expression and printed. `:lua =expr` and `:=expr`
|
||||||
are equivalent to `:lua vim.print(expr)`.
|
are equivalent to `:lua print(vim.inspect(expr))`.
|
||||||
|
|
||||||
Examples: >vim
|
Examples: >vim
|
||||||
:lua vim.api.nvim_command('echo "Hello, Nvim!"')
|
:lua vim.api.nvim_command('echo "Hello, Nvim!"')
|
||||||
|
@ -1142,6 +1142,21 @@ do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- @param inspect_strings boolean use vim.inspect() for strings
|
||||||
|
function vim._print(inspect_strings, ...)
|
||||||
|
local msg = {}
|
||||||
|
for i = 1, select('#', ...) do
|
||||||
|
local o = select(i, ...)
|
||||||
|
if not inspect_strings and type(o) == 'string' then
|
||||||
|
table.insert(msg, o)
|
||||||
|
else
|
||||||
|
table.insert(msg, vim.inspect(o, { newline = '\n', indent = ' ' }))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
print(table.concat(msg, '\n'))
|
||||||
|
return ...
|
||||||
|
end
|
||||||
|
|
||||||
--- "Pretty prints" the given arguments and returns them unmodified.
|
--- "Pretty prints" the given arguments and returns them unmodified.
|
||||||
---
|
---
|
||||||
--- Example:
|
--- Example:
|
||||||
@ -1155,17 +1170,7 @@ end
|
|||||||
--- @param ... any
|
--- @param ... any
|
||||||
--- @return any # given arguments.
|
--- @return any # given arguments.
|
||||||
function vim.print(...)
|
function vim.print(...)
|
||||||
local msg = {}
|
return vim._print(false, ...)
|
||||||
for i = 1, select('#', ...) do
|
|
||||||
local o = select(i, ...)
|
|
||||||
if type(o) == 'string' then
|
|
||||||
table.insert(msg, o)
|
|
||||||
else
|
|
||||||
table.insert(msg, vim.inspect(o, { newline = '\n', indent = ' ' }))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
print(table.concat(msg, '\n'))
|
|
||||||
return ...
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Translates keycodes.
|
--- Translates keycodes.
|
||||||
|
@ -1668,13 +1668,13 @@ void ex_lua(exarg_T *const eap)
|
|||||||
|
|
||||||
// ":lua {code}", ":={expr}" or ":lua ={expr}"
|
// ":lua {code}", ":={expr}" or ":lua ={expr}"
|
||||||
//
|
//
|
||||||
// When "=expr" is used transform it to "vim.print(expr)".
|
// When "=expr" is used transform it to "vim._print(true, expr)".
|
||||||
if (eap->cmdidx == CMD_equal || code[0] == '=') {
|
if (eap->cmdidx == CMD_equal || code[0] == '=') {
|
||||||
size_t off = (eap->cmdidx == CMD_equal) ? 0 : 1;
|
size_t off = (eap->cmdidx == CMD_equal) ? 0 : 1;
|
||||||
len += sizeof("vim.print()") - 1 - off;
|
len += sizeof("vim._print(true, )") - 1 - off;
|
||||||
// `nlua_typval_exec` doesn't expect NUL-terminated string so `len` must end before NUL byte.
|
// `nlua_typval_exec` doesn't expect NUL-terminated string so `len` must end before NUL byte.
|
||||||
char *code_buf = xmallocz(len);
|
char *code_buf = xmallocz(len);
|
||||||
vim_snprintf(code_buf, len + 1, "vim.print(%s)", code + off);
|
vim_snprintf(code_buf, len + 1, "vim._print(true, %s)", code + off);
|
||||||
xfree(code);
|
xfree(code);
|
||||||
code = code_buf;
|
code = code_buf;
|
||||||
}
|
}
|
||||||
|
@ -173,8 +173,13 @@ describe(':lua', function()
|
|||||||
exec_lua('x = 5')
|
exec_lua('x = 5')
|
||||||
eq('5', exec_capture(':lua =x'))
|
eq('5', exec_capture(':lua =x'))
|
||||||
eq('5', exec_capture('=x'))
|
eq('5', exec_capture('=x'))
|
||||||
|
exec_lua('x = "5"')
|
||||||
|
eq('"5"', exec_capture(':lua =x'))
|
||||||
|
eq('"5"', exec_capture('=x'))
|
||||||
exec_lua("function x() return 'hello' end")
|
exec_lua("function x() return 'hello' end")
|
||||||
eq('hello', exec_capture(':lua = x()'))
|
eq('"hello"', exec_capture(':lua = x()'))
|
||||||
|
exec_lua("function x() return 'hello ' end")
|
||||||
|
eq('"hello "', exec_capture(':lua = x()'))
|
||||||
exec_lua('x = {a = 1, b = 2}')
|
exec_lua('x = {a = 1, b = 2}')
|
||||||
eq('{\n a = 1,\n b = 2\n}', exec_capture(':lua =x'))
|
eq('{\n a = 1,\n b = 2\n}', exec_capture(':lua =x'))
|
||||||
exec_lua(function()
|
exec_lua(function()
|
||||||
@ -189,14 +194,14 @@ describe(':lua', function()
|
|||||||
eq(
|
eq(
|
||||||
dedent [[
|
dedent [[
|
||||||
true
|
true
|
||||||
Return value]],
|
"Return value"]],
|
||||||
exec_capture(':lua =x(true)')
|
exec_capture(':lua =x(true)')
|
||||||
)
|
)
|
||||||
eq(
|
eq(
|
||||||
dedent [[
|
dedent [[
|
||||||
false
|
false
|
||||||
nil
|
nil
|
||||||
Error message]],
|
"Error message"]],
|
||||||
exec_capture('=x(false)')
|
exec_capture('=x(false)')
|
||||||
)
|
)
|
||||||
end)
|
end)
|
||||||
|
Reference in New Issue
Block a user