+26
-3
lines changedFilter options
+26
-3
lines changed Original file line number Diff line number Diff line change
@@ -249,13 +249,15 @@ arguments separated by " " (space) instead of "\t" (tab).
249
249
*:lua*
250
250
:[range]lua {chunk}
251
251
Executes Lua chunk {chunk}.
252
-
252
+
if {chunk} starts with "=" the rest of the chunk is
253
+
evaluated as an expression and printed. `:lua =expr`
254
+
is equivalent to `:lua print(vim.inspect(expr))`
253
255
Examples: >
254
256
:lua vim.api.nvim_command('echo "Hello, Nvim!"')
255
257
< To see the Lua version: >
256
258
:lua print(_VERSION)
257
259
< To see the LuaJIT version: >
258
-
:lua print(jit.version)
260
+
:lua =jit.version
259
261
<
260
262
*:lua-heredoc*
261
263
:[range]lua << [endmarker]
Original file line number Diff line number Diff line change
@@ -1115,11 +1115,23 @@ void ex_lua(exarg_T *const eap)
1115
1115
FUNC_ATTR_NONNULL_ALL
1116
1116
{
1117
1117
size_t len;
1118
-
char *const code = script_get(eap, &len);
1118
+
char *code = script_get(eap, &len);
1119
1119
if (eap->skip) {
1120
1120
xfree(code);
1121
1121
return;
1122
1122
}
1123
+
// When =expr is used transform it to print(vim.inspect(expr))
1124
+
if (code[0] == '=') {
1125
+
len += sizeof("print(vim.inspect())") - sizeof("=");
1126
+
// code_buf needs to be 1 char larger then len for null byte in the end.
1127
+
// lua nlua_typval_exec doesn't expect null terminated string so len
1128
+
// needs to end before null byte.
1129
+
char *code_buf = xmallocz(len);
1130
+
vim_snprintf(code_buf, len+1, "print(vim.inspect(%s))", code+1);
1131
+
xfree(code);
1132
+
code = code_buf;
1133
+
}
1134
+
1123
1135
nlua_typval_exec(code, len, ":lua", NULL, 0, false, NULL);
1124
1136
1125
1137
xfree(code);
Original file line number Diff line number Diff line change
@@ -141,6 +141,15 @@ describe(':lua command', function()
141
141
{4:Press ENTER or type command to continue}^ |
142
142
]]}
143
143
end)
144
+
145
+
it('Can print results of =expr', function()
146
+
helpers.exec_lua("x = 5")
147
+
eq("5", helpers.exec_capture(':lua =x'))
148
+
helpers.exec_lua("function x() return 'hello' end")
149
+
eq([["hello"]], helpers.exec_capture(':lua = x()'))
150
+
helpers.exec_lua("x = {a = 1, b = 2}")
151
+
eq("{\n a = 1,\n b = 2\n}", helpers.exec_capture(':lua =x'))
152
+
end)
144
153
end)
145
154
146
155
describe(':luado command', function()
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4