From ac8cd5368db83cced9bc049ceb50c21cb8a4f743 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 12 Mar 2024 10:44:53 +0800 Subject: refactor: use ml_get_buf_len() in API code (#27825) --- src/nvim/lua/executor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 1a9bd026b5..08677b77b0 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1767,7 +1767,7 @@ void ex_luado(exarg_T *const eap) 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. - const size_t old_line_len = strlen(old_line); + const colnr_T old_line_len = ml_get_buf_len(curbuf, l); lua_pushstring(lstate, old_line); lua_pushnumber(lstate, (lua_Number)l); if (nlua_pcall(lstate, 2, 1)) { @@ -1791,7 +1791,7 @@ void ex_luado(exarg_T *const eap) } } ml_replace(l, new_line_transformed, false); - inserted_bytes(l, 0, (int)old_line_len, (int)new_line_len); + inserted_bytes(l, 0, old_line_len, (int)new_line_len); } lua_pop(lstate, 1); } -- cgit From 08fc1ebbaa49e3110b65bddeed28d2e61a96f5d9 Mon Sep 17 00:00:00 2001 From: bfredl Date: Mon, 11 Mar 2024 13:19:49 +0100 Subject: fix(api/buffer): fix handling of viewport of non-current buffer A lot of functions in move.c only worked for curwin, alternatively took a `wp` arg but still only work if that happens to be curwin. Refactor those that are needed for update_topline(wp) to work for any window. fixes #27723 fixes #27720 --- src/nvim/lua/executor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 08677b77b0..78c746d169 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1797,7 +1797,7 @@ void ex_luado(exarg_T *const eap) } lua_pop(lstate, 1); - check_cursor(); + check_cursor(curwin); redraw_curbuf_later(UPD_NOT_VALID); } -- cgit From aca2048bcd57937ea1c7b7f0325f25d5b82588db Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 18 Mar 2024 23:19:01 +0000 Subject: refactor(treesitter): redesign query iterating Problem: `TSNode:_rawquery()` is complicated, has known issues and the Lua and C code is awkwardly coupled (see logic with `active`). Solution: - Add `TSQueryCursor` and `TSQueryMatch` bindings. - Replace `TSNode:_rawquery()` with `TSQueryCursor:next_capture()` and `TSQueryCursor:next_match()` - Do more stuff in Lua - API for `Query:iter_captures()` and `Query:iter_matches()` remains the same. - `treesitter.c` no longer contains any logic related to predicates. - Add `match_limit` option to `iter_matches()`. Default is still 256. --- src/nvim/lua/executor.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 78c746d169..d5d35c5295 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1909,6 +1909,9 @@ static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_pushcfunction(lstate, tslua_push_parser); lua_setfield(lstate, -2, "_create_ts_parser"); + lua_pushcfunction(lstate, tslua_push_querycursor); + lua_setfield(lstate, -2, "_create_ts_querycursor"); + lua_pushcfunction(lstate, tslua_add_language); lua_setfield(lstate, -2, "_ts_add_language"); -- cgit From f29c41d665fde8e03848db12093219466deceda2 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 29 Mar 2024 18:37:07 +0800 Subject: test: add a bit more testing for vim.on_key() (#28095) Also: - Don't use NUMBUFLEN as buffer length as its unrelated. - Restore accidentally removed comment from last commit. --- src/nvim/lua/executor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index d5d35c5295..9e4b698b69 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -2066,7 +2066,7 @@ char *nlua_register_table_as_callable(const typval_T *const arg) void nlua_execute_on_key(int c) { - char buf[NUMBUFLEN]; + char buf[MB_MAXBYTES * 3 + 4]; size_t buf_len = special_to_buf(c, mod_mask, false, buf); lua_State *const lstate = global_lstate; -- cgit From e1ff2c51cad755d0ddc04a23df23e317d77023ed Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 31 Mar 2024 11:20:05 +0800 Subject: feat(lua): pass keys before mapping to vim.on_key() callback (#28098) Keys before mapping (i.e. typed keys) are passed as the second argument. --- src/nvim/lua/executor.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 9e4b698b69..cfc68dc08f 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -2064,7 +2064,7 @@ char *nlua_register_table_as_callable(const typval_T *const arg) return name; } -void nlua_execute_on_key(int c) +void nlua_execute_on_key(int c, char *typed_buf, size_t typed_len) { char buf[MB_MAXBYTES * 3 + 4]; size_t buf_len = special_to_buf(c, mod_mask, false, buf); @@ -2085,9 +2085,12 @@ void nlua_execute_on_key(int c) // [ vim, vim._on_key, buf ] lua_pushlstring(lstate, buf, buf_len); + // [ vim, vim._on_key, buf, typed_buf ] + lua_pushlstring(lstate, typed_buf, typed_len); + int save_got_int = got_int; got_int = false; // avoid interrupts when the key typed is Ctrl-C - if (nlua_pcall(lstate, 1, 0)) { + if (nlua_pcall(lstate, 2, 0)) { nlua_error(lstate, _("Error executing vim.on_key Lua callback: %.*s")); } -- cgit From f150b62423d57b6f9fbe57330589937dfbb34f4a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 17 Apr 2024 05:44:06 +0800 Subject: fix(lua): only free luarefs when returning from API (#28373) --- src/nvim/lua/executor.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index cfc68dc08f..9096cac619 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -103,7 +103,7 @@ typedef struct { if (args[i].v_type == VAR_UNKNOWN) { \ lua_pushnil(lstate); \ } else { \ - nlua_push_typval(lstate, &args[i], special); \ + nlua_push_typval(lstate, &args[i], (special) ? kNluaPushSpecial : 0); \ } \ } @@ -325,7 +325,7 @@ static int nlua_thr_api_nvim__get_runtime(lua_State *lstate) } ArrayOf(String) ret = runtime_get_named_thread(is_lua, pat, all); - nlua_push_Array(lstate, ret, true); + nlua_push_Array(lstate, ret, kNluaPushSpecial); api_free_array(ret); api_free_array(pat); @@ -1210,7 +1210,7 @@ int nlua_call(lua_State *lstate) }); if (!ERROR_SET(&err)) { - nlua_push_typval(lstate, &rettv, false); + nlua_push_typval(lstate, &rettv, 0); } tv_clear(&rettv); @@ -1261,7 +1261,7 @@ static int nlua_rpc(lua_State *lstate, bool request) ArenaMem res_mem = NULL; Object result = rpc_send_call(chan_id, name, args, &res_mem, &err); if (!ERROR_SET(&err)) { - nlua_push_Object(lstate, &result, false); + nlua_push_Object(lstate, &result, 0); arena_mem_free(res_mem); } } else { @@ -1564,7 +1564,7 @@ Object nlua_exec(const String str, const Array args, LuaRetMode mode, Arena *are } for (size_t i = 0; i < args.size; i++) { - nlua_push_Object(lstate, &args.items[i], false); + nlua_push_Object(lstate, &args.items[i], 0); } if (nlua_pcall(lstate, (int)args.size, 1)) { @@ -1611,7 +1611,7 @@ Object nlua_call_ref(LuaRef ref, const char *name, Array args, LuaRetMode mode, nargs++; } for (size_t i = 0; i < args.size; i++) { - nlua_push_Object(lstate, &args.items[i], false); + nlua_push_Object(lstate, &args.items[i], 0); } if (nlua_pcall(lstate, nargs, 1)) { -- cgit From c32fcd1ed527236e52cfd78033685e713ec36d75 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 25 Apr 2024 08:26:49 +0800 Subject: refactor(source): remove unnecessary concatenation with Lua (#28499) --- src/nvim/lua/executor.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) (limited to 'src/nvim/lua/executor.c') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 9096cac619..a76b8213e5 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1487,7 +1487,7 @@ static void nlua_typval_exec(const char *lcmd, size_t lcmd_len, const char *name } } -int nlua_source_using_linegetter(LineGetter fgetline, void *cookie, char *name) +void nlua_source_str(const char *code, char *name) { const sctx_T save_current_sctx = current_sctx; current_sctx.sc_sid = SID_STR; @@ -1495,22 +1495,11 @@ int nlua_source_using_linegetter(LineGetter fgetline, void *cookie, char *name) current_sctx.sc_lnum = 0; estack_push(ETYPE_SCRIPT, name, 0); - garray_T ga; - char *line = NULL; - - ga_init(&ga, (int)sizeof(char *), 10); - while ((line = fgetline(0, cookie, 0, false)) != NULL) { - GA_APPEND(char *, &ga, line); - } - char *code = ga_concat_strings_sep(&ga, "\n"); size_t len = strlen(code); nlua_typval_exec(code, len, name, NULL, 0, false, NULL); estack_pop(); current_sctx = save_current_sctx; - ga_clear_strings(&ga); - xfree(code); - return OK; } /// Call a LuaCallable given some typvals -- cgit