fix(lua): print multiple return values with =expr (#16933)

This commit is contained in:
Shadman
2022-01-07 00:42:31 +06:00
committed by GitHub
parent d78e46679d
commit 287d3566de
4 changed files with 42 additions and 2 deletions

View File

@@ -1131,12 +1131,12 @@ void ex_lua(exarg_T *const eap)
}
// When =expr is used transform it to print(vim.inspect(expr))
if (code[0] == '=') {
len += sizeof("print(vim.inspect())") - sizeof("=");
len += sizeof("vim.pretty_print()") - sizeof("=");
// code_buf needs to be 1 char larger then len for null byte in the end.
// lua nlua_typval_exec doesn't expect null terminated string so len
// needs to end before null byte.
char *code_buf = xmallocz(len);
vim_snprintf(code_buf, len+1, "print(vim.inspect(%s))", code+1);
vim_snprintf(code_buf, len+1, "vim.pretty_print(%s)", code+1);
xfree(code);
code = code_buf;
}

View File

@@ -689,4 +689,23 @@ vim._expand_pat_get_parts = function(lua_string)
return parts, search_index
end
---Prints given arguments in human-readable format.
---Example:
---<pre>
--- -- Print highlight group Normal and store it's contents in a variable.
--- local hl_normal = vim.pretty_print(vim.api.nvim_get_hl_by_name("Normal", true))
---</pre>
---@see |vim.inspect()|
---@return given arguments.
function vim.pretty_print(...)
local objects = {}
for i = 1, select('#', ...) do
local v = select(i, ...)
table.insert(objects, vim.inspect(v))
end
print(table.concat(objects, ' '))
return ...
end
return module