fix(lua): always use vim.inspect() for :lua= (#32715)

This commit is contained in:
zeertzjq
2025-03-07 19:50:00 +08:00
committed by GitHub
parent 5d08b65ac2
commit c8b64b7a43
4 changed files with 28 additions and 18 deletions

View File

@ -289,7 +289,7 @@ arguments separated by " " (space) instead of "\t" (tab).
:lua {chunk}
Executes Lua chunk {chunk}. If {chunk} starts with "=" the rest of the
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
:lua vim.api.nvim_command('echo "Hello, Nvim!"')

View File

@ -1142,6 +1142,21 @@ do
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.
---
--- Example:
@ -1155,17 +1170,7 @@ end
--- @param ... any
--- @return any # given arguments.
function vim.print(...)
local msg = {}
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 ...
return vim._print(false, ...)
end
--- Translates keycodes.

View File

@ -1668,13 +1668,13 @@ void ex_lua(exarg_T *const eap)
// ":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] == '=') {
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.
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);
code = code_buf;
}

View File

@ -173,8 +173,13 @@ describe(':lua', function()
exec_lua('x = 5')
eq('5', exec_capture(':lua =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")
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}')
eq('{\n a = 1,\n b = 2\n}', exec_capture(':lua =x'))
exec_lua(function()
@ -189,14 +194,14 @@ describe(':lua', function()
eq(
dedent [[
true
Return value]],
"Return value"]],
exec_capture(':lua =x(true)')
)
eq(
dedent [[
false
nil
Error message]],
"Error message"]],
exec_capture('=x(false)')
)
end)