aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShadman <13149513+shadmansaleh@users.noreply.github.com>2022-01-07 00:42:31 +0600
committerGitHub <noreply@github.com>2022-01-06 11:42:31 -0700
commit287d3566de11f82aa86448998fd4703b1db328bd (patch)
tree9fd586e31fc9058f499a4247c1c0efe72a64f55d
parentd78e46679d2ff31916091f9368367ccc1539c299 (diff)
downloadrneovim-287d3566de11f82aa86448998fd4703b1db328bd.tar.gz
rneovim-287d3566de11f82aa86448998fd4703b1db328bd.tar.bz2
rneovim-287d3566de11f82aa86448998fd4703b1db328bd.zip
fix(lua): print multiple return values with =expr (#16933)
-rw-r--r--runtime/doc/lua.txt12
-rw-r--r--src/nvim/lua/executor.c4
-rw-r--r--src/nvim/lua/vim.lua19
-rw-r--r--test/functional/lua/commands_spec.lua9
4 files changed, 42 insertions, 2 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt
index d25ed4e3f1..80c1f58323 100644
--- a/runtime/doc/lua.txt
+++ b/runtime/doc/lua.txt
@@ -1329,6 +1329,18 @@ paste({lines}, {phase}) *vim.paste()*
See also: ~
|paste|
+pretty_print({...}) *vim.pretty_print()*
+ Prints given arguments in human-readable format. Example: >
+ -- 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))
+<
+
+ Return: ~
+ given arguments.
+
+ See also: ~
+ |vim.inspect()|
+
region({bufnr}, {pos1}, {pos2}, {regtype}, {inclusive}) *vim.region()*
Get a table of lines with start, end columns for a region
marked by two points
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c
index c814974fe7..cfdbe7b344 100644
--- a/src/nvim/lua/executor.c
+++ b/src/nvim/lua/executor.c
@@ -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;
}
diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua
index e5a5ae799b..731e7d8d36 100644
--- a/src/nvim/lua/vim.lua
+++ b/src/nvim/lua/vim.lua
@@ -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
diff --git a/test/functional/lua/commands_spec.lua b/test/functional/lua/commands_spec.lua
index b3c3f937ac..b8346df290 100644
--- a/test/functional/lua/commands_spec.lua
+++ b/test/functional/lua/commands_spec.lua
@@ -149,6 +149,15 @@ describe(':lua command', function()
eq([["hello"]], helpers.exec_capture(':lua = x()'))
helpers.exec_lua("x = {a = 1, b = 2}")
eq("{\n a = 1,\n b = 2\n}", helpers.exec_capture(':lua =x'))
+ helpers.exec_lua([[function x(success)
+ if success then
+ return true, "Return value"
+ else
+ return false, nil, "Error message"
+ end
+ end]])
+ eq([[true "Return value"]], helpers.exec_capture(':lua =x(true)'))
+ eq([[false nil "Error message"]], helpers.exec_capture(':lua =x(false)'))
end)
end)