aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshadmansaleh <13149513+shadmansaleh@users.noreply.github.com>2022-01-04 11:05:15 +0600
committershadmansaleh <13149513+shadmansaleh@users.noreply.github.com>2022-01-04 16:08:07 +0600
commitd44254641ffb5c9f185db4082d2bf1f04bf1117e (patch)
treea48ea400721dcf3d9a61ef73850472a8dfe13aa6
parent207307d0faf9b821cfd3cfeae4d027ab9ee5dbdb (diff)
downloadrneovim-d44254641ffb5c9f185db4082d2bf1f04bf1117e.tar.gz
rneovim-d44254641ffb5c9f185db4082d2bf1f04bf1117e.tar.bz2
rneovim-d44254641ffb5c9f185db4082d2bf1f04bf1117e.zip
feat(lua): make =expr print result of expr
-rw-r--r--runtime/doc/lua.txt6
-rw-r--r--src/nvim/lua/executor.c14
-rw-r--r--test/functional/lua/commands_spec.lua9
3 files changed, 26 insertions, 3 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt
index eaa25f0961..63f142b789 100644
--- a/runtime/doc/lua.txt
+++ b/runtime/doc/lua.txt
@@ -249,13 +249,15 @@ arguments separated by " " (space) instead of "\t" (tab).
*:lua*
:[range]lua {chunk}
Executes Lua chunk {chunk}.
-
+ if {chunk} starts with "=" the rest of the chunk is
+ evaluated as an expression and printed. `:lua =expr`
+ is equivalent to `:lua print(vim.inspect(expr))`
Examples: >
:lua vim.api.nvim_command('echo "Hello, Nvim!"')
< To see the Lua version: >
:lua print(_VERSION)
< To see the LuaJIT version: >
- :lua print(jit.version)
+ :lua =jit.version
<
*:lua-heredoc*
:[range]lua << [endmarker]
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c
index 7d43d21d53..334fb91576 100644
--- a/src/nvim/lua/executor.c
+++ b/src/nvim/lua/executor.c
@@ -1115,11 +1115,23 @@ void ex_lua(exarg_T *const eap)
FUNC_ATTR_NONNULL_ALL
{
size_t len;
- char *const code = script_get(eap, &len);
+ char *code = script_get(eap, &len);
if (eap->skip) {
xfree(code);
return;
}
+ // When =expr is used transform it to print(vim.inspect(expr))
+ if (code[0] == '=') {
+ len += sizeof("print(vim.inspect())") - 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);
+ xfree(code);
+ code = code_buf;
+ }
+
nlua_typval_exec(code, len, ":lua", NULL, 0, false, NULL);
xfree(code);
diff --git a/test/functional/lua/commands_spec.lua b/test/functional/lua/commands_spec.lua
index 9b9ba531b0..b3c3f937ac 100644
--- a/test/functional/lua/commands_spec.lua
+++ b/test/functional/lua/commands_spec.lua
@@ -141,6 +141,15 @@ describe(':lua command', function()
{4:Press ENTER or type command to continue}^ |
]]}
end)
+
+ it('Can print results of =expr', function()
+ helpers.exec_lua("x = 5")
+ eq("5", helpers.exec_capture(':lua =x'))
+ helpers.exec_lua("function x() return 'hello' end")
+ 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'))
+ end)
end)
describe(':luado command', function()