diff options
-rw-r--r-- | runtime/doc/lua.txt | 2 | ||||
-rw-r--r-- | runtime/lua/vim/_editor.lua | 27 | ||||
-rw-r--r-- | src/nvim/lua/executor.c | 6 | ||||
-rw-r--r-- | test/functional/lua/commands_spec.lua | 11 |
4 files changed, 28 insertions, 18 deletions
diff --git a/runtime/doc/lua.txt b/runtime/doc/lua.txt index c184e4792d..fa3367602d 100644 --- a/runtime/doc/lua.txt +++ b/runtime/doc/lua.txt @@ -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!"') diff --git a/runtime/lua/vim/_editor.lua b/runtime/lua/vim/_editor.lua index 975f3fea4a..e29d8f1c30 100644 --- a/runtime/lua/vim/_editor.lua +++ b/runtime/lua/vim/_editor.lua @@ -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. diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 91ff285046..d1b83a6b4d 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -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; } diff --git a/test/functional/lua/commands_spec.lua b/test/functional/lua/commands_spec.lua index fb55611198..cb3b436f9e 100644 --- a/test/functional/lua/commands_spec.lua +++ b/test/functional/lua/commands_spec.lua @@ -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) |