aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/lua
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/lua')
-rw-r--r--src/nvim/lua/executor.c43
-rw-r--r--src/nvim/lua/treesitter.c4
2 files changed, 31 insertions, 16 deletions
diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c
index 3139e924a1..5d51d58b1d 100644
--- a/src/nvim/lua/executor.c
+++ b/src/nvim/lua/executor.c
@@ -1641,19 +1641,20 @@ bool nlua_is_deferred_safe(void)
return in_fast_callback == 0;
}
-/// Run lua string
+/// Executes Lua code.
///
-/// Used for :lua.
+/// Implements `:lua` and `:lua ={expr}`.
///
-/// @param eap Vimscript command being run.
+/// @param eap Vimscript `:lua {code}`, `:{range}lua`, or `:lua ={expr}` command.
void ex_lua(exarg_T *const eap)
FUNC_ATTR_NONNULL_ALL
{
+ // ":{range}lua"
if (eap->addr_count > 0 || *eap->arg == NUL) {
if (eap->addr_count > 0 && *eap->arg == NUL) {
cmd_source_buffer(eap, true);
} else {
- semsg(_(e_invarg2), "exactly one of {chunk} and {range} required");
+ semsg(_(e_invarg2), "exactly one of {chunk} or {range} required");
}
return;
}
@@ -1664,13 +1665,14 @@ void ex_lua(exarg_T *const eap)
xfree(code);
return;
}
- // When =expr is used transform it to vim.print(expr)
+
+ // ":lua {code}", ":={expr}" or ":lua ={expr}"
+ //
+ // When "=expr" is used transform it to "vim.print(expr)".
if (eap->cmdidx == CMD_equal || code[0] == '=') {
size_t off = (eap->cmdidx == CMD_equal) ? 0 : 1;
len += sizeof("vim.print()") - 1 - off;
- // 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.
+ // `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);
xfree(code);
@@ -1682,11 +1684,11 @@ void ex_lua(exarg_T *const eap)
xfree(code);
}
-/// Run lua string for each line in range
+/// Executes Lua code for-each line in a buffer range.
///
-/// Used for :luado.
+/// Implements `:luado`.
///
-/// @param eap Vimscript command being run.
+/// @param eap Vimscript `:luado {code}` command.
void ex_luado(exarg_T *const eap)
FUNC_ATTR_NONNULL_ALL
{
@@ -1730,10 +1732,15 @@ void ex_luado(exarg_T *const eap)
nlua_error(lstate, _("E5110: Error executing lua: %.*s"));
return;
}
+
+ buf_T *const was_curbuf = curbuf;
+
for (linenr_T l = eap->line1; l <= eap->line2; l++) {
+ // Check the line number, the command may have deleted lines.
if (l > curbuf->b_ml.ml_line_count) {
break;
}
+
lua_pushvalue(lstate, -1);
const char *const old_line = ml_get_buf(curbuf, l);
// Get length of old_line here as calling Lua code may free it.
@@ -1744,6 +1751,13 @@ void ex_luado(exarg_T *const eap)
nlua_error(lstate, _("E5111: Error calling lua: %.*s"));
break;
}
+
+ // Catch the command switching to another buffer.
+ // Check the line number, the command may have deleted lines.
+ if (curbuf != was_curbuf || l > curbuf->b_ml.ml_line_count) {
+ break;
+ }
+
if (lua_isstring(lstate, -1)) {
size_t new_line_len;
const char *const new_line = lua_tolstring(lstate, -1, &new_line_len);
@@ -1758,16 +1772,17 @@ void ex_luado(exarg_T *const eap)
}
lua_pop(lstate, 1);
}
+
lua_pop(lstate, 1);
check_cursor();
redraw_curbuf_later(UPD_NOT_VALID);
}
-/// Run lua file
+/// Executes Lua code from a file location.
///
-/// Used for :luafile.
+/// Implements `:luafile`.
///
-/// @param eap Vimscript command being run.
+/// @param eap Vimscript `:luafile {file}` command.
void ex_luafile(exarg_T *const eap)
FUNC_ATTR_NONNULL_ALL
{
diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c
index 8b62bff496..de17aabca2 100644
--- a/src/nvim/lua/treesitter.c
+++ b/src/nvim/lua/treesitter.c
@@ -1,5 +1,5 @@
-// lua bindings for tree-sitter.
-// NB: this file mostly contains a generic lua interface for tree-sitter
+// lua bindings for treesitter.
+// NB: this file mostly contains a generic lua interface for treesitter
// trees and nodes, and could be broken out as a reusable lua package
#include <assert.h>