From 69bb145cea56067e6e82ed0a130a51c0d611e540 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 4 Feb 2023 20:14:31 +0800 Subject: refactor(exit): pass error message to preserve_exit() (#22097) Problem: 1. Some calls to preserve_exit() don't put a message in IObuff, so the IObuff printed by preserve_exit() contains unrelated information. 2. If a TUI client runs out of memory or receives a deadly signal, the error message is shown on alternate screen and cannot be easily seen because the TUI exits alternate screen soon afterwards. Solution: Pass error message to preserve_exit() and exit alternate screen before printing it. Note that this doesn't fix the problem that server error messages cannot be easily seen on exit. This is tracked in #21608 and #21843. --- src/nvim/lua/executor.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 1415ceeaed..007662fbc9 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -211,9 +211,7 @@ static int nlua_luv_cfpcall(lua_State *lstate, int nargs, int nresult, int flags if (status) { if (status == LUA_ERRMEM && !(flags & LUVF_CALLBACK_NOEXIT)) { // consider out of memory errors unrecoverable, just like xmalloc() - os_errmsg(e_outofmem); - os_errmsg("\n"); - preserve_exit(); + preserve_exit(e_outofmem); } const char *error = lua_tostring(lstate, -1); -- cgit From 7224c889e0d5d70b99ae377036baa6377c33a568 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Sat, 11 Feb 2023 10:25:24 +0100 Subject: build: enable MSVC level 3 warnings (#21934) MSVC has 4 different warning levels: 1 (severe), 2 (significant), 3 (production quality) and 4 (informational). Enabling level 3 warnings mostly revealed conversion problems, similar to GCC/clang -Wconversion flag. --- src/nvim/lua/stdlib.c | 14 +++++++------- src/nvim/lua/treesitter.c | 16 ++++++++-------- src/nvim/lua/xdiff.c | 4 ++-- 3 files changed, 17 insertions(+), 17 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 6ebca6d97e..73246f81b7 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -78,20 +78,20 @@ static int regex_match_line(lua_State *lstate) return luaL_error(lstate, "not enough args"); } - long bufnr = luaL_checkinteger(lstate, 2); + handle_T bufnr = (handle_T)luaL_checkinteger(lstate, 2); linenr_T rownr = (linenr_T)luaL_checkinteger(lstate, 3); - long start = 0, end = -1; + int start = 0, end = -1; if (narg >= 4) { - start = luaL_checkinteger(lstate, 4); + start = (int)luaL_checkinteger(lstate, 4); } if (narg >= 5) { - end = luaL_checkinteger(lstate, 5); + end = (int)luaL_checkinteger(lstate, 5); if (end < 0) { return luaL_error(lstate, "invalid end"); } } - buf_T *buf = bufnr ? handle_get_buffer((int)bufnr) : curbuf; + buf_T *buf = bufnr ? handle_get_buffer(bufnr) : curbuf; if (!buf || buf->b_ml.ml_mfp == NULL) { return luaL_error(lstate, "invalid buffer"); } @@ -218,7 +218,7 @@ static int nlua_str_utf_start(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL { size_t s1_len; const char *s1 = luaL_checklstring(lstate, 1, &s1_len); - long offset = luaL_checkinteger(lstate, 2); + ptrdiff_t offset = luaL_checkinteger(lstate, 2); if (offset < 0 || offset > (intptr_t)s1_len) { return luaL_error(lstate, "index out of range"); } @@ -238,7 +238,7 @@ static int nlua_str_utf_end(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL { size_t s1_len; const char *s1 = luaL_checklstring(lstate, 1, &s1_len); - long offset = luaL_checkinteger(lstate, 2); + ptrdiff_t offset = luaL_checkinteger(lstate, 2); if (offset < 0 || offset > (intptr_t)s1_len) { return luaL_error(lstate, "index out of range"); } diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 56f4daed1a..5248ebed14 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -393,7 +393,7 @@ static int parser_parse(lua_State *L) TSTree *new_tree = NULL; size_t len; const char *str; - long bufnr; + handle_T bufnr; buf_T *buf; TSInput input; @@ -406,13 +406,13 @@ static int parser_parse(lua_State *L) break; case LUA_TNUMBER: - bufnr = lua_tointeger(L, 3); - buf = handle_get_buffer((handle_T)bufnr); + bufnr = (handle_T)lua_tointeger(L, 3); + buf = handle_get_buffer(bufnr); if (!buf) { #define BUFSIZE 256 char ebuf[BUFSIZE] = { 0 }; - vim_snprintf(ebuf, BUFSIZE, "invalid buffer handle: %ld", bufnr); + vim_snprintf(ebuf, BUFSIZE, "invalid buffer handle: %d", bufnr); return luaL_argerror(L, 3, ebuf); #undef BUFSIZE } @@ -898,8 +898,8 @@ static int node_child(lua_State *L) if (!node_check(L, 1, &node)) { return 0; } - long num = lua_tointeger(L, 2); - TSNode child = ts_node_child(node, (uint32_t)num); + uint32_t num = (uint32_t)lua_tointeger(L, 2); + TSNode child = ts_node_child(node, num); push_node(L, child, 1); return 1; @@ -911,8 +911,8 @@ static int node_named_child(lua_State *L) if (!node_check(L, 1, &node)) { return 0; } - long num = lua_tointeger(L, 2); - TSNode child = ts_node_named_child(node, (uint32_t)num); + uint32_t num = (uint32_t)lua_tointeger(L, 2); + TSNode child = ts_node_named_child(node, num); push_node(L, child, 1); return 1; diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 857b159af5..9a7ae5c146 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -257,13 +257,13 @@ static NluaXdiffMode process_xdl_diff_opts(lua_State *lstate, xdemitconf_t *cfg, if (check_xdiff_opt(v->type, kObjectTypeInteger, "ctxlen", err)) { goto exit_1; } - cfg->ctxlen = v->data.integer; + cfg->ctxlen = (long)v->data.integer; } else if (strequal("interhunkctxlen", k.data)) { if (check_xdiff_opt(v->type, kObjectTypeInteger, "interhunkctxlen", err)) { goto exit_1; } - cfg->interhunkctxlen = v->data.integer; + cfg->interhunkctxlen = (long)v->data.integer; } else if (strequal("linematch", k.data)) { *linematch = api_object_to_bool(*v, "linematch", false, err); if (ERROR_SET(err)) { -- cgit From 4be6c6cf0ddf5e31d4103cb5df06651ba6f4897b Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Sat, 11 Feb 2023 11:05:57 +0100 Subject: refactor: replace char_u with char (#21901) refactor: replace char_u with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/lua/executor.c | 2 +- src/nvim/lua/stdlib.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 007662fbc9..5fbbb342bf 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1993,7 +1993,7 @@ char *nlua_register_table_as_callable(const typval_T *const arg) void nlua_execute_on_key(int c) { char buf[NUMBUFLEN]; - size_t buf_len = special_to_buf(c, mod_mask, false, (char_u *)buf); + size_t buf_len = special_to_buf(c, mod_mask, false, buf); lua_State *const lstate = global_lstate; diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 73246f81b7..5aeff4de98 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -222,7 +222,7 @@ static int nlua_str_utf_start(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL if (offset < 0 || offset > (intptr_t)s1_len) { return luaL_error(lstate, "index out of range"); } - int head_offset = utf_cp_head_off((char_u *)s1, (char_u *)s1 + offset - 1); + int head_offset = utf_cp_head_off(s1, s1 + offset - 1); lua_pushinteger(lstate, head_offset); return 1; } -- cgit From 27177e581902967dcf4f2f426464da1b636ca420 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Sat, 11 Feb 2023 14:14:24 +0100 Subject: refactor: reduce scope of locals as per the style guide (#22211) --- src/nvim/lua/executor.c | 3 +-- src/nvim/lua/spell.c | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 5fbbb342bf..c8fc76e20d 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1766,13 +1766,12 @@ bool nlua_exec_file(const char *path) StringBuilder sb = KV_INITIAL_VALUE; kv_resize(sb, 64); - ptrdiff_t read_size = -1; // Read all input from stdin, unless interrupted (ctrl-c). while (true) { if (got_int) { // User canceled. return false; } - read_size = file_read(stdin_dup, IObuff, 64); + ptrdiff_t read_size = file_read(stdin_dup, IObuff, 64); if (read_size < 0) { // Error. return false; } diff --git a/src/nvim/lua/spell.c b/src/nvim/lua/spell.c index d510d25e90..78e023c26d 100644 --- a/src/nvim/lua/spell.c +++ b/src/nvim/lua/spell.c @@ -51,7 +51,6 @@ int nlua_spell_check(lua_State *lstate) } hlf_T attr = HLF_COUNT; - size_t len = 0; size_t pos = 0; int capcol = -1; int no_res = 0; @@ -61,7 +60,7 @@ int nlua_spell_check(lua_State *lstate) while (*str != NUL) { attr = HLF_COUNT; - len = spell_check(curwin, (char *)str, &attr, &capcol, false); + size_t len = spell_check(curwin, (char *)str, &attr, &capcol, false); assert(len <= INT_MAX); if (attr != HLF_COUNT) { -- cgit From 799edca18a4ddcf8edcb63dd391219e01e187f0d Mon Sep 17 00:00:00 2001 From: bfredl Date: Mon, 28 Nov 2022 22:43:10 +0100 Subject: feat(lua): make sure require'bit' always works, even with PUC lua 5.1 --- src/nvim/lua/stdlib.c | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 5aeff4de98..d9682ff63d 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -11,6 +11,10 @@ #include #include +#ifdef NVIM_VENDOR_BIT +# include "bit.h" +#endif + #include "auto/config.h" #include "cjson/lua_cjson.h" #include "mpack/lmpack.h" @@ -596,6 +600,13 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread) // vim.json lua_cjson_new(lstate); lua_setfield(lstate, -2, "json"); + +#ifdef NVIM_VENDOR_BIT + // if building with puc lua, use internal fallback for require'bit' + int top = lua_gettop(lstate); + luaopen_bit(lstate); + lua_settop(lstate, top); +#endif } /// like luaL_error, but allow cleanup -- cgit From 774e59f3f9bf50c8350857c6722bb58df2dd940a Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sun, 26 Feb 2023 16:53:33 +0000 Subject: feat(treesitter): expand the API --- src/nvim/lua/treesitter.c | 138 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 123 insertions(+), 15 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 5248ebed14..d72e7a1825 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -51,8 +51,11 @@ static struct luaL_Reg parser_meta[] = { { "__gc", parser_gc }, { "__tostring", parser_tostring }, { "parse", parser_parse }, + { "reset", parser_reset }, { "set_included_ranges", parser_set_ranges }, { "included_ranges", parser_get_ranges }, + { "set_timeout", parser_set_timeout }, + { "timeout", parser_get_timeout }, { NULL, NULL } }; @@ -78,6 +81,8 @@ static struct luaL_Reg node_meta[] = { { "field", node_field }, { "named", node_named }, { "missing", node_missing }, + { "extra", node_extra }, + { "has_changes", node_has_changes }, { "has_error", node_has_error }, { "sexpr", node_sexpr }, { "child_count", node_child_count }, @@ -95,7 +100,9 @@ static struct luaL_Reg node_meta[] = { { "prev_named_sibling", node_prev_named_sibling }, { "named_children", node_named_children }, { "root", node_root }, + { "tree", node_tree }, { "byte_length", node_byte_length }, + { "equal", node_equal }, { NULL, NULL } }; @@ -446,6 +453,16 @@ static int parser_parse(lua_State *L) return 2; } +static int parser_reset(lua_State *L) +{ + TSParser **p = parser_check(L, 1); + if (p && *p) { + ts_parser_reset(*p); + } + + return 0; +} + static int tree_copy(lua_State *L) { TSTree **tree = tree_check(L, 1); @@ -597,6 +614,33 @@ static int parser_get_ranges(lua_State *L) return 1; } +static int parser_set_timeout(lua_State *L) +{ + TSParser **p = parser_check(L, 1); + if (!p) { + return 0; + } + + if (lua_gettop(L) < 2) { + luaL_error(L, "integer expected"); + } + + uint32_t timeout = (uint32_t)luaL_checkinteger(L, 2); + ts_parser_set_timeout_micros(*p, timeout); + return 0; +} + +static int parser_get_timeout(lua_State *L) +{ + TSParser **p = parser_check(L, 1); + if (!p) { + return 0; + } + + lua_pushinteger(L, (long)ts_parser_timeout_micros(*p)); + return 0; +} + // Tree methods /// push tree interface on lua stack. @@ -740,12 +784,29 @@ static int node_range(lua_State *L) if (!node_check(L, 1, &node)) { return 0; } + + bool include_bytes = false; + if (lua_gettop(L) >= 2) { + include_bytes = lua_toboolean(L, 2); + } + TSPoint start = ts_node_start_point(node); TSPoint end = ts_node_end_point(node); - lua_pushnumber(L, start.row); - lua_pushnumber(L, start.column); - lua_pushnumber(L, end.row); - lua_pushnumber(L, end.column); + + if (include_bytes) { + lua_pushinteger(L, start.row); + lua_pushinteger(L, start.column); + lua_pushinteger(L, ts_node_start_byte(node)); + lua_pushinteger(L, end.row); + lua_pushinteger(L, end.column); + lua_pushinteger(L, ts_node_end_byte(node)); + return 6; + } + + lua_pushinteger(L, start.row); + lua_pushinteger(L, start.column); + lua_pushinteger(L, end.row); + lua_pushinteger(L, end.column); return 4; } @@ -757,9 +818,9 @@ static int node_start(lua_State *L) } TSPoint start = ts_node_start_point(node); uint32_t start_byte = ts_node_start_byte(node); - lua_pushnumber(L, start.row); - lua_pushnumber(L, start.column); - lua_pushnumber(L, start_byte); + lua_pushinteger(L, start.row); + lua_pushinteger(L, start.column); + lua_pushinteger(L, start_byte); return 3; } @@ -771,9 +832,9 @@ static int node_end(lua_State *L) } TSPoint end = ts_node_end_point(node); uint32_t end_byte = ts_node_end_byte(node); - lua_pushnumber(L, end.row); - lua_pushnumber(L, end.column); - lua_pushnumber(L, end_byte); + lua_pushinteger(L, end.row); + lua_pushinteger(L, end.column); + lua_pushinteger(L, end_byte); return 3; } @@ -784,7 +845,7 @@ static int node_child_count(lua_State *L) return 0; } uint32_t count = ts_node_child_count(node); - lua_pushnumber(L, count); + lua_pushinteger(L, count); return 1; } @@ -795,7 +856,7 @@ static int node_named_child_count(lua_State *L) return 0; } uint32_t count = ts_node_named_child_count(node); - lua_pushnumber(L, count); + lua_pushinteger(L, count); return 1; } @@ -816,7 +877,7 @@ static int node_symbol(lua_State *L) return 0; } TSSymbol symbol = ts_node_symbol(node); - lua_pushnumber(L, symbol); + lua_pushinteger(L, symbol); return 1; } @@ -882,6 +943,26 @@ static int node_missing(lua_State *L) return 1; } +static int node_extra(lua_State *L) +{ + TSNode node; + if (!node_check(L, 1, &node)) { + return 0; + } + lua_pushboolean(L, ts_node_is_extra(node)); + return 1; +} + +static int node_has_changes(lua_State *L) +{ + TSNode node; + if (!node_check(L, 1, &node)) { + return 0; + } + lua_pushboolean(L, ts_node_has_changes(node)); + return 1; +} + static int node_has_error(lua_State *L) { TSNode node; @@ -1108,6 +1189,17 @@ static int node_root(lua_State *L) return 1; } +static int node_tree(lua_State *L) +{ + TSNode node; + if (!node_check(L, 1, &node)) { + return 0; + } + + push_tree(L, (TSTree *)node.tree, false); + return 1; +} + static int node_byte_length(lua_State *L) { TSNode node; @@ -1118,7 +1210,23 @@ static int node_byte_length(lua_State *L) uint32_t start_byte = ts_node_start_byte(node); uint32_t end_byte = ts_node_end_byte(node); - lua_pushnumber(L, end_byte - start_byte); + lua_pushinteger(L, end_byte - start_byte); + return 1; +} + +static int node_equal(lua_State *L) +{ + TSNode node1; + if (!node_check(L, 1, &node1)) { + return 0; + } + + TSNode node2; + if (!node_check(L, 2, &node2)) { + return luaL_error(L, "TSNode expected"); + } + + lua_pushboolean(L, ts_node_eq(node1, node2)); return 1; } @@ -1367,7 +1475,7 @@ static int query_inspect(lua_State *L) &strlen); lua_pushlstring(L, str, strlen); // [retval, patterns, pat, pred, item] } else if (step[k].type == TSQueryPredicateStepTypeCapture) { - lua_pushnumber(L, step[k].value_id + 1); // [..., pat, pred, item] + lua_pushinteger(L, step[k].value_id + 1); // [..., pat, pred, item] } else { abort(); } -- cgit From c002fd421e3d8deb4adf144cadcd3a6fe4635e12 Mon Sep 17 00:00:00 2001 From: bfredl Date: Mon, 27 Feb 2023 19:41:40 +0100 Subject: refactor(build): graduate libtreesitter features which are 1+ years old --- src/nvim/lua/treesitter.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index d72e7a1825..2db12b8ded 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -152,9 +152,7 @@ void tslua_init(lua_State *L) build_meta(L, TS_META_QUERYCURSOR, querycursor_meta); build_meta(L, TS_META_TREECURSOR, treecursor_meta); -#ifdef NVIM_TS_HAS_SET_ALLOCATOR ts_set_allocator(xmalloc, xcalloc, xrealloc, xfree); -#endif } int tslua_has_language(lua_State *L) @@ -1321,11 +1319,7 @@ static int node_rawquery(lua_State *L) } else { cursor = ts_query_cursor_new(); } - // TODO(clason): API introduced after tree-sitter release 0.19.5 - // remove guard when minimum ts version is bumped to 0.19.6+ -#ifdef NVIM_TS_HAS_SET_MATCH_LIMIT ts_query_cursor_set_match_limit(cursor, 64); -#endif ts_query_cursor_exec(cursor, query, node); bool captures = lua_toboolean(L, 3); -- cgit From 7e90f247e7b3add74a49895054a543c2140bbb08 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Fri, 3 Mar 2023 16:16:17 +0100 Subject: fix(treesitter): raise ts_match_limit to 256 (#22497) Problem: Some complex queries may not return all matches. Solution: Raise `ts_match_limit` from current 64 (twice the original default) to 256 (which Helix uses, and seems to be enough for the reported problematic cases). If this leads performance regressions in other queries, we should add a generic querying timeout instead of relying on a low value here. --- src/nvim/lua/treesitter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 2db12b8ded..4cbe7329a6 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -1319,7 +1319,7 @@ static int node_rawquery(lua_State *L) } else { cursor = ts_query_cursor_new(); } - ts_query_cursor_set_match_limit(cursor, 64); + ts_query_cursor_set_match_limit(cursor, 256); ts_query_cursor_exec(cursor, query, node); bool captures = lua_toboolean(L, 3); -- cgit From 808691e3993ee54519229c175fb950cc02261287 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 4 Mar 2023 12:08:45 +0800 Subject: fix(luado): get old_line length before executing Lua code --- src/nvim/lua/executor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index c8fc76e20d..bb461a7f13 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1706,7 +1706,9 @@ void ex_luado(exarg_T *const eap) break; } lua_pushvalue(lstate, -1); - const char *old_line = (const char *)ml_get_buf(curbuf, l, false); + const char *const old_line = (const char *)ml_get_buf(curbuf, l, false); + // Get length of old_line here as calling Lua code may free it. + const size_t old_line_len = strlen(old_line); lua_pushstring(lstate, old_line); lua_pushnumber(lstate, (lua_Number)l); if (nlua_pcall(lstate, 2, 1)) { @@ -1714,8 +1716,6 @@ void ex_luado(exarg_T *const eap) break; } if (lua_isstring(lstate, -1)) { - size_t old_line_len = strlen(old_line); - size_t new_line_len; const char *const new_line = lua_tolstring(lstate, -1, &new_line_len); char *const new_line_transformed = xmemdupz(new_line, new_line_len); -- cgit From 0e7196438d8f856eecd7c90e160b79cbc8fb08dc Mon Sep 17 00:00:00 2001 From: Kelly Lin Date: Sun, 19 Feb 2023 22:33:57 +1100 Subject: feat(lua): add semver api --- src/nvim/lua/executor.c | 15 --------------- 1 file changed, 15 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index bb461a7f13..5ec91aebb0 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -165,17 +165,6 @@ static int nlua_pcall(lua_State *lstate, int nargs, int nresults) return status; } -/// Gets the version of the current Nvim build. -/// -/// @param lstate Lua interpreter state. -static int nlua_nvim_version(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL -{ - Dictionary version = version_dict(); - nlua_push_Dictionary(lstate, version, true); - api_free_dictionary(version); - return 1; -} - static void nlua_luv_error_event(void **argv) { char *error = (char *)argv[0]; @@ -739,10 +728,6 @@ static bool nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL // vim.types, vim.type_idx, vim.val_idx nlua_init_types(lstate); - // neovim version - lua_pushcfunction(lstate, &nlua_nvim_version); - lua_setfield(lstate, -2, "version"); - // schedule lua_pushcfunction(lstate, &nlua_schedule); lua_setfield(lstate, -2, "schedule"); -- cgit From 79571b92ced968ad27bee2a7515a4a04e84dbad2 Mon Sep 17 00:00:00 2001 From: Björn Linse Date: Wed, 27 Jan 2021 09:00:28 +0100 Subject: feat(lua): omnifunc for builting lua interpreter also make implicit submodules "uri" and "_inspector" work with completion this is needed for `:lua=vim.uri_` wildmenu completion to work even before uri or _inspector functions are used. --- src/nvim/lua/executor.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index bb461a7f13..8a50c8fe4f 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1872,6 +1872,12 @@ int nlua_expand_pat(expand_T *xp, char *pat, int *num_results, char ***results) lua_getfield(lstate, -1, "_expand_pat"); luaL_checktype(lstate, -1, LUA_TFUNCTION); + // ex expansion prepends a ^, but don't worry, it is not a regex + if (pat[0] != '^') { + return FAIL; + } + pat++; + // [ vim, vim._expand_pat, buf ] lua_pushlstring(lstate, (const char *)pat, strlen(pat)); -- cgit From 276b647fdba07bf1762d8dd371c4b655b8a418df Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 8 Mar 2023 17:22:28 +0000 Subject: refactor(treesitter): delegate region calculation to treesitter (#22553) --- src/nvim/lua/treesitter.c | 54 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 12 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 4cbe7329a6..104fa5f3e9 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -64,6 +64,7 @@ static struct luaL_Reg tree_meta[] = { { "__tostring", tree_tostring }, { "root", tree_root }, { "edit", tree_edit }, + { "included_ranges", tree_get_ranges }, { "copy", tree_copy }, { NULL, NULL } }; @@ -364,19 +365,29 @@ static const char *input_cb(void *payload, uint32_t byte_index, TSPoint position #undef BUFSIZE } -static void push_ranges(lua_State *L, const TSRange *ranges, const size_t length) +static void push_ranges(lua_State *L, const TSRange *ranges, const size_t length, + bool include_bytes) { lua_createtable(L, (int)length, 0); for (size_t i = 0; i < length; i++) { - lua_createtable(L, 4, 0); + lua_createtable(L, include_bytes ? 6 : 4, 0); + int j = 1; lua_pushinteger(L, ranges[i].start_point.row); - lua_rawseti(L, -2, 1); + lua_rawseti(L, -2, j++); lua_pushinteger(L, ranges[i].start_point.column); - lua_rawseti(L, -2, 2); + lua_rawseti(L, -2, j++); + if (include_bytes) { + lua_pushinteger(L, ranges[i].start_byte); + lua_rawseti(L, -2, j++); + } lua_pushinteger(L, ranges[i].end_point.row); - lua_rawseti(L, -2, 3); + lua_rawseti(L, -2, j++); lua_pushinteger(L, ranges[i].end_point.column); - lua_rawseti(L, -2, 4); + lua_rawseti(L, -2, j++); + if (include_bytes) { + lua_pushinteger(L, ranges[i].end_byte); + lua_rawseti(L, -2, j++); + } lua_rawseti(L, -2, (int)(i + 1)); } @@ -395,6 +406,8 @@ static int parser_parse(lua_State *L) old_tree = tmp ? *tmp : NULL; } + bool include_bytes = (lua_gettop(L) >= 3) && lua_toboolean(L, 3); + TSTree *new_tree = NULL; size_t len; const char *str; @@ -445,7 +458,7 @@ static int parser_parse(lua_State *L) push_tree(L, new_tree, false); // [tree] - push_ranges(L, changed, n_ranges); // [tree, ranges] + push_ranges(L, changed, n_ranges, include_bytes); // [tree, ranges] xfree(changed); return 2; @@ -500,6 +513,24 @@ static int tree_edit(lua_State *L) return 0; } +static int tree_get_ranges(lua_State *L) +{ + TSTree **tree = tree_check(L, 1); + if (!(*tree)) { + return 0; + } + + bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); + + uint32_t len; + TSRange *ranges = ts_tree_included_ranges(*tree, &len); + + push_ranges(L, ranges, len, include_bytes); + + xfree(ranges); + return 1; +} + // Use the top of the stack (without popping it) to create a TSRange, it can be // either a lua table or a TSNode static void range_from_lua(lua_State *L, TSRange *range) @@ -605,10 +636,12 @@ static int parser_get_ranges(lua_State *L) return 0; } + bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); + uint32_t len; const TSRange *ranges = ts_parser_included_ranges(*p, &len); - push_ranges(L, ranges, len); + push_ranges(L, ranges, len, include_bytes); return 1; } @@ -783,10 +816,7 @@ static int node_range(lua_State *L) return 0; } - bool include_bytes = false; - if (lua_gettop(L) >= 2) { - include_bytes = lua_toboolean(L, 2); - } + bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); TSPoint start = ts_node_start_point(node); TSPoint end = ts_node_end_point(node); -- cgit From b9f19d3e286d95d9209afbc479fa2eb908067fb1 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 8 Mar 2023 17:59:45 +0000 Subject: Revert "refactor(treesitter): delegate region calculation to treesitter" (#22575) Revert "refactor(treesitter): delegate region calculation to treesitter (#22553)" This reverts commit 276b647fdba07bf1762d8dd371c4b655b8a418df. --- src/nvim/lua/treesitter.c | 54 +++++++++++------------------------------------ 1 file changed, 12 insertions(+), 42 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 104fa5f3e9..4cbe7329a6 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -64,7 +64,6 @@ static struct luaL_Reg tree_meta[] = { { "__tostring", tree_tostring }, { "root", tree_root }, { "edit", tree_edit }, - { "included_ranges", tree_get_ranges }, { "copy", tree_copy }, { NULL, NULL } }; @@ -365,29 +364,19 @@ static const char *input_cb(void *payload, uint32_t byte_index, TSPoint position #undef BUFSIZE } -static void push_ranges(lua_State *L, const TSRange *ranges, const size_t length, - bool include_bytes) +static void push_ranges(lua_State *L, const TSRange *ranges, const size_t length) { lua_createtable(L, (int)length, 0); for (size_t i = 0; i < length; i++) { - lua_createtable(L, include_bytes ? 6 : 4, 0); - int j = 1; + lua_createtable(L, 4, 0); lua_pushinteger(L, ranges[i].start_point.row); - lua_rawseti(L, -2, j++); + lua_rawseti(L, -2, 1); lua_pushinteger(L, ranges[i].start_point.column); - lua_rawseti(L, -2, j++); - if (include_bytes) { - lua_pushinteger(L, ranges[i].start_byte); - lua_rawseti(L, -2, j++); - } + lua_rawseti(L, -2, 2); lua_pushinteger(L, ranges[i].end_point.row); - lua_rawseti(L, -2, j++); + lua_rawseti(L, -2, 3); lua_pushinteger(L, ranges[i].end_point.column); - lua_rawseti(L, -2, j++); - if (include_bytes) { - lua_pushinteger(L, ranges[i].end_byte); - lua_rawseti(L, -2, j++); - } + lua_rawseti(L, -2, 4); lua_rawseti(L, -2, (int)(i + 1)); } @@ -406,8 +395,6 @@ static int parser_parse(lua_State *L) old_tree = tmp ? *tmp : NULL; } - bool include_bytes = (lua_gettop(L) >= 3) && lua_toboolean(L, 3); - TSTree *new_tree = NULL; size_t len; const char *str; @@ -458,7 +445,7 @@ static int parser_parse(lua_State *L) push_tree(L, new_tree, false); // [tree] - push_ranges(L, changed, n_ranges, include_bytes); // [tree, ranges] + push_ranges(L, changed, n_ranges); // [tree, ranges] xfree(changed); return 2; @@ -513,24 +500,6 @@ static int tree_edit(lua_State *L) return 0; } -static int tree_get_ranges(lua_State *L) -{ - TSTree **tree = tree_check(L, 1); - if (!(*tree)) { - return 0; - } - - bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); - - uint32_t len; - TSRange *ranges = ts_tree_included_ranges(*tree, &len); - - push_ranges(L, ranges, len, include_bytes); - - xfree(ranges); - return 1; -} - // Use the top of the stack (without popping it) to create a TSRange, it can be // either a lua table or a TSNode static void range_from_lua(lua_State *L, TSRange *range) @@ -636,12 +605,10 @@ static int parser_get_ranges(lua_State *L) return 0; } - bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); - uint32_t len; const TSRange *ranges = ts_parser_included_ranges(*p, &len); - push_ranges(L, ranges, len, include_bytes); + push_ranges(L, ranges, len); return 1; } @@ -816,7 +783,10 @@ static int node_range(lua_State *L) return 0; } - bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); + bool include_bytes = false; + if (lua_gettop(L) >= 2) { + include_bytes = lua_toboolean(L, 2); + } TSPoint start = ts_node_start_point(node); TSPoint end = ts_node_end_point(node); -- cgit From ae263aff9547b8b513c4fedaceb4cbf93c57b866 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 9 Mar 2023 16:09:39 +0000 Subject: refactor(treesitter): use byte ranges from treesitter (#22589) --- src/nvim/lua/treesitter.c | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 4cbe7329a6..ae69f3f120 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -364,19 +364,29 @@ static const char *input_cb(void *payload, uint32_t byte_index, TSPoint position #undef BUFSIZE } -static void push_ranges(lua_State *L, const TSRange *ranges, const size_t length) +static void push_ranges(lua_State *L, const TSRange *ranges, const size_t length, + bool include_bytes) { lua_createtable(L, (int)length, 0); for (size_t i = 0; i < length; i++) { - lua_createtable(L, 4, 0); + lua_createtable(L, include_bytes ? 6 : 4, 0); + int j = 1; lua_pushinteger(L, ranges[i].start_point.row); - lua_rawseti(L, -2, 1); + lua_rawseti(L, -2, j++); lua_pushinteger(L, ranges[i].start_point.column); - lua_rawseti(L, -2, 2); + lua_rawseti(L, -2, j++); + if (include_bytes) { + lua_pushinteger(L, ranges[i].start_byte); + lua_rawseti(L, -2, j++); + } lua_pushinteger(L, ranges[i].end_point.row); - lua_rawseti(L, -2, 3); + lua_rawseti(L, -2, j++); lua_pushinteger(L, ranges[i].end_point.column); - lua_rawseti(L, -2, 4); + lua_rawseti(L, -2, j++); + if (include_bytes) { + lua_pushinteger(L, ranges[i].end_byte); + lua_rawseti(L, -2, j++); + } lua_rawseti(L, -2, (int)(i + 1)); } @@ -395,6 +405,8 @@ static int parser_parse(lua_State *L) old_tree = tmp ? *tmp : NULL; } + bool include_bytes = (lua_gettop(L) >= 3) && lua_toboolean(L, 3); + TSTree *new_tree = NULL; size_t len; const char *str; @@ -445,7 +457,7 @@ static int parser_parse(lua_State *L) push_tree(L, new_tree, false); // [tree] - push_ranges(L, changed, n_ranges); // [tree, ranges] + push_ranges(L, changed, n_ranges, include_bytes); // [tree, ranges] xfree(changed); return 2; @@ -605,10 +617,12 @@ static int parser_get_ranges(lua_State *L) return 0; } + bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); + uint32_t len; const TSRange *ranges = ts_parser_included_ranges(*p, &len); - push_ranges(L, ranges, len); + push_ranges(L, ranges, len, include_bytes); return 1; } @@ -783,10 +797,7 @@ static int node_range(lua_State *L) return 0; } - bool include_bytes = false; - if (lua_gettop(L) >= 2) { - include_bytes = lua_toboolean(L, 2); - } + bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); TSPoint start = ts_node_start_point(node); TSPoint end = ts_node_end_point(node); -- cgit From 4f75960660f9065214f49ba6ed9176e0a72fa92a Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Fri, 10 Mar 2023 10:08:06 +0000 Subject: fix(treesitter): correct include_bytes arg for parse() --- src/nvim/lua/treesitter.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index ae69f3f120..289a0cb9b4 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -405,8 +405,6 @@ static int parser_parse(lua_State *L) old_tree = tmp ? *tmp : NULL; } - bool include_bytes = (lua_gettop(L) >= 3) && lua_toboolean(L, 3); - TSTree *new_tree = NULL; size_t len; const char *str; @@ -443,6 +441,8 @@ static int parser_parse(lua_State *L) return luaL_argerror(L, 3, "expected either string or buffer handle"); } + bool include_bytes = (lua_gettop(L) >= 4) && lua_toboolean(L, 4); + // Sometimes parsing fails (timeout, or wrong parser ABI) // In those case, just return an error. if (!new_tree) { -- cgit From 46b73bf22cb951151de9bf0712d42e194000b677 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 9 Mar 2023 15:28:55 +0000 Subject: perf(treesitter): more efficient foldexpr --- src/nvim/lua/stdlib.c | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index d9682ff63d..b6e56c35d6 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -26,6 +26,7 @@ #include "nvim/eval/typval.h" #include "nvim/eval/typval_defs.h" #include "nvim/ex_eval.h" +#include "nvim/fold.h" #include "nvim/globals.h" #include "nvim/lua/converter.h" #include "nvim/lua/spell.h" @@ -528,6 +529,31 @@ static int nlua_iconv(lua_State *lstate) return 1; } +// Like 'zx' but don't call newFoldLevel() +static int nlua_foldupdate(lua_State *lstate) +{ + curwin->w_foldinvalid = true; // recompute folds + foldOpenCursor(); + + return 0; +} + +// Access to internal functions. For use in runtime/ +static void nlua_state_add_internal(lua_State *const lstate) +{ + // _getvar + lua_pushcfunction(lstate, &nlua_getvar); + lua_setfield(lstate, -2, "_getvar"); + + // _setvar + lua_pushcfunction(lstate, &nlua_setvar); + lua_setfield(lstate, -2, "_setvar"); + + // _updatefolds + lua_pushcfunction(lstate, &nlua_foldupdate); + lua_setfield(lstate, -2, "_foldupdate"); +} + void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread) { if (!is_thread) { @@ -562,14 +588,6 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread) lua_setfield(lstate, -2, "__index"); // [meta] lua_pop(lstate, 1); // don't use metatable now - // _getvar - lua_pushcfunction(lstate, &nlua_getvar); - lua_setfield(lstate, -2, "_getvar"); - - // _setvar - lua_pushcfunction(lstate, &nlua_setvar); - lua_setfield(lstate, -2, "_setvar"); - // vim.spell luaopen_spell(lstate); lua_setfield(lstate, -2, "spell"); @@ -578,6 +596,8 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread) // depends on p_ambw, p_emoji lua_pushcfunction(lstate, &nlua_iconv); lua_setfield(lstate, -2, "iconv"); + + nlua_state_add_internal(lstate); } // vim.mpack -- cgit From 402c31a82d2961172c6eaf8014762f28c60bd93e Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 11 Mar 2023 17:58:05 +0800 Subject: refactor: move ga_loaded to runtime.c (#22626) --- src/nvim/lua/stdlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index b6e56c35d6..852e10c8d8 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -22,7 +22,6 @@ #include "nvim/api/private/helpers.h" #include "nvim/ascii.h" #include "nvim/buffer_defs.h" -#include "nvim/eval.h" #include "nvim/eval/typval.h" #include "nvim/eval/typval_defs.h" #include "nvim/ex_eval.h" @@ -38,6 +37,7 @@ #include "nvim/memory.h" #include "nvim/pos.h" #include "nvim/regexp.h" +#include "nvim/runtime.h" #include "nvim/types.h" #include "nvim/vim.h" -- cgit From 7dc9182cf0b27cbfb4e289db55dd7b02998ef5c8 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 11 Mar 2023 21:29:25 +0800 Subject: vim-patch:8.2.1398: autoload script sourced twice if sourced directly (#22622) Problem: Autoload script sourced twice if sourced directly. Solution: Do not source an autoload script again. (issue vim/vim#6644) https://github.com/vim/vim/commit/daa2f36573db3e1df7eb1fdbc3a09a2815644048 Cherry-pick ret_sid changes from patch 8.2.0149. Use do_in_runtimepath() as that's what source_runtime() calls in Nvim. Co-authored-by: Bram Moolenaar --- src/nvim/lua/executor.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 078bc4fea9..11a02ea402 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -2061,10 +2061,15 @@ void nlua_set_sctx(sctx_T *current) break; } char *source_path = fix_fname(info->source + 1); - get_current_script_id(&source_path, current); - xfree(source_path); - current->sc_lnum = info->currentline; + int sid = find_script_by_name(source_path); + if (sid > 0) { + xfree(source_path); + } else { + new_script_item(source_path, &sid); + } + current->sc_sid = sid; current->sc_seq = -1; + current->sc_lnum = info->currentline; cleanup: xfree(info); -- cgit From 673d2b52fa4335aa083c52e6686f0728e25b8ebd Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 7 Mar 2023 16:04:57 +0100 Subject: refactor!: rename vim.pretty_print => vim.print Problem: The function name `vim.pretty_print`: 1. is verbose, which partially defeats its purpose as sugar 2. does not draw from existing precedent or any sort of convention (except external projects like penlight or python?), which reduces discoverability, and degrades signaling about best practices. Solution: - Rename to `vim.print`. - Change the behavior so that 1. strings are printed without quotes 2. each arg is printed on its own line 3. tables are indented with 2 instead of 4 spaces - Example: :lua ='a', 'b', 42, {a=3} a b 42 { a = 3 } Comparison of alternatives: - `vim.print`: - pro: consistent with Lua's `print()` - pro: aligns with potential `nvim_print` API function which will replace nvim_echo, nvim_notify, etc. - con: behaves differently than Lua's `print()`, slightly misleading? - `vim.echo`: - pro: `:echo` has similar "pretty print" behavior. - con: inconsistent with Lua idioms. - `vim.p`: - pro: very short, fits with `vim.o`, etc. - con: not as discoverable as "echo" - con: less opportunity for `local p = vim.p` because of potential shadowing. --- src/nvim/lua/executor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 11a02ea402..819e3cccff 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1623,12 +1623,12 @@ void ex_lua(exarg_T *const eap) } // When =expr is used transform it to print(vim.inspect(expr)) if (code[0] == '=') { - len += sizeof("vim.pretty_print()") - sizeof("="); + len += sizeof("vim.print()") - 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, "vim.pretty_print(%s)", code + 1); + vim_snprintf(code_buf, len + 1, "vim.print(%s)", code + 1); xfree(code); code = code_buf; } -- cgit From 07f59467da5a80c7ce46fdc9efcc0e2c18633df1 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 17 Mar 2023 20:09:56 +0800 Subject: refactor(completion): don't add and remove '^' for Lua (#22702) --- src/nvim/lua/executor.c | 6 ------ 1 file changed, 6 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 819e3cccff..3616f1f69f 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1857,12 +1857,6 @@ int nlua_expand_pat(expand_T *xp, char *pat, int *num_results, char ***results) lua_getfield(lstate, -1, "_expand_pat"); luaL_checktype(lstate, -1, LUA_TFUNCTION); - // ex expansion prepends a ^, but don't worry, it is not a regex - if (pat[0] != '^') { - return FAIL; - } - pat++; - // [ vim, vim._expand_pat, buf ] lua_pushlstring(lstate, (const char *)pat, strlen(pat)); -- cgit From a92b38934a2d00c13ee4d1969d994da15e0857ab Mon Sep 17 00:00:00 2001 From: bfredl Date: Mon, 20 Mar 2023 21:11:10 +0100 Subject: feat(lua): allow `:=expr` as a shorter version of `:lua =expr` existing behavior of := and :[range]= are unchanged. `|` is still allowed with this usage. However, :=p and similar are changed in a way which could be construed as a breaking change. Allowing |ex-flags| for := in the first place was a mistake as any form of := DOES NOT MOVE THE CURSOR. So it would print one line number and then print a completely different line contents after that. --- src/nvim/lua/executor.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 3616f1f69f..7e331a097f 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1621,14 +1621,15 @@ void ex_lua(exarg_T *const eap) xfree(code); return; } - // When =expr is used transform it to print(vim.inspect(expr)) - if (code[0] == '=') { - len += sizeof("vim.print()") - sizeof("="); + // 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. char *code_buf = xmallocz(len); - vim_snprintf(code_buf, len + 1, "vim.print(%s)", code + 1); + vim_snprintf(code_buf, len + 1, "vim.print(%s)", code + off); xfree(code); code = code_buf; } -- cgit From 3285cd6eccd9b7f33cc32f992c2607c3fc4ca13f Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 22 Mar 2023 10:09:28 +0000 Subject: refactor: do more in TRY_WRAP --- src/nvim/lua/executor.c | 35 ++++++++++++++++++----------------- src/nvim/lua/stdlib.c | 4 +--- 2 files changed, 19 insertions(+), 20 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 7e331a097f..dfbbfe9ab5 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1157,28 +1157,29 @@ int nlua_call(lua_State *lstate) } } - TRY_WRAP({ - // TODO(bfredl): this should be simplified in error handling refactor - force_abort = false; - suppress_errthrow = false; - did_throw = false; - did_emsg = false; - - try_start(); - typval_T rettv; - funcexe_T funcexe = FUNCEXE_INIT; - funcexe.fe_firstline = curwin->w_cursor.lnum; - funcexe.fe_lastline = curwin->w_cursor.lnum; - funcexe.fe_evaluate = true; + // TODO(bfredl): this should be simplified in error handling refactor + force_abort = false; + suppress_errthrow = false; + did_throw = false; + did_emsg = false; + + typval_T rettv; + funcexe_T funcexe = FUNCEXE_INIT; + funcexe.fe_firstline = curwin->w_cursor.lnum; + funcexe.fe_lastline = curwin->w_cursor.lnum; + funcexe.fe_evaluate = true; + + TRY_WRAP(&err, { // call_func() retval is deceptive, ignore it. Instead we set `msg_list` // (TRY_WRAP) to capture abort-causing non-exception errors. (void)call_func((char *)name, (int)name_len, &rettv, nargs, vim_args, &funcexe); - if (!try_end(&err)) { - nlua_push_typval(lstate, &rettv, false); - } - tv_clear(&rettv); }); + if (!ERROR_SET(&err)) { + nlua_push_typval(lstate, &rettv, false); + } + tv_clear(&rettv); + free_vim_args: while (i > 0) { tv_clear(&vim_args[--i]); diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 852e10c8d8..ee5676e927 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -287,10 +287,8 @@ int nlua_regex(lua_State *lstate) const char *text = luaL_checkstring(lstate, 1); regprog_T *prog = NULL; - TRY_WRAP({ - try_start(); + TRY_WRAP(&err, { prog = vim_regcomp((char *)text, RE_AUTO | RE_MAGIC | RE_STRICT); - try_end(&err); }); if (ERROR_SET(&err)) { -- cgit From d5f6176e6dc4b4e12fc5061ca6e87f4af533e46a Mon Sep 17 00:00:00 2001 From: ii14 <59243201+ii14@users.noreply.github.com> Date: Sat, 1 Apr 2023 02:49:51 +0200 Subject: refactor: add const and remove unnecessary casts (#22841) --- src/nvim/lua/executor.c | 2 +- src/nvim/lua/stdlib.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index dfbbfe9ab5..b5dd7a3e78 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -623,7 +623,7 @@ static bool nlua_init_packages(lua_State *lstate, bool is_standalone) lua_getglobal(lstate, "require"); lua_pushstring(lstate, "vim._init_packages"); if (nlua_pcall(lstate, 1, 0)) { - os_errmsg((char *)lua_tostring(lstate, -1)); + os_errmsg(lua_tostring(lstate, -1)); os_errmsg("\n"); return false; } diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index ee5676e927..20a99b2836 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -288,7 +288,7 @@ int nlua_regex(lua_State *lstate) regprog_T *prog = NULL; TRY_WRAP(&err, { - prog = vim_regcomp((char *)text, RE_AUTO | RE_MAGIC | RE_STRICT); + prog = vim_regcomp(text, RE_AUTO | RE_MAGIC | RE_STRICT); }); if (ERROR_SET(&err)) { -- cgit From 090ade4af6344a7bc4ee56a8052c0739c0428c04 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 4 Apr 2023 12:58:16 +0100 Subject: refactor(treesitter): delegate region calculation to treesitter (#22576) --- src/nvim/lua/treesitter.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 289a0cb9b4..ae3a7b6d75 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -64,6 +64,7 @@ static struct luaL_Reg tree_meta[] = { { "__tostring", tree_tostring }, { "root", tree_root }, { "edit", tree_edit }, + { "included_ranges", tree_get_ranges }, { "copy", tree_copy }, { NULL, NULL } }; @@ -512,6 +513,24 @@ static int tree_edit(lua_State *L) return 0; } +static int tree_get_ranges(lua_State *L) +{ + TSTree **tree = tree_check(L, 1); + if (!(*tree)) { + return 0; + } + + bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); + + uint32_t len; + TSRange *ranges = ts_tree_included_ranges(*tree, &len); + + push_ranges(L, ranges, len, include_bytes); + + xfree(ranges); + return 1; +} + // Use the top of the stack (without popping it) to create a TSRange, it can be // either a lua table or a TSNode static void range_from_lua(lua_State *L, TSRange *range) -- cgit From 9e7426718b678e299f3fd03ef94f81b1e2d01ab0 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 4 Apr 2023 23:59:39 +0100 Subject: feat(vim.diff): allow passing an integer for linematch --- src/nvim/lua/xdiff.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 9a7ae5c146..e0bbdb8942 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -32,7 +32,7 @@ typedef struct { Error *err; mmfile_t *ma; mmfile_t *mb; - bool linematch; + int64_t linematch; bool iwhite; } hunkpriv_t; @@ -128,7 +128,7 @@ static int hunk_locations_cb(long start_a, long count_a, long start_b, long coun { hunkpriv_t *priv = (hunkpriv_t *)cb_data; lua_State *lstate = priv->lstate; - if (priv->linematch) { + if (priv->linematch > 0 && count_a + count_b <= priv->linematch) { get_linematch_results(lstate, priv->ma, priv->mb, start_a, count_a, start_b, count_b, priv->iwhite); } else { @@ -208,7 +208,7 @@ static bool check_xdiff_opt(ObjectType actType, ObjectType expType, const char * } static NluaXdiffMode process_xdl_diff_opts(lua_State *lstate, xdemitconf_t *cfg, xpparam_t *params, - bool *linematch, Error *err) + int64_t *linematch, Error *err) { const DictionaryOf(LuaRef) opts = nlua_pop_Dictionary(lstate, true, err); @@ -265,8 +265,12 @@ static NluaXdiffMode process_xdl_diff_opts(lua_State *lstate, xdemitconf_t *cfg, } cfg->interhunkctxlen = (long)v->data.integer; } else if (strequal("linematch", k.data)) { - *linematch = api_object_to_bool(*v, "linematch", false, err); - if (ERROR_SET(err)) { + if (v->type == kObjectTypeBoolean) { + *linematch = v->data.boolean ? INT64_MAX : 0; + } else if (v->type == kObjectTypeInteger) { + *linematch = v->data.integer; + } else { + api_set_error(err, kErrorTypeValidation, "linematch must be a boolean or integer"); goto exit_1; } } else { @@ -330,7 +334,7 @@ int nlua_xdl_diff(lua_State *lstate) xdemitconf_t cfg; xpparam_t params; xdemitcb_t ecb; - bool linematch = false; + int64_t linematch = 0; CLEAR_FIELD(cfg); CLEAR_FIELD(params); -- cgit From 9408f2dcf7cade2631688300e9b58eed6bc5219a Mon Sep 17 00:00:00 2001 From: ii14 <59243201+ii14@users.noreply.github.com> Date: Fri, 7 Apr 2023 19:40:57 +0200 Subject: refactor: remove redundant const char * casts --- src/nvim/lua/converter.c | 6 ++---- src/nvim/lua/executor.c | 12 ++++++------ 2 files changed, 8 insertions(+), 10 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 6160b84485..9ff03bc67d 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -459,7 +459,7 @@ static bool typval_conv_special = false; TYPVAL_ENCODE_CONV_NUMBER(tv, flt) #define TYPVAL_ENCODE_CONV_STRING(tv, str, len) \ - lua_pushlstring(lstate, (const char *)(str), (len)) + lua_pushlstring(lstate, (str), (len)) #define TYPVAL_ENCODE_CONV_STR_STRING TYPVAL_ENCODE_CONV_STRING @@ -469,9 +469,7 @@ static bool typval_conv_special = false; #define TYPVAL_ENCODE_CONV_BLOB(tv, blob, len) \ do { \ const blob_T *const blob_ = (blob); \ - lua_pushlstring(lstate, \ - blob_ != NULL ? (const char *)blob_->bv_ga.ga_data : "", \ - (size_t)(len)); \ + lua_pushlstring(lstate, blob_ != NULL ? blob_->bv_ga.ga_data : "", (size_t)(len)); \ } while (0) #define TYPVAL_ENCODE_CONV_FUNC_START(tv, fun) \ diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index b5dd7a3e78..82db8445df 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1105,7 +1105,7 @@ static int nlua_debug(lua_State *lstate) tv_clear(&input); return 0; } - if (luaL_loadbuffer(lstate, (const char *)input.vval.v_string, + if (luaL_loadbuffer(lstate, input.vval.v_string, strlen(input.vval.v_string), "=(debug command)")) { nlua_error(lstate, _("E5115: Error while loading debug string: %.*s")); } else if (nlua_pcall(lstate, 0, 0)) { @@ -1652,7 +1652,7 @@ void ex_luado(exarg_T *const eap) emsg(_("cannot save undo information")); return; } - const char *const cmd = (const char *)eap->arg; + const char *const cmd = eap->arg; const size_t cmd_len = strlen(cmd); lua_State *const lstate = global_lstate; @@ -1693,7 +1693,7 @@ void ex_luado(exarg_T *const eap) break; } lua_pushvalue(lstate, -1); - const char *const old_line = (const char *)ml_get_buf(curbuf, l, false); + const char *const old_line = ml_get_buf(curbuf, l, false); // Get length of old_line here as calling Lua code may free it. const size_t old_line_len = strlen(old_line); lua_pushstring(lstate, old_line); @@ -1729,7 +1729,7 @@ void ex_luado(exarg_T *const eap) void ex_luafile(exarg_T *const eap) FUNC_ATTR_NONNULL_ALL { - nlua_exec_file((const char *)eap->arg); + nlua_exec_file(eap->arg); } /// Executes Lua code from a file or "-" (stdin). @@ -1860,7 +1860,7 @@ int nlua_expand_pat(expand_T *xp, char *pat, int *num_results, char ***results) luaL_checktype(lstate, -1, LUA_TFUNCTION); // [ vim, vim._expand_pat, buf ] - lua_pushlstring(lstate, (const char *)pat, strlen(pat)); + lua_pushlstring(lstate, pat, strlen(pat)); if (nlua_pcall(lstate, 1, 2) != 0) { nlua_error(lstate, @@ -2092,7 +2092,7 @@ int nlua_do_ucmd(ucmd_T *cmd, exarg_T *eap, bool preview) lua_setfield(lstate, -2, "line2"); lua_newtable(lstate); // f-args table - lua_pushstring(lstate, (const char *)eap->arg); + lua_pushstring(lstate, eap->arg); lua_pushvalue(lstate, -1); // Reference for potential use on f-args lua_setfield(lstate, -4, "args"); -- cgit From 2d78e656b715119ca11d131a1a932f22f1b4ad36 Mon Sep 17 00:00:00 2001 From: ii14 <59243201+ii14@users.noreply.github.com> Date: Fri, 7 Apr 2023 21:43:00 +0200 Subject: refactor: remove redundant casts --- src/nvim/lua/executor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 82db8445df..3646fd876b 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1172,7 +1172,7 @@ int nlua_call(lua_State *lstate) TRY_WRAP(&err, { // call_func() retval is deceptive, ignore it. Instead we set `msg_list` // (TRY_WRAP) to capture abort-causing non-exception errors. - (void)call_func((char *)name, (int)name_len, &rettv, nargs, vim_args, &funcexe); + (void)call_func(name, (int)name_len, &rettv, nargs, vim_args, &funcexe); }); if (!ERROR_SET(&err)) { -- cgit From 706f871014b46300180156590ff269ee38473989 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 19 Apr 2023 17:04:00 +0100 Subject: build: update uncrustify to 0.76 --- src/nvim/lua/executor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 3646fd876b..13a0c4b0cd 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -377,7 +377,8 @@ static int nlua_schedule(lua_State *const lstate) // Dummy timer callback. Used by f_wait(). static void dummy_timer_due_cb(TimeWatcher *tw, void *data) -{} +{ +} // Dummy timer close callback. Used by f_wait(). static void dummy_timer_close_cb(TimeWatcher *tw, void *data) -- cgit From 3b0df1780e2c8526bda5dead18ee7cc45925caba Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Wed, 26 Apr 2023 23:23:44 +0200 Subject: refactor: uncrustify Notable changes: replace all infinite loops to `while(true)` and remove `int` from `unsigned int`. --- src/nvim/lua/executor.c | 2 +- src/nvim/lua/spell.c | 6 +++--- src/nvim/lua/stdlib.c | 4 ++-- src/nvim/lua/treesitter.c | 2 +- src/nvim/lua/xdiff.c | 8 ++++---- 5 files changed, 11 insertions(+), 11 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 13a0c4b0cd..1c1f68b68d 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1094,7 +1094,7 @@ static int nlua_debug(lua_State *lstate) .v_type = VAR_UNKNOWN, }, }; - for (;;) { + while (true) { lua_settop(lstate, 0); typval_T input; get_user_input(input_args, &input, false, false); diff --git a/src/nvim/lua/spell.c b/src/nvim/lua/spell.c index 78e023c26d..742e8720f9 100644 --- a/src/nvim/lua/spell.c +++ b/src/nvim/lua/spell.c @@ -69,10 +69,10 @@ int nlua_spell_check(lua_State *lstate) lua_pushlstring(lstate, str, len); lua_rawseti(lstate, -2, 1); - result = attr == HLF_SPB ? "bad" : - attr == HLF_SPR ? "rare" : + result = attr == HLF_SPB ? "bad" : + attr == HLF_SPR ? "rare" : attr == HLF_SPL ? "local" : - attr == HLF_SPC ? "caps" : + attr == HLF_SPC ? "caps" : NULL; assert(result != NULL); diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 20a99b2836..f53493e088 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -455,7 +455,7 @@ static int nlua_stricmp(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL int ret = 0; assert(s1[s1_len] == NUL); assert(s2[s2_len] == NUL); - do { + while (true) { nul1 = memchr(s1, NUL, s1_len); nul2 = memchr(s2, NUL, s2_len); ret = STRICMP(s1, s2); @@ -479,7 +479,7 @@ static int nlua_stricmp(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL } else { break; } - } while (true); + } lua_pop(lstate, 2); lua_pushnumber(lstate, (lua_Number)((ret > 0) - (ret < 0))); return 1; diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index ae3a7b6d75..da64685a40 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -454,7 +454,7 @@ static int parser_parse(lua_State *L) // the lua GC. // Old tree is still owned by the lua GC. uint32_t n_ranges = 0; - TSRange *changed = old_tree ? ts_tree_get_changed_ranges(old_tree, new_tree, &n_ranges) : NULL; + TSRange *changed = old_tree ? ts_tree_get_changed_ranges(old_tree, new_tree, &n_ranges) : NULL; push_tree(L, new_tree, false); // [tree] diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index e0bbdb8942..b5575fe202 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -193,10 +193,10 @@ static bool check_xdiff_opt(ObjectType actType, ObjectType expType, const char * { if (actType != expType) { const char *type_str = - expType == kObjectTypeString ? "string" : - expType == kObjectTypeInteger ? "integer" : - expType == kObjectTypeBoolean ? "boolean" : - expType == kObjectTypeLuaRef ? "function" : + expType == kObjectTypeString ? "string" : + expType == kObjectTypeInteger ? "integer" : + expType == kObjectTypeBoolean ? "boolean" : + expType == kObjectTypeLuaRef ? "function" : "NA"; api_set_error(err, kErrorTypeValidation, "%s is not a %s", name, -- cgit From 45bcf8386918bbb475fbe20c48b508aa89ed0624 Mon Sep 17 00:00:00 2001 From: bfredl Date: Thu, 20 Apr 2023 13:19:38 +0200 Subject: refactor(build): include lpeg as a library --- src/nvim/lua/stdlib.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 20a99b2836..c196a89842 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -611,6 +611,19 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread) lua_setfield(lstate, -2, "mpack"); lua_pop(lstate, 3); + // vim.lpeg + int luaopen_lpeg(lua_State *); + luaopen_lpeg(lstate); + lua_pushvalue(lstate, -1); + lua_setfield(lstate, -4, "lpeg"); + + // package.loaded.lpeg = vim.lpeg + lua_getglobal(lstate, "package"); + lua_getfield(lstate, -1, "loaded"); + lua_pushvalue(lstate, -3); + lua_setfield(lstate, -2, "lpeg"); + lua_pop(lstate, 4); + // vim.diff lua_pushcfunction(lstate, &nlua_xdl_diff); lua_setfield(lstate, -2, "diff"); -- cgit From ff34c91194f9ab9d02808f2880029c38a4655eb5 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 17 Apr 2023 17:23:47 +0100 Subject: vim-patch:9.0.1330: handling new value of an option has a long "else if" chain Problem: Handling new value of an option has a long "else if" chain. Solution: Use a function pointer. (Yegappan Lakshmanan, closes vim/vim#12015) https://github.com/vim/vim/commit/af93691b53f38784efce0b93fe7644c44a7e382e --- src/nvim/lua/spell.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/spell.c b/src/nvim/lua/spell.c index 742e8720f9..37f1c5216d 100644 --- a/src/nvim/lua/spell.c +++ b/src/nvim/lua/spell.c @@ -39,7 +39,7 @@ int nlua_spell_check(lua_State *lstate) const int wo_spell_save = curwin->w_p_spell; if (!curwin->w_p_spell) { - did_set_spelllang(curwin); + parse_spelllang(curwin); curwin->w_p_spell = true; } -- cgit From 7b6d041baed712b071acfa8bb71727a5f5e27561 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 29 Apr 2023 09:23:31 +0800 Subject: fix(heredoc): allow missing end marker for scripts Also do not crash when getting heredoc fails. --- src/nvim/lua/executor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 1c1f68b68d..9586b56f3c 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1619,7 +1619,7 @@ void ex_lua(exarg_T *const eap) { size_t len; char *code = script_get(eap, &len); - if (eap->skip) { + if (eap->skip || code == NULL) { xfree(code); return; } -- cgit From af040c3a079f6e25db0ad6b908aa1327f67deb82 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 11 May 2023 11:13:32 +0100 Subject: feat(treesitter): add support for setting query depths --- src/nvim/lua/treesitter.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index da64685a40..0c4700ccab 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -1360,6 +1360,29 @@ static int node_rawquery(lua_State *L) ts_query_cursor_set_point_range(cursor, (TSPoint){ start, 0 }, (TSPoint){ end, 0 }); } + if (lua_gettop(L) >= 6 && !lua_isnil(L, 6)) { + if (!lua_istable(L, 6)) { + return luaL_error(L, "table expected"); + } + lua_pushnil(L); + // stack: [dict, ..., nil] + while (lua_next(L, 6)) { + // stack: [dict, ..., key, value] + if (lua_type(L, -2) == LUA_TSTRING) { + char *k = (char *)lua_tostring(L, -2); + if (strequal("max_start_depth", k)) { + // TODO(lewis6991): remove ifdef when min TS version is 0.20.9 +#ifdef NVIM_TS_HAS_SET_MAX_START_DEPTH + uint32_t max_start_depth = (uint32_t)lua_tointeger(L, -1); + ts_query_cursor_set_max_start_depth(cursor, max_start_depth); +#endif + } + } + lua_pop(L, 1); // pop the value; lua_next will pop the key. + // stack: [dict, ..., key] + } + } + TSLua_cursor *ud = lua_newuserdata(L, sizeof(*ud)); // [udata] ud->cursor = cursor; ud->predicated_match = -1; -- cgit From e124672ce9a81e0ca32e6c30ea3730ad5fe981af Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 11 May 2023 12:08:33 +0100 Subject: fix(treesitter): reset cursor max_start_depth --- src/nvim/lua/treesitter.c | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 0c4700ccab..0c16d09b63 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -1349,6 +1349,11 @@ static int node_rawquery(lua_State *L) } else { cursor = ts_query_cursor_new(); } + +#ifdef NVIM_TS_HAS_SET_MAX_START_DEPTH + // reset the start depth + ts_query_cursor_set_max_start_depth(cursor, 0); +#endif ts_query_cursor_set_match_limit(cursor, 256); ts_query_cursor_exec(cursor, query, node); -- cgit From 6a273af10517d1f7e4ea85635f1d25a9158adeb5 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 13 May 2023 10:40:53 +0800 Subject: refactor: remove typval.h from most header files (#23601) Because typval_defs.h is enough for most of them. --- src/nvim/lua/converter.h | 2 +- src/nvim/lua/executor.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.h b/src/nvim/lua/converter.h index ddc0acfbfa..241dd65cc7 100644 --- a/src/nvim/lua/converter.h +++ b/src/nvim/lua/converter.h @@ -6,7 +6,7 @@ #include #include "nvim/api/private/defs.h" -#include "nvim/eval/typval.h" +#include "nvim/eval/typval_defs.h" #include "nvim/func_attr.h" #ifdef INCLUDE_GENERATED_DECLARATIONS diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index c6747833e5..7e16c0f45c 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -8,7 +8,7 @@ #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" #include "nvim/assert.h" -#include "nvim/eval/typval.h" +#include "nvim/eval/typval_defs.h" #include "nvim/ex_cmds_defs.h" #include "nvim/func_attr.h" #include "nvim/lua/converter.h" -- cgit From e2fdd53d8c015913e8be4ff708fc3488558c8906 Mon Sep 17 00:00:00 2001 From: bfredl Date: Sun, 14 May 2023 18:45:56 +0200 Subject: refactor(map): avoid duplicated khash_t types for values This reduces the total number of khash_t instantiations from 22 to 8. Make the khash internal functions take the size of values as a runtime parameter. This is abstracted with typesafe Map containers which are still specialized for both key, value type. Introduce `Set(key)` type for when there is no value. Refactor shada.c to use Map/Set instead of khash directly. This requires `map_ref` operation to be more flexible. Return pointers to both key and value, plus an indicator for new_item. As a bonus, `map_key` is now redundant. Instead of Map(cstr_t, FileMarks), use a pointer map as the FileMarks struct is humongous. Make `event_strings` actually work like an intern pool instead of wtf it was doing before. --- src/nvim/lua/executor.c | 6 +++--- src/nvim/lua/executor.h | 2 +- src/nvim/lua/treesitter.c | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 9586b56f3c..1d11379956 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -910,7 +910,7 @@ static void nlua_common_free_all_mem(lua_State *lstate) if (nlua_track_refs) { // in case there are leaked luarefs, leak the associated memory // to get LeakSanitizer stacktraces on exit - pmap_destroy(handle_T)(&ref_state->ref_markers); + map_destroy(int, &ref_state->ref_markers); } #endif @@ -1285,7 +1285,7 @@ LuaRef nlua_ref(lua_State *lstate, nlua_ref_state_t *ref_state, int index) #ifdef NLUA_TRACK_REFS if (nlua_track_refs) { // dummy allocation to make LeakSanitizer track our luarefs - pmap_put(handle_T)(&ref_state->ref_markers, ref, xmalloc(3)); + pmap_put(int)(&ref_state->ref_markers, ref, xmalloc(3)); } #endif } @@ -1305,7 +1305,7 @@ void nlua_unref(lua_State *lstate, nlua_ref_state_t *ref_state, LuaRef ref) #ifdef NLUA_TRACK_REFS // NB: don't remove entry from map to track double-unref if (nlua_track_refs) { - xfree(pmap_get(handle_T)(&ref_state->ref_markers, ref)); + xfree(pmap_get(int)(&ref_state->ref_markers, ref)); } #endif luaL_unref(lstate, LUA_REGISTRYINDEX, ref); diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index 7e16c0f45c..f340d9d0d8 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -24,7 +24,7 @@ typedef struct { LuaRef empty_dict_ref; int ref_count; #if __has_feature(address_sanitizer) - PMap(handle_T) ref_markers; + PMap(int) ref_markers; #endif } nlua_ref_state_t; diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 0c16d09b63..dae1365272 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -231,9 +231,9 @@ int tslua_remove_lang(lua_State *L) const char *lang_name = luaL_checkstring(L, 1); bool present = pmap_has(cstr_t)(&langs, lang_name); if (present) { - char *key = (char *)pmap_key(cstr_t)(&langs, lang_name); - pmap_del(cstr_t)(&langs, lang_name); - xfree(key); + cstr_t key; + pmap_del(cstr_t)(&langs, lang_name, &key); + xfree((void *)key); } lua_pushboolean(L, present); return 1; -- cgit From 189fb6203262340e7a59e782be970bcd8ae28e61 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 17 May 2023 11:42:18 +0100 Subject: feat(treesitter): improved logging (#23638) - Add bindings to Treesitter ts_parser_set_logger and ts_parser_logger - Add logfile with path STDPATH('log')/treesitter.c - Rework existing LanguageTree loggin to use logfile - Begin implementing log levels for vim.g.__ts_debug --- src/nvim/lua/treesitter.c | 89 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 0c16d09b63..96e104cd48 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -20,6 +20,7 @@ #include "nvim/api/private/helpers.h" #include "nvim/buffer_defs.h" #include "nvim/globals.h" +#include "nvim/lua/executor.h" #include "nvim/lua/treesitter.h" #include "nvim/macros.h" #include "nvim/map.h" @@ -43,6 +44,13 @@ typedef struct { int max_match_id; } TSLua_cursor; +typedef struct { + LuaRef cb; + lua_State *lstate; + bool lex; + bool parse; +} TSLuaLoggerOpts; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/treesitter.c.generated.h" #endif @@ -56,6 +64,8 @@ static struct luaL_Reg parser_meta[] = { { "included_ranges", parser_get_ranges }, { "set_timeout", parser_set_timeout }, { "timeout", parser_get_timeout }, + { "_set_logger", parser_set_logger }, + { "_logger", parser_get_logger }, { NULL, NULL } }; @@ -322,6 +332,12 @@ static int parser_gc(lua_State *L) return 0; } + TSLogger logger = ts_parser_logger(*p); + if (logger.log) { + TSLuaLoggerOpts *opts = (TSLuaLoggerOpts *)logger.payload; + xfree(opts); + } + ts_parser_delete(*p); return 0; } @@ -669,9 +685,82 @@ static int parser_get_timeout(lua_State *L) } lua_pushinteger(L, (long)ts_parser_timeout_micros(*p)); + return 1; +} + +static void logger_cb(void *payload, TSLogType logtype, const char *s) +{ + TSLuaLoggerOpts *opts = (TSLuaLoggerOpts *)payload; + if ((!opts->lex && logtype == TSLogTypeLex) + || (!opts->parse && logtype == TSLogTypeParse)) { + return; + } + + lua_State *lstate = opts->lstate; + + nlua_pushref(lstate, opts->cb); + lua_pushstring(lstate, logtype == TSLogTypeParse ? "parse" : "lex"); + lua_pushstring(lstate, s); + if (lua_pcall(lstate, 2, 0, 0)) { + luaL_error(lstate, "Error executing treesitter logger callback"); + } +} + +static int parser_set_logger(lua_State *L) +{ + TSParser **p = parser_check(L, 1); + if (!p) { + return 0; + } + + if (!lua_isboolean(L, 2)) { + return luaL_argerror(L, 2, "boolean expected"); + } + + if (!lua_isboolean(L, 3)) { + return luaL_argerror(L, 3, "boolean expected"); + } + + if (!lua_isfunction(L, 4)) { + return luaL_argerror(L, 4, "function expected"); + } + + TSLuaLoggerOpts *opts = xmalloc(sizeof(TSLuaLoggerOpts)); + + *opts = (TSLuaLoggerOpts){ + .lex = lua_toboolean(L, 2), + .parse = lua_toboolean(L, 3), + .cb = nlua_ref_global(L, 4), + .lstate = L + }; + + TSLogger logger = { + .payload = (void *)opts, + .log = logger_cb + }; + + ts_parser_set_logger(*p, logger); return 0; } +static int parser_get_logger(lua_State *L) +{ + TSParser **p = parser_check(L, 1); + if (!p) { + return 0; + } + + TSLogger logger = ts_parser_logger(*p); + if (logger.log) { + TSLuaLoggerOpts *opts = (TSLuaLoggerOpts *)logger.payload; + nlua_pushref(L, opts->cb); + } else { + lua_pushnil(L); + } + + return 1; +} + // Tree methods /// push tree interface on lua stack. -- cgit From 01ea42c32afe8d235d2110a5fcf12a353a7d2a71 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 22 May 2023 09:24:36 +0100 Subject: refactor(vim.secure): move to lua/secure.c --- src/nvim/lua/executor.c | 89 ++++--------------------------------- src/nvim/lua/secure.c | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ src/nvim/lua/secure.h | 12 +++++ 3 files changed, 136 insertions(+), 80 deletions(-) create mode 100644 src/nvim/lua/secure.c create mode 100644 src/nvim/lua/secure.h (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 1d11379956..0069ba8ceb 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -109,11 +109,16 @@ typedef enum luv_err_type { kThreadCallback, } luv_err_t; +lua_State *get_global_lstate(void) +{ + return global_lstate; +} + /// Convert lua error into a Vim error message /// /// @param lstate Lua interpreter state. /// @param[in] msg Message base, must contain one `%s`. -static void nlua_error(lua_State *const lstate, const char *const msg) +void nlua_error(lua_State *const lstate, const char *const msg) FUNC_ATTR_NONNULL_ALL { size_t len; @@ -150,7 +155,7 @@ static void nlua_error(lua_State *const lstate, const char *const msg) /// @param lstate Lua interpreter state /// @param[in] nargs Number of arguments expected by the function being called. /// @param[in] nresults Number of results the function returns. -static int nlua_pcall(lua_State *lstate, int nargs, int nresults) +int nlua_pcall(lua_State *lstate, int nargs, int nresults) { lua_getglobal(lstate, "debug"); lua_getfield(lstate, -1, "traceback"); @@ -836,7 +841,7 @@ void nlua_run_script(char **argv, int argc, int lua_arg0) exit(lua_ok ? 0 : 1); } -lua_State *nlua_init_state(bool thread) +static lua_State *nlua_init_state(bool thread) { // If it is called from the main thread, it will attempt to rebuild the cache. const uv_thread_t self = uv_thread_self(); @@ -916,6 +921,7 @@ static void nlua_common_free_all_mem(lua_State *lstate) lua_close(lstate); } + static void nlua_print_event(void **argv) { char *str = argv[0]; @@ -2275,80 +2281,3 @@ plain: kv_printf(str, "", ref); return str.items; } - -char *nlua_read_secure(const char *path) -{ - lua_State *const lstate = global_lstate; - const int top = lua_gettop(lstate); - - lua_getglobal(lstate, "vim"); - lua_getfield(lstate, -1, "secure"); - lua_getfield(lstate, -1, "read"); - lua_pushstring(lstate, path); - if (nlua_pcall(lstate, 1, 1)) { - nlua_error(lstate, _("Error executing vim.secure.read: %.*s")); - lua_settop(lstate, top); - return NULL; - } - - size_t len = 0; - const char *contents = lua_tolstring(lstate, -1, &len); - char *buf = NULL; - if (contents != NULL) { - // Add one to include trailing null byte - buf = xcalloc(len + 1, sizeof(char)); - memcpy(buf, contents, len + 1); - } - - lua_settop(lstate, top); - return buf; -} - -bool nlua_trust(const char *action, const char *path) -{ - lua_State *const lstate = global_lstate; - const int top = lua_gettop(lstate); - - lua_getglobal(lstate, "vim"); - lua_getfield(lstate, -1, "secure"); - lua_getfield(lstate, -1, "trust"); - - lua_newtable(lstate); - lua_pushstring(lstate, "action"); - lua_pushstring(lstate, action); - lua_settable(lstate, -3); - if (path == NULL) { - lua_pushstring(lstate, "bufnr"); - lua_pushnumber(lstate, 0); - lua_settable(lstate, -3); - } else { - lua_pushstring(lstate, "path"); - lua_pushstring(lstate, path); - lua_settable(lstate, -3); - } - - if (nlua_pcall(lstate, 1, 2)) { - nlua_error(lstate, _("Error executing vim.secure.trust: %.*s")); - lua_settop(lstate, top); - return false; - } - - bool success = lua_toboolean(lstate, -2); - const char *msg = lua_tostring(lstate, -1); - if (msg != NULL) { - if (success) { - if (strcmp(action, "allow") == 0) { - smsg("Allowed \"%s\" in trust database.", msg); - } else if (strcmp(action, "deny") == 0) { - smsg("Denied \"%s\" in trust database.", msg); - } else if (strcmp(action, "remove") == 0) { - smsg("Removed \"%s\" from trust database.", msg); - } - } else { - semsg(e_trustfile, msg); - } - } - - lua_settop(lstate, top); - return success; -} diff --git a/src/nvim/lua/secure.c b/src/nvim/lua/secure.c new file mode 100644 index 0000000000..59fdd6d819 --- /dev/null +++ b/src/nvim/lua/secure.c @@ -0,0 +1,115 @@ +#include +#include +#include + +#include "nvim/charset.h" +#include "nvim/lua/executor.h" +#include "nvim/lua/secure.h" +#include "nvim/message.h" + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "lua/secure.c.generated.h" +#endif + +char *nlua_read_secure(const char *path) +{ + lua_State *const lstate = get_global_lstate(); + const int top = lua_gettop(lstate); + + lua_getglobal(lstate, "vim"); + lua_getfield(lstate, -1, "secure"); + lua_getfield(lstate, -1, "read"); + lua_pushstring(lstate, path); + if (nlua_pcall(lstate, 1, 1)) { + nlua_error(lstate, _("Error executing vim.secure.read: %.*s")); + lua_settop(lstate, top); + return NULL; + } + + size_t len = 0; + const char *contents = lua_tolstring(lstate, -1, &len); + char *buf = NULL; + if (contents != NULL) { + // Add one to include trailing null byte + buf = xcalloc(len + 1, sizeof(char)); + memcpy(buf, contents, len + 1); + } + + lua_settop(lstate, top); + return buf; +} + +static bool nlua_trust(const char *action, const char *path) +{ + lua_State *const lstate = get_global_lstate(); + const int top = lua_gettop(lstate); + + lua_getglobal(lstate, "vim"); + lua_getfield(lstate, -1, "secure"); + lua_getfield(lstate, -1, "trust"); + + lua_newtable(lstate); + lua_pushstring(lstate, "action"); + lua_pushstring(lstate, action); + lua_settable(lstate, -3); + if (path == NULL) { + lua_pushstring(lstate, "bufnr"); + lua_pushnumber(lstate, 0); + lua_settable(lstate, -3); + } else { + lua_pushstring(lstate, "path"); + lua_pushstring(lstate, path); + lua_settable(lstate, -3); + } + + if (nlua_pcall(lstate, 1, 2)) { + nlua_error(lstate, _("Error executing vim.secure.trust: %.*s")); + lua_settop(lstate, top); + return false; + } + + bool success = lua_toboolean(lstate, -2); + const char *msg = lua_tostring(lstate, -1); + if (msg != NULL) { + if (success) { + if (strcmp(action, "allow") == 0) { + smsg("Allowed \"%s\" in trust database.", msg); + } else if (strcmp(action, "deny") == 0) { + smsg("Denied \"%s\" in trust database.", msg); + } else if (strcmp(action, "remove") == 0) { + smsg("Removed \"%s\" from trust database.", msg); + } + } else { + semsg(e_trustfile, msg); + } + } + + lua_settop(lstate, top); + return success; +} + +void ex_trust(exarg_T *eap) +{ + const char *const p = skiptowhite(eap->arg); + char *arg1 = xmemdupz(eap->arg, (size_t)(p - eap->arg)); + const char *action = "allow"; + const char *path = skipwhite(p); + + if (strcmp(arg1, "++deny") == 0) { + action = "deny"; + } else if (strcmp(arg1, "++remove") == 0) { + action = "remove"; + } else if (*arg1 != '\0') { + semsg(e_invarg2, arg1); + goto theend; + } + + if (path[0] == '\0') { + path = NULL; + } + + nlua_trust(action, path); + +theend: + xfree(arg1); +} diff --git a/src/nvim/lua/secure.h b/src/nvim/lua/secure.h new file mode 100644 index 0000000000..87c468538e --- /dev/null +++ b/src/nvim/lua/secure.h @@ -0,0 +1,12 @@ +#ifndef NVIM_LUA_SECURE_H +#define NVIM_LUA_SECURE_H + +#include + +#include "nvim/ex_cmds_defs.h" + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "lua/secure.h.generated.h" +#endif + +#endif // NVIM_LUA_SECURE_H -- cgit From 677e02be4e1255435a39d25a4b12ecbed4fff37b Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 23 May 2023 18:11:03 +0800 Subject: refactor: fix clang/PVS warnings (#23731) --- src/nvim/lua/executor.c | 2 +- src/nvim/lua/secure.c | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 0069ba8ceb..8c1d8addcd 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -341,7 +341,7 @@ static int nlua_init_argv(lua_State *const L, char **argv, int argc, int lua_arg lua_pushstring(L, argv[lua_arg0 - 1]); lua_rawseti(L, -2, 0); // _G.arg[0] = "foo.lua" - for (; lua_arg0 >= 0 && i + lua_arg0 < argc; i++) { + for (; i + lua_arg0 < argc; i++) { lua_pushstring(L, argv[i + lua_arg0]); lua_rawseti(L, -2, i + 1); // _G.arg[i+1] = "--foo" } diff --git a/src/nvim/lua/secure.c b/src/nvim/lua/secure.c index 59fdd6d819..30d5a95fc0 100644 --- a/src/nvim/lua/secure.c +++ b/src/nvim/lua/secure.c @@ -1,3 +1,6 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check +// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + #include #include #include -- cgit From 2db719f6c2b677fcbc197b02fe52764a851523b2 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sat, 3 Jun 2023 11:06:00 +0100 Subject: feat(lua): rename vim.loop -> vim.uv (#22846) --- src/nvim/lua/executor.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 8c1d8addcd..36dcb6efcf 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -565,7 +565,7 @@ static void nlua_common_vim_init(lua_State *lstate, bool is_thread, bool is_stan lua_setfield(lstate, LUA_REGISTRYINDEX, "mpack.empty_dict"); lua_setfield(lstate, -2, "_empty_dict_mt"); - // vim.loop + // vim.uv if (is_standalone) { // do nothing, use libluv like in a standalone interpreter } else if (is_thread) { @@ -578,9 +578,9 @@ static void nlua_common_vim_init(lua_State *lstate, bool is_thread, bool is_stan } luaopen_luv(lstate); lua_pushvalue(lstate, -1); - lua_setfield(lstate, -3, "loop"); + lua_setfield(lstate, -3, "uv"); - // package.loaded.luv = vim.loop + // package.loaded.luv = vim.uv // otherwise luv will be reinitialized when require'luv' lua_getglobal(lstate, "package"); lua_getfield(lstate, -1, "loaded"); -- cgit From c1ee187f82141d778335955c9603c69e6f4785d7 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Mon, 5 Jun 2023 19:08:01 +0100 Subject: fix: vim.loop in luv threads (#23924) Fixes #23914 --- src/nvim/lua/executor.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 36dcb6efcf..35540de99e 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -580,6 +580,9 @@ static void nlua_common_vim_init(lua_State *lstate, bool is_thread, bool is_stan lua_pushvalue(lstate, -1); lua_setfield(lstate, -3, "uv"); + lua_pushvalue(lstate, -1); + lua_setfield(lstate, -3, "loop"); // deprecated + // package.loaded.luv = vim.uv // otherwise luv will be reinitialized when require'luv' lua_getglobal(lstate, "package"); -- cgit From 2f17ef1fc4b96cf1106fd95ba090d34a2e4b977b Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Thu, 22 Jun 2023 04:09:14 -0700 Subject: fix(messages): use "Vimscript" instead of "VimL" #24111 followup to #24109 fix #16150 --- src/nvim/lua/converter.c | 9 ++++----- src/nvim/lua/executor.c | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 9ff03bc67d..5b3b4dbe08 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -183,7 +183,7 @@ typedef struct { int idx; ///< Container index (used to detect self-referencing structures). } TVPopStackItem; -/// Convert lua object to VimL typval_T +/// Convert lua object to Vimscript typval_T /// /// Should pop exactly one value from lua stack. /// @@ -601,7 +601,7 @@ static bool typval_conv_special = false; #undef TYPVAL_ENCODE_CONV_RECURSE #undef TYPVAL_ENCODE_ALLOW_SPECIALS -/// Convert VimL typval_T to lua value +/// Convert Vimscript typval_T to lua value /// /// Should leave single value in lua stack. May only fail if lua failed to grow /// stack. @@ -628,8 +628,7 @@ bool nlua_push_typval(lua_State *lstate, typval_T *const tv, bool special) /// Push value which is a type index /// -/// Used for all “typed” tables: i.e. for all tables which represent VimL -/// values. +/// Used for all “typed” tables: i.e. for all tables which represent Vimscript values. static inline void nlua_push_type_idx(lua_State *lstate) FUNC_ATTR_NONNULL_ALL { @@ -657,7 +656,7 @@ static inline void nlua_push_type(lua_State *lstate, ObjectType type) lua_pushnumber(lstate, (lua_Number)type); } -/// Create lua table which has an entry that determines its VimL type +/// Create Lua table which has an entry that determines its Vimscript type /// /// @param[out] lstate Lua state. /// @param[in] narr Number of “array” entries to be populated later. diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 35540de99e..ed84e2a601 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1487,7 +1487,7 @@ int nlua_source_using_linegetter(LineGetter fgetline, void *cookie, char *name) /// Call a LuaCallable given some typvals /// -/// Used to call any lua callable passed from Lua into VimL +/// Used to call any Lua callable passed from Lua into Vimscript. /// /// @param[in] lstate Lua State /// @param[in] lua_cb Lua Callable @@ -1622,7 +1622,7 @@ bool nlua_is_deferred_safe(void) /// /// Used for :lua. /// -/// @param eap VimL command being run. +/// @param eap Vimscript command being run. void ex_lua(exarg_T *const eap) FUNC_ATTR_NONNULL_ALL { @@ -1654,7 +1654,7 @@ void ex_lua(exarg_T *const eap) /// /// Used for :luado. /// -/// @param eap VimL command being run. +/// @param eap Vimscript command being run. void ex_luado(exarg_T *const eap) FUNC_ATTR_NONNULL_ALL { @@ -1735,7 +1735,7 @@ void ex_luado(exarg_T *const eap) /// /// Used for :luafile. /// -/// @param eap VimL command being run. +/// @param eap Vimscript command being run. void ex_luafile(exarg_T *const eap) FUNC_ATTR_NONNULL_ALL { -- cgit From 92760a7f42a95bb252966c2a38423e5bc9d57cc7 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 4 Jul 2023 07:19:02 +0800 Subject: fix(api, lua): make blank lines in a message work properly (#24244) --- src/nvim/lua/executor.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index ed84e2a601..ec069131e5 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -954,6 +954,7 @@ static void nlua_print_event(void **argv) break; } msg(str + start); + msg_didout = true; // Make blank lines work properly } if (len && str[len - 1] == NUL) { // Last was newline msg(""); -- cgit From d0b612f360125785eb95afaa51620c5c7695e381 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sun, 16 Jul 2023 09:27:39 +0100 Subject: refactor: rename _meta.lua to _options.lua --- src/nvim/lua/executor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index ec069131e5..09392b4f60 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -2036,9 +2036,9 @@ void nlua_set_sctx(sctx_T *current) lua_Debug *info = (lua_Debug *)xmalloc(sizeof(lua_Debug)); // Files where internal wrappers are defined so we can ignore them - // like vim.o/opt etc are defined in _meta.lua + // like vim.o/opt etc are defined in _options.lua char *ignorelist[] = { - "vim/_meta.lua", + "vim/_options.lua", "vim/keymap.lua", }; int ignorelist_size = sizeof(ignorelist) / sizeof(ignorelist[0]); -- cgit From ca9f4a7cb1fea1ef1f22c011679fd8afa0a5d161 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 21 Jul 2023 16:30:05 +0800 Subject: docs: also change "vimL" and "viml" to "Vimscript" (#24414) --- src/nvim/lua/executor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 09392b4f60..9215926434 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1139,7 +1139,7 @@ static bool viml_func_is_fast(const char *name) if (fdef) { return fdef->fast; } - // Not a vimL function + // Not a Vimscript function return false; } @@ -1149,7 +1149,7 @@ int nlua_call(lua_State *lstate) size_t name_len; const char *name = luaL_checklstring(lstate, 1, &name_len); if (!nlua_is_deferred_safe() && !viml_func_is_fast(name)) { - return luaL_error(lstate, e_luv_api_disabled, "vimL function"); + return luaL_error(lstate, e_luv_api_disabled, "Vimscript function"); } int nargs = lua_gettop(lstate) - 1; -- cgit From 7bc93e0e2f246dd78026a3472d929a0fe450f70d Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 1 Aug 2023 14:01:19 +0200 Subject: refactor(api): use typed keysets Initially this is just for geting rid of boilerplate, but eventually the types could get exposed as metadata --- src/nvim/lua/converter.c | 73 ++++++++++++++++++++++++++++++++---------------- src/nvim/lua/converter.h | 4 +++ 2 files changed, 53 insertions(+), 24 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 5b3b4dbe08..07c452deed 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -871,7 +871,8 @@ static inline LuaTableProps nlua_check_type(lua_State *const lstate, Error *cons { if (lua_type(lstate, -1) != LUA_TTABLE) { if (err) { - api_set_error(err, kErrorTypeValidation, "Expected lua table"); + api_set_error(err, kErrorTypeValidation, "Expected lua %s", + (type == kObjectTypeFloat) ? "number" : "table"); } return (LuaTableProps) { .type = kObjectTypeNil }; } @@ -884,7 +885,7 @@ static inline LuaTableProps nlua_check_type(lua_State *const lstate, Error *cons if (table_props.type != type) { if (err) { - api_set_error(err, kErrorTypeValidation, "Unexpected type"); + api_set_error(err, kErrorTypeValidation, "Expected %s-like lua table", api_typename(type)); } } @@ -1224,26 +1225,19 @@ LuaRef nlua_pop_LuaRef(lua_State *const lstate, Error *err) return rv; } -#define GENERATE_INDEX_FUNCTION(type) \ - type nlua_pop_##type(lua_State *lstate, Error *err) \ - FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT \ - { \ - type ret; \ - if (lua_type(lstate, -1) != LUA_TNUMBER) { \ - api_set_error(err, kErrorTypeValidation, "Expected Lua number"); \ - ret = (type) - 1; \ - } else { \ - ret = (type)lua_tonumber(lstate, -1); \ - } \ - lua_pop(lstate, 1); \ - return ret; \ +handle_T nlua_pop_handle(lua_State *lstate, Error *err) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT +{ + handle_T ret; + if (lua_type(lstate, -1) != LUA_TNUMBER) { + api_set_error(err, kErrorTypeValidation, "Expected Lua number"); + ret = (handle_T) - 1; + } else { + ret = (handle_T)lua_tonumber(lstate, -1); } - -GENERATE_INDEX_FUNCTION(Buffer) -GENERATE_INDEX_FUNCTION(Window) -GENERATE_INDEX_FUNCTION(Tabpage) - -#undef GENERATE_INDEX_FUNCTION + lua_pop(lstate, 1); + return ret; +} /// Record some auxiliary values in vim module /// @@ -1292,7 +1286,8 @@ void nlua_init_types(lua_State *const lstate) lua_rawset(lstate, -3); } -void nlua_pop_keydict(lua_State *L, void *retval, field_hash hashy, Error *err) +// lua specific variant of api_dict_to_keydict +void nlua_pop_keydict(lua_State *L, void *retval, FieldHashfn hashy, Error *err) { if (!lua_istable(L, -1)) { api_set_error(err, kErrorTypeValidation, "Expected lua table"); @@ -1305,14 +1300,44 @@ void nlua_pop_keydict(lua_State *L, void *retval, field_hash hashy, Error *err) // [dict, key, value] size_t len; const char *s = lua_tolstring(L, -2, &len); - Object *field = hashy(retval, s, len); + KeySetLink *field = hashy(s, len); if (!field) { api_set_error(err, kErrorTypeValidation, "invalid key: %.*s", (int)len, s); lua_pop(L, 3); // [] return; } - *field = nlua_pop_Object(L, true, err); + if (field->opt_index >= 0) { + OptKeySet *ks = (OptKeySet *)retval; + ks->is_set_ |= (1ULL << field->opt_index); + } + char *mem = ((char *)retval + field->ptr_off); + + if (field->type == kObjectTypeNil) { + *(Object *)mem = nlua_pop_Object(L, true, err); + } else if (field->type == kObjectTypeInteger) { + *(Integer *)mem = nlua_pop_Integer(L, err); + } else if (field->type == kObjectTypeBoolean) { + *(Boolean *)mem = nlua_pop_Boolean(L, err); + } else if (field->type == kObjectTypeString) { + *(String *)mem = nlua_pop_String(L, err); + } else if (field->type == kObjectTypeFloat) { + *(Float *)mem = nlua_pop_Float(L, err); + } else if (field->type == kObjectTypeBuffer || field->type == kObjectTypeWindow + || field->type == kObjectTypeTabpage) { + *(handle_T *)mem = nlua_pop_handle(L, err); + } else if (field->type == kObjectTypeArray) { + *(Array *)mem = nlua_pop_Array(L, err); + } else if (field->type == kObjectTypeDictionary) { + *(Dictionary *)mem = nlua_pop_Dictionary(L, false, err); + } else if (field->type == kObjectTypeLuaRef) { + *(LuaRef *)mem = nlua_pop_LuaRef(L, err); + } else { + abort(); + } + if (ERROR_SET(err)) { + break; + } } // [dict] lua_pop(L, 1); diff --git a/src/nvim/lua/converter.h b/src/nvim/lua/converter.h index 241dd65cc7..d1d6b9d62a 100644 --- a/src/nvim/lua/converter.h +++ b/src/nvim/lua/converter.h @@ -9,6 +9,10 @@ #include "nvim/eval/typval_defs.h" #include "nvim/func_attr.h" +#define nlua_pop_Buffer nlua_pop_handle +#define nlua_pop_Window nlua_pop_handle +#define nlua_pop_Tabpage nlua_pop_handle + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/converter.h.generated.h" #endif -- cgit From 6c0812d92e0c7937b913175b8856064cb45f55c2 Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 1 Aug 2023 23:35:34 +0200 Subject: feat(lua): specific error messages for type checking `opts` params --- src/nvim/lua/converter.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 07c452deed..2df6458198 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -813,7 +813,7 @@ String nlua_pop_String(lua_State *lstate, Error *err) { if (lua_type(lstate, -1) != LUA_TSTRING) { lua_pop(lstate, 1); - api_set_error(err, kErrorTypeValidation, "Expected lua string"); + api_set_error(err, kErrorTypeValidation, "Expected Lua string"); return (String) { .size = 0, .data = NULL }; } String ret; @@ -834,7 +834,7 @@ Integer nlua_pop_Integer(lua_State *lstate, Error *err) { if (lua_type(lstate, -1) != LUA_TNUMBER) { lua_pop(lstate, 1); - api_set_error(err, kErrorTypeValidation, "Expected lua number"); + api_set_error(err, kErrorTypeValidation, "Expected Lua number"); return 0; } const lua_Number n = lua_tonumber(lstate, -1); @@ -871,7 +871,7 @@ static inline LuaTableProps nlua_check_type(lua_State *const lstate, Error *cons { if (lua_type(lstate, -1) != LUA_TTABLE) { if (err) { - api_set_error(err, kErrorTypeValidation, "Expected lua %s", + api_set_error(err, kErrorTypeValidation, "Expected Lua %s", (type == kObjectTypeFloat) ? "number" : "table"); } return (LuaTableProps) { .type = kObjectTypeNil }; @@ -885,7 +885,7 @@ static inline LuaTableProps nlua_check_type(lua_State *const lstate, Error *cons if (table_props.type != type) { if (err) { - api_set_error(err, kErrorTypeValidation, "Expected %s-like lua table", api_typename(type)); + api_set_error(err, kErrorTypeValidation, "Expected %s-like Lua table", api_typename(type)); } } @@ -1169,7 +1169,7 @@ Object nlua_pop_Object(lua_State *const lstate, bool ref, Error *const err) break; case kObjectTypeNil: api_set_error(err, kErrorTypeValidation, - "Cannot convert given lua table"); + "Cannot convert given Lua table"); break; default: abort(); @@ -1287,10 +1287,10 @@ void nlua_init_types(lua_State *const lstate) } // lua specific variant of api_dict_to_keydict -void nlua_pop_keydict(lua_State *L, void *retval, FieldHashfn hashy, Error *err) +void nlua_pop_keydict(lua_State *L, void *retval, FieldHashfn hashy, char **err_opt, Error *err) { if (!lua_istable(L, -1)) { - api_set_error(err, kErrorTypeValidation, "Expected lua table"); + api_set_error(err, kErrorTypeValidation, "Expected Lua table"); lua_pop(L, -1); return; } @@ -1336,6 +1336,7 @@ void nlua_pop_keydict(lua_State *L, void *retval, FieldHashfn hashy, Error *err) abort(); } if (ERROR_SET(err)) { + *err_opt = field->str; break; } } -- cgit From 2e824e89c13ea720308517b6f8f49cdd6613ae91 Mon Sep 17 00:00:00 2001 From: Christian Clason Date: Tue, 8 Aug 2023 14:57:06 +0200 Subject: build(deps): bump tree-sitter to HEAD - 0a1c4d846 (#24607) adapt to breaking change in `ts_query_cursor_set_max_start_depth` https://github.com/tree-sitter/tree-sitter/pull/2278 --- src/nvim/lua/treesitter.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index a9e7838980..66a75f8d40 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -1441,7 +1441,7 @@ static int node_rawquery(lua_State *L) #ifdef NVIM_TS_HAS_SET_MAX_START_DEPTH // reset the start depth - ts_query_cursor_set_max_start_depth(cursor, 0); + ts_query_cursor_set_max_start_depth(cursor, UINT32_MAX); #endif ts_query_cursor_set_match_limit(cursor, 256); ts_query_cursor_exec(cursor, query, node); -- cgit From dbcba26bf1e4ec717dc488b73351f8a9bb93ff26 Mon Sep 17 00:00:00 2001 From: bfredl Date: Wed, 9 Aug 2023 20:25:16 +0200 Subject: fix(api): revert unintended change of optional bool params Currently (as of nvim 0.9), the behavior of boolean params in vim.api lua wrappers is inconsistent for optional parameters (part of an `opts` dict) compared to positional parameters. This was inadvertently changed in #24524 . While cleaning up this inconsistency is something we might want eventually, it needs to be discussed separately and the impact of existing code considered. --- src/nvim/lua/converter.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 2df6458198..385ac11546 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -849,6 +849,9 @@ Integer nlua_pop_Integer(lua_State *lstate, Error *err) /// Convert lua value to boolean /// +/// Despite the name of the function, this uses lua semantics for booleans. +/// thus `err` is never set as any lua value can be co-erced into a lua bool +/// /// Always pops one value from the stack. Boolean nlua_pop_Boolean(lua_State *lstate, Error *err) FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT @@ -858,6 +861,36 @@ Boolean nlua_pop_Boolean(lua_State *lstate, Error *err) return ret; } +/// Convert lua value to boolean +/// +/// This follows API conventions for a Boolean value, compare api_object_to_bool +/// +/// Always pops one value from the stack. +Boolean nlua_pop_Boolean_strict(lua_State *lstate, Error *err) + FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT +{ + Boolean ret = false; + switch (lua_type(lstate, -1)) { + case LUA_TBOOLEAN: + ret = lua_toboolean(lstate, -1); + break; + + case LUA_TNUMBER: + ret = (lua_tonumber(lstate, -1) != 0); + break; + + case LUA_TNIL: + ret = false; + break; + + default: + api_set_error(err, kErrorTypeValidation, "not a boolean"); + } + + lua_pop(lstate, 1); + return ret; +} + /// Check whether typed table on top of the stack has given type /// /// @param[in] lstate Lua state. @@ -1318,7 +1351,7 @@ void nlua_pop_keydict(lua_State *L, void *retval, FieldHashfn hashy, char **err_ } else if (field->type == kObjectTypeInteger) { *(Integer *)mem = nlua_pop_Integer(L, err); } else if (field->type == kObjectTypeBoolean) { - *(Boolean *)mem = nlua_pop_Boolean(L, err); + *(Boolean *)mem = nlua_pop_Boolean_strict(L, err); } else if (field->type == kObjectTypeString) { *(String *)mem = nlua_pop_String(L, err); } else if (field->type == kObjectTypeFloat) { -- cgit From 8179d68dc1a90f47bfb307d73e71adc98883ae00 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sun, 13 Aug 2023 08:03:56 +0100 Subject: fix(treesitter): logger memory leak --- src/nvim/lua/executor.c | 3 +++ src/nvim/lua/treesitter.c | 26 +++++++++++++++++--------- 2 files changed, 20 insertions(+), 9 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 9215926434..459c69f385 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1302,6 +1302,9 @@ LuaRef nlua_ref(lua_State *lstate, nlua_ref_state_t *ref_state, int index) return ref; } +// TODO(lewis6991): Currently cannot be run in __gc metamethods as they are +// invoked in lua_close() which can be invoked after the ref_markers map is +// destroyed in nlua_common_free_all_mem. LuaRef nlua_ref_global(lua_State *lstate, int index) { return nlua_ref(lstate, nlua_global_refs, index); diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 66a75f8d40..1e559316dd 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -325,6 +325,17 @@ static TSParser **parser_check(lua_State *L, uint16_t index) return luaL_checkudata(L, index, TS_META_PARSER); } +static void logger_gc(TSLogger logger) +{ + if (!logger.log) { + return; + } + + TSLuaLoggerOpts *opts = (TSLuaLoggerOpts *)logger.payload; + luaL_unref(opts->lstate, LUA_REGISTRYINDEX, opts->cb); + xfree(opts); +} + static int parser_gc(lua_State *L) { TSParser **p = parser_check(L, 1); @@ -332,12 +343,7 @@ static int parser_gc(lua_State *L) return 0; } - TSLogger logger = ts_parser_logger(*p); - if (logger.log) { - TSLuaLoggerOpts *opts = (TSLuaLoggerOpts *)logger.payload; - xfree(opts); - } - + logger_gc(ts_parser_logger(*p)); ts_parser_delete(*p); return 0; } @@ -698,7 +704,7 @@ static void logger_cb(void *payload, TSLogType logtype, const char *s) lua_State *lstate = opts->lstate; - nlua_pushref(lstate, opts->cb); + lua_rawgeti(lstate, LUA_REGISTRYINDEX, opts->cb); lua_pushstring(lstate, logtype == TSLogTypeParse ? "parse" : "lex"); lua_pushstring(lstate, s); if (lua_pcall(lstate, 2, 0, 0)) { @@ -726,11 +732,13 @@ static int parser_set_logger(lua_State *L) } TSLuaLoggerOpts *opts = xmalloc(sizeof(TSLuaLoggerOpts)); + lua_pushvalue(L, 4); + LuaRef ref = luaL_ref(L, LUA_REGISTRYINDEX); *opts = (TSLuaLoggerOpts){ .lex = lua_toboolean(L, 2), .parse = lua_toboolean(L, 3), - .cb = nlua_ref_global(L, 4), + .cb = ref, .lstate = L }; @@ -753,7 +761,7 @@ static int parser_get_logger(lua_State *L) TSLogger logger = ts_parser_logger(*p); if (logger.log) { TSLuaLoggerOpts *opts = (TSLuaLoggerOpts *)logger.payload; - nlua_pushref(L, opts->cb); + lua_rawgeti(L, LUA_REGISTRYINDEX, opts->cb); } else { lua_pushnil(L); } -- cgit From cefd774fac76b91f5368833555818c80c992c3b1 Mon Sep 17 00:00:00 2001 From: bfredl Date: Thu, 24 Aug 2023 15:14:23 +0200 Subject: refactor(memline): distinguish mutating uses of ml_get_buf() ml_get_buf() takes a third parameters to indicate whether the caller wants to mutate the memline data in place. However the vast majority of the call sites is using this function just to specify a buffer but without any mutation. This makes it harder to grep for the places which actually perform mutation. Solution: Remove the bool param from ml_get_buf(). it now works like ml_get() except for a non-current buffer. Add a new ml_get_buf_mut() function for the mutating use-case, which can be grepped along with the other ml_replace() etc functions which can modify the memline. --- src/nvim/lua/executor.c | 2 +- src/nvim/lua/stdlib.c | 2 +- src/nvim/lua/treesitter.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 459c69f385..4aaf4397f9 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1707,7 +1707,7 @@ void ex_luado(exarg_T *const eap) break; } lua_pushvalue(lstate, -1); - const char *const old_line = ml_get_buf(curbuf, l, false); + 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); lua_pushstring(lstate, old_line); diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index cf90420586..4f9677650f 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -105,7 +105,7 @@ static int regex_match_line(lua_State *lstate) return luaL_error(lstate, "invalid row"); } - char *line = ml_get_buf(buf, rownr + 1, false); + char *line = ml_get_buf(buf, rownr + 1); size_t len = strlen(line); if (start < 0 || (size_t)start > len) { diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 1e559316dd..995ed407d4 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -365,7 +365,7 @@ static const char *input_cb(void *payload, uint32_t byte_index, TSPoint position *bytes_read = 0; return ""; } - char *line = ml_get_buf(bp, (linenr_T)position.row + 1, false); + char *line = ml_get_buf(bp, (linenr_T)position.row + 1); size_t len = strlen(line); if (position.column > len) { *bytes_read = 0; -- cgit From 6e45567b498ca8455aaf3628c10de997ac070ee1 Mon Sep 17 00:00:00 2001 From: nwounkn Date: Tue, 29 Aug 2023 13:48:23 +0500 Subject: fix(treesitter): fix TSNode:tree() double free (#24796) Problem: `push_tree`, every time its called for the same TSTree with `do_copy=false` argument, creates a new userdata for it. Each userdata, when garbage collected, frees the same TSTree C object. Solution: Add flag to userdata, which indicates, should C object, which userdata points to, be freed, when userdata is garbage collected. --- src/nvim/lua/treesitter.c | 61 ++++++++++++++++++++++++----------------------- 1 file changed, 31 insertions(+), 30 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 995ed407d4..39935f656f 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -51,6 +51,11 @@ typedef struct { bool parse; } TSLuaLoggerOpts; +typedef struct { + TSTree *tree; + bool gc; +} TSLuaTree; + #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/treesitter.c.generated.h" #endif @@ -424,8 +429,8 @@ static int parser_parse(lua_State *L) TSTree *old_tree = NULL; if (!lua_isnil(L, 2)) { - TSTree **tmp = tree_check(L, 2); - old_tree = tmp ? *tmp : NULL; + TSLuaTree *ud = tree_check(L, 2); + old_tree = ud ? ud->tree : NULL; } TSTree *new_tree = NULL; @@ -478,7 +483,7 @@ static int parser_parse(lua_State *L) uint32_t n_ranges = 0; TSRange *changed = old_tree ? ts_tree_get_changed_ranges(old_tree, new_tree, &n_ranges) : NULL; - push_tree(L, new_tree, false); // [tree] + push_tree(L, new_tree, true); // [tree] push_ranges(L, changed, n_ranges, include_bytes); // [tree, ranges] @@ -498,12 +503,13 @@ static int parser_reset(lua_State *L) static int tree_copy(lua_State *L) { - TSTree **tree = tree_check(L, 1); - if (!(*tree)) { + TSLuaTree *ud = tree_check(L, 1); + if (!ud) { return 0; } - push_tree(L, *tree, true); // [tree] + TSTree *copy = ts_tree_copy(ud->tree); + push_tree(L, copy, true); // [tree] return 1; } @@ -515,8 +521,8 @@ static int tree_edit(lua_State *L) return lua_error(L); } - TSTree **tree = tree_check(L, 1); - if (!(*tree)) { + TSLuaTree *ud = tree_check(L, 1); + if (!ud) { return 0; } @@ -530,22 +536,22 @@ static int tree_edit(lua_State *L) TSInputEdit edit = { start_byte, old_end_byte, new_end_byte, start_point, old_end_point, new_end_point }; - ts_tree_edit(*tree, &edit); + ts_tree_edit(ud->tree, &edit); return 0; } static int tree_get_ranges(lua_State *L) { - TSTree **tree = tree_check(L, 1); - if (!(*tree)) { + TSLuaTree *ud = tree_check(L, 1); + if (!ud) { return 0; } bool include_bytes = (lua_gettop(L) >= 2) && lua_toboolean(L, 2); uint32_t len; - TSRange *ranges = ts_tree_included_ranges(*tree, &len); + TSRange *ranges = ts_tree_included_ranges(ud->tree, &len); push_ranges(L, ranges, len, include_bytes); @@ -773,20 +779,17 @@ static int parser_get_logger(lua_State *L) /// push tree interface on lua stack. /// -/// This makes a copy of the tree, so ownership of the argument is unaffected. -void push_tree(lua_State *L, TSTree *tree, bool do_copy) +/// The tree is garbage collected if gc is true +void push_tree(lua_State *L, TSTree *tree, bool gc) { if (tree == NULL) { lua_pushnil(L); return; } - TSTree **ud = lua_newuserdata(L, sizeof(TSTree *)); // [udata] + TSLuaTree *ud = lua_newuserdata(L, sizeof(TSLuaTree)); // [udata] - if (do_copy) { - *ud = ts_tree_copy(tree); - } else { - *ud = tree; - } + ud->tree = tree; + ud->gc = gc; lua_getfield(L, LUA_REGISTRYINDEX, TS_META_TREE); // [udata, meta] lua_setmetatable(L, -2); // [udata] @@ -800,20 +803,18 @@ void push_tree(lua_State *L, TSTree *tree, bool do_copy) lua_setfenv(L, -2); // [udata] } -static TSTree **tree_check(lua_State *L, int index) +static TSLuaTree *tree_check(lua_State *L, int index) { - TSTree **ud = luaL_checkudata(L, index, TS_META_TREE); + TSLuaTree *ud = luaL_checkudata(L, index, TS_META_TREE); return ud; } static int tree_gc(lua_State *L) { - TSTree **tree = tree_check(L, 1); - if (!tree) { - return 0; + TSLuaTree *ud = tree_check(L, 1); + if (ud && ud->gc) { + ts_tree_delete(ud->tree); } - - ts_tree_delete(*tree); return 0; } @@ -825,11 +826,11 @@ static int tree_tostring(lua_State *L) static int tree_root(lua_State *L) { - TSTree **tree = tree_check(L, 1); - if (!tree) { + TSLuaTree *ud = tree_check(L, 1); + if (!ud) { return 0; } - TSNode root = ts_tree_root_node(*tree); + TSNode root = ts_tree_root_node(ud->tree); push_node(L, root, 1); return 1; } -- cgit From 50a03c0e9975925e3198a2741c5b9fc0ad727e84 Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 29 Aug 2023 11:21:57 +0200 Subject: fix(treesitter): fix another TSNode:tree() double free Unfortunately the gc=false objects can refer to a dangling tree if the gc=true tree was freed first. This reuses the same tree object as the node itself is keeping alive via the uservalue of the node userdata. (wrapped in a table due to lua 5.1 restrictions) --- src/nvim/lua/treesitter.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 39935f656f..7b308ef7b4 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -53,7 +53,6 @@ typedef struct { typedef struct { TSTree *tree; - bool gc; } TSLuaTree; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -477,13 +476,12 @@ static int parser_parse(lua_State *L) return luaL_error(L, "An error occurred when parsing."); } - // The new tree will be pushed to the stack, without copy, ownership is now to - // the lua GC. - // Old tree is still owned by the lua GC. + // The new tree will be pushed to the stack, without copy, ownership is now to the lua GC. + // Old tree is owned by lua GC since before uint32_t n_ranges = 0; TSRange *changed = old_tree ? ts_tree_get_changed_ranges(old_tree, new_tree, &n_ranges) : NULL; - push_tree(L, new_tree, true); // [tree] + push_tree(L, new_tree); // [tree] push_ranges(L, changed, n_ranges, include_bytes); // [tree, ranges] @@ -509,7 +507,7 @@ static int tree_copy(lua_State *L) } TSTree *copy = ts_tree_copy(ud->tree); - push_tree(L, copy, true); // [tree] + push_tree(L, copy); // [tree] return 1; } @@ -779,8 +777,9 @@ static int parser_get_logger(lua_State *L) /// push tree interface on lua stack. /// -/// The tree is garbage collected if gc is true -void push_tree(lua_State *L, TSTree *tree, bool gc) +/// The tree is not copied. Ownership of the tree is transfered from c code to +/// lua. if needed use ts_tree_copy() in the caller +void push_tree(lua_State *L, TSTree *tree) { if (tree == NULL) { lua_pushnil(L); @@ -789,7 +788,6 @@ void push_tree(lua_State *L, TSTree *tree, bool gc) TSLuaTree *ud = lua_newuserdata(L, sizeof(TSLuaTree)); // [udata] ud->tree = tree; - ud->gc = gc; lua_getfield(L, LUA_REGISTRYINDEX, TS_META_TREE); // [udata, meta] lua_setmetatable(L, -2); // [udata] @@ -812,7 +810,7 @@ static TSLuaTree *tree_check(lua_State *L, int index) static int tree_gc(lua_State *L) { TSLuaTree *ud = tree_check(L, 1); - if (ud && ud->gc) { + if (ud) { ts_tree_delete(ud->tree); } return 0; @@ -1322,7 +1320,9 @@ static int node_tree(lua_State *L) return 0; } - push_tree(L, (TSTree *)node.tree, false); + lua_getfenv(L, 1); // [udata, reftable] + lua_rawgeti(L, -1, 1); // [udata, reftable, tree_udata] + return 1; } -- cgit From f85aa2e67f3e11c3a0f1e373d321398681e84990 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Wed, 30 Aug 2023 10:21:35 +0100 Subject: fix(treesitter.c): improve comments on fenv usage --- src/nvim/lua/treesitter.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 7b308ef7b4..49e920f748 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -154,9 +154,9 @@ static void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta) lua_pop(L, 1); // [] (don't use it now) } -/// init the tslua library +/// Init the tslua library. /// -/// all global state is stored in the regirstry of the lua_State +/// All global state is stored in the registry of the lua_State. void tslua_init(lua_State *L) { // type metatables @@ -775,16 +775,17 @@ static int parser_get_logger(lua_State *L) // Tree methods -/// push tree interface on lua stack. +/// Push tree interface on to the lua stack. /// -/// The tree is not copied. Ownership of the tree is transfered from c code to -/// lua. if needed use ts_tree_copy() in the caller -void push_tree(lua_State *L, TSTree *tree) +/// The tree is not copied. Ownership of the tree is transferred from C to +/// Lua. If needed use ts_tree_copy() in the caller +static void push_tree(lua_State *L, TSTree *tree) { if (tree == NULL) { lua_pushnil(L); return; } + TSLuaTree *ud = lua_newuserdata(L, sizeof(TSLuaTree)); // [udata] ud->tree = tree; @@ -792,9 +793,10 @@ void push_tree(lua_State *L, TSTree *tree) lua_getfield(L, LUA_REGISTRYINDEX, TS_META_TREE); // [udata, meta] lua_setmetatable(L, -2); // [udata] - // table used for node wrappers to keep a reference to tree wrapper - // NB: in lua 5.3 the uservalue for the node could just be the tree, but - // in lua 5.1 the uservalue (fenv) must be a table. + // To prevent the tree from being garbage collected, create a reference to it + // in the fenv which will be passed to userdata nodes of the tree. + // Note: environments (fenvs) associated with userdata have no meaning in Lua + // and are only used to associate a table. lua_createtable(L, 1, 0); // [udata, reftable] lua_pushvalue(L, -2); // [udata, reftable, udata] lua_rawseti(L, -2, 1); // [udata, reftable] @@ -835,9 +837,9 @@ static int tree_root(lua_State *L) // Node methods -/// push node interface on lua stack +/// Push node interface on to the Lua stack /// -/// top of stack must either be the tree this node belongs to or another node +/// Top of stack must either be the tree this node belongs to or another node /// of the same tree! This value is not popped. Can only be called inside a /// cfunction with the tslua environment. static void push_node(lua_State *L, TSNode node, int uindex) @@ -851,6 +853,8 @@ static void push_node(lua_State *L, TSNode node, int uindex) *ud = node; lua_getfield(L, LUA_REGISTRYINDEX, TS_META_NODE); // [udata, meta] lua_setmetatable(L, -2); // [udata] + + // Copy the fenv which contains the nodes tree. lua_getfenv(L, uindex); // [udata, reftable] lua_setfenv(L, -2); // [udata] } -- cgit From 845d5b8b64190e0e09a6a6dd97bdbc0e6f96eb02 Mon Sep 17 00:00:00 2001 From: Amaan Qureshi Date: Wed, 19 Jul 2023 05:02:49 -0400 Subject: feat(treesitter): improve query error message --- src/nvim/lua/treesitter.c | 80 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 9 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 49e920f748..48057b0c65 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -1543,8 +1543,9 @@ int tslua_parse_query(lua_State *L) TSQuery *query = ts_query_new(lang, src, (uint32_t)len, &error_offset, &error_type); if (!query) { - return luaL_error(L, "query: %s at position %d for language %s", - query_err_string(error_type), (int)error_offset, lang_name); + char err_msg[IOSIZE]; + query_err_string(src, (int)error_offset, error_type, err_msg, sizeof(err_msg)); + return luaL_error(L, "%s", err_msg); } TSQuery **ud = lua_newuserdata(L, sizeof(TSQuery *)); // [udata] @@ -1554,24 +1555,85 @@ int tslua_parse_query(lua_State *L) return 1; } -static const char *query_err_string(TSQueryError err) +static const char *query_err_to_string(TSQueryError error_type) { - switch (err) { + switch (error_type) { case TSQueryErrorSyntax: - return "invalid syntax"; + return "Invalid syntax:\n"; case TSQueryErrorNodeType: - return "invalid node type"; + return "Invalid node type "; case TSQueryErrorField: - return "invalid field"; + return "Invalid field name "; case TSQueryErrorCapture: - return "invalid capture"; + return "Invalid capture name "; case TSQueryErrorStructure: - return "invalid structure"; + return "Impossible pattern:\n"; default: return "error"; } } +static void query_err_string(const char *src, int error_offset, TSQueryError error_type, char *err, + size_t errlen) +{ + int line_start = 0; + int row = 0; + const char *error_line = NULL; + int error_line_len = 0; + + const char *end_str; + const char *src_tmp = src; + while ((end_str = strchr(src_tmp, '\n')) != NULL) { + int line_length = (int)(end_str - src_tmp) + 1; + int line_end = line_start + line_length; + if (line_end > error_offset) { + error_line = src_tmp; + error_line_len = line_length; + break; + } + line_start = line_end; + row++; + src_tmp += line_length; + } + + // Additional check for the last line + if (line_start <= error_offset) { + error_line = src_tmp; + error_line_len = (int)strlen(src_tmp); + } + + int column = error_offset - line_start; + + const char *type_msg = query_err_to_string(error_type); + snprintf(err, errlen, "Query error at %d:%d. %s", row + 1, column + 1, type_msg); + size_t offset = strlen(err); + errlen = errlen - offset; + err = err + offset; + + // Error types that report names + if (error_type == TSQueryErrorNodeType + || error_type == TSQueryErrorField + || error_type == TSQueryErrorCapture) { + const char *suffix = src + error_offset; + int suffix_len = 0; + char c = suffix[suffix_len]; + while (isalnum(c) || c == '_' || c == '-' || c == '.') { + c = suffix[++suffix_len]; + } + snprintf(err, errlen, "\"%.*s\":\n", suffix_len, suffix); + offset = strlen(err); + errlen = errlen - offset; + err = err + offset; + } + + if (!error_line) { + snprintf(err, errlen, "Unexpected EOF\n"); + return; + } + + snprintf(err, errlen, "%.*s\n%*s^\n", error_line_len, error_line, column, ""); +} + static TSQuery *query_check(lua_State *L, int index) { TSQuery **ud = luaL_checkudata(L, index, TS_META_QUERY); -- cgit From dd0e77d48a843fc9730fe4ef7567330daf8dfb81 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 31 Aug 2023 14:11:15 +0100 Subject: fix(query_error): multiline bug --- src/nvim/lua/treesitter.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 48057b0c65..45fe7f6129 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -1582,25 +1582,19 @@ static void query_err_string(const char *src, int error_offset, TSQueryError err int error_line_len = 0; const char *end_str; - const char *src_tmp = src; - while ((end_str = strchr(src_tmp, '\n')) != NULL) { - int line_length = (int)(end_str - src_tmp) + 1; + do { + const char *src_tmp = src + line_start; + end_str = strchr(src_tmp, '\n'); + int line_length = end_str != NULL ? (int)(end_str - src_tmp) : (int)strlen(src_tmp); int line_end = line_start + line_length; if (line_end > error_offset) { error_line = src_tmp; error_line_len = line_length; break; } - line_start = line_end; + line_start = line_end + 1; row++; - src_tmp += line_length; - } - - // Additional check for the last line - if (line_start <= error_offset) { - error_line = src_tmp; - error_line_len = (int)strlen(src_tmp); - } + } while (end_str != NULL); int column = error_offset - line_start; -- cgit From f30844008bdd313b03a19486159f571a067e68b9 Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Sun, 3 Sep 2023 00:38:10 +0200 Subject: build: download busted from own neovim/deps repository Downloading the necessary files all at once instead of doing dependency handling with luarocks speeds up installation immensely. We speed up the process even more by using luv as a replacement for the C modules in the busted dependencies, which allows us to skip costly compilation times. Co-authored-by: bfredl --- src/nvim/lua/executor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 4aaf4397f9..f2efd866f8 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -140,8 +140,8 @@ void nlua_error(lua_State *const lstate, const char *const msg) } if (in_script) { - os_errmsg(str); - os_errmsg("\n"); + fprintf(stderr, msg, (int)len, str); + fprintf(stderr, "\n"); } else { msg_ext_set_kind("lua_error"); semsg_multiline(msg, (int)len, str); -- cgit From 5970157e1d22fd5e05ae5d3bd949f807fb7a744c Mon Sep 17 00:00:00 2001 From: bfredl Date: Wed, 17 May 2023 16:08:06 +0200 Subject: refactor(map): enhanced implementation, Clean Code™, etc etc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This involves two redesigns of the map.c implementations: 1. Change of macro style and code organization The old khash.h and map.c implementation used huge #define blocks with a lot of backslash line continuations. This instead uses the "implementation file" .c.h pattern. Such a file is meant to be included multiple times, with different macros set prior to inclusion as parameters. we already use this pattern e.g. for eval/typval_encode.c.h to implement different typval encoders reusing a similar structure. We can structure this code into two parts. one that only depends on key type and is enough to implement sets, and one which depends on both key and value to implement maps (as a wrapper around sets, with an added value[] array) 2. Separate the main hash buckets from the key / value arrays Change the hack buckets to only contain an index into separate key / value arrays This is a common pattern in modern, state of the art hashmap implementations. Even though this leads to one more allocated array, it is this often is a net reduction of memory consumption. Consider key+value consuming at least 12 bytes per pair. On average, we will have twice as many buckets per item. Thus old implementation: 2*12 = 24 bytes per item New implementation 1*12 + 2*4 = 20 bytes per item And the difference gets bigger with larger items. One might think we have pulled a fast one here, as wouldn't the average size of the new key/value arrays be 1.5 slots per items due to amortized grows? But remember, these arrays are fully dense, and thus the accessed memory, measured in _cache lines_, the unit which actually matters, will be the fully used memory but just rounded up to the nearest cache line boundary. This has some other interesting properties, such as an insert-only set/map will be fully ordered by insert only. Preserving this ordering in face of deletions is more tricky tho. As we currently don't use ordered maps, the "delete" operation maintains compactness of the item arrays in the simplest way by breaking the ordering. It would be possible to implement an order-preserving delete although at some cost, like allowing the items array to become non-dense until the next rehash. Finally, in face of these two major changes, all code used in khash.h has been integrated into map.c and friends. Given the heavy edits it makes no sense to "layer" the code into a vendored and a wrapper part. Rather, the layered cake follows the specialization depth: code shared for all maps, code specialized to a key type (and its equivalence relation), and finally code specialized to value+key type. --- src/nvim/lua/treesitter.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 45fe7f6129..d255dd56e5 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -173,7 +173,7 @@ void tslua_init(lua_State *L) int tslua_has_language(lua_State *L) { const char *lang_name = luaL_checkstring(L, 1); - lua_pushboolean(L, pmap_has(cstr_t)(&langs, lang_name)); + lua_pushboolean(L, map_has(cstr_t, &langs, lang_name)); return 1; } @@ -190,7 +190,7 @@ int tslua_add_language(lua_State *L) symbol_name = luaL_checkstring(L, 3); } - if (pmap_has(cstr_t)(&langs, lang_name)) { + if (map_has(cstr_t, &langs, lang_name)) { lua_pushboolean(L, true); return 1; } @@ -243,7 +243,7 @@ int tslua_add_language(lua_State *L) int tslua_remove_lang(lua_State *L) { const char *lang_name = luaL_checkstring(L, 1); - bool present = pmap_has(cstr_t)(&langs, lang_name); + bool present = map_has(cstr_t, &langs, lang_name); if (present) { cstr_t key; pmap_del(cstr_t)(&langs, lang_name, &key); -- cgit From 5331d5772ffbbdb3635d0a4b41306817097e86de Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 23 Sep 2023 15:59:37 +0800 Subject: fix(lua): show error message when failing to set variable (#25321) --- src/nvim/lua/stdlib.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 4f9677650f..8f1b5c7b86 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -359,6 +359,9 @@ int nlua_setvar(lua_State *lstate) Error err = ERROR_INIT; dictitem_T *di = dict_check_writable(dict, key, del, &err); if (ERROR_SET(&err)) { + nlua_push_errstr(lstate, "%s", err.msg); + api_clear_error(&err); + lua_error(lstate); return 0; } -- cgit From 4d3a38ac074fff7e2a4bede4cee7699bdd55ffdc Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 24 Sep 2023 10:57:09 +0800 Subject: fix(api, lua): handle setting v: variables properly (#25325) --- src/nvim/lua/stdlib.c | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 8f1b5c7b86..f175a7abb0 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -24,6 +24,7 @@ #include "nvim/buffer_defs.h" #include "nvim/eval/typval.h" #include "nvim/eval/typval_defs.h" +#include "nvim/eval/vars.h" #include "nvim/ex_eval.h" #include "nvim/fold.h" #include "nvim/globals.h" @@ -397,6 +398,15 @@ int nlua_setvar(lua_State *lstate) di = tv_dict_item_alloc_len(key.data, key.size); tv_dict_add(dict, di); } else { + bool type_error = false; + if (dict == &vimvardict + && !before_set_vvar(key.data, di, &tv, true, watched, &type_error)) { + tv_clear(&tv); + if (type_error) { + return luaL_error(lstate, "Setting v:%s to value with wrong type", key.data); + } + return 0; + } if (watched) { tv_copy(&di->di_tv, &oldtv); } -- cgit From b85f1dafc7c0a19704135617454f1c66f41202c1 Mon Sep 17 00:00:00 2001 From: bfredl Date: Wed, 27 Sep 2023 22:21:17 +0200 Subject: refactor(messages): fold msg_attr into msg problem: there are too many different functions in message.c solution: fold some of the functions into themselves --- src/nvim/lua/executor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index f2efd866f8..32706b74f1 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -953,11 +953,11 @@ static void nlua_print_event(void **argv) } break; } - msg(str + start); + msg(str + start, 0); msg_didout = true; // Make blank lines work properly } if (len && str[len - 1] == NUL) { // Last was newline - msg(""); + msg("", 0); } xfree(str); } -- cgit From bc13bc154aa574e0bb58a50f2e0ca4570efa57c3 Mon Sep 17 00:00:00 2001 From: bfredl Date: Fri, 29 Sep 2023 16:10:54 +0200 Subject: refactor(message): smsg_attr -> smsg --- src/nvim/lua/secure.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/secure.c b/src/nvim/lua/secure.c index 30d5a95fc0..2552e21884 100644 --- a/src/nvim/lua/secure.c +++ b/src/nvim/lua/secure.c @@ -76,11 +76,11 @@ static bool nlua_trust(const char *action, const char *path) if (msg != NULL) { if (success) { if (strcmp(action, "allow") == 0) { - smsg("Allowed \"%s\" in trust database.", msg); + smsg(0, "Allowed \"%s\" in trust database.", msg); } else if (strcmp(action, "deny") == 0) { - smsg("Denied \"%s\" in trust database.", msg); + smsg(0, "Denied \"%s\" in trust database.", msg); } else if (strcmp(action, "remove") == 0) { - smsg("Removed \"%s\" from trust database.", msg); + smsg(0, "Removed \"%s\" from trust database.", msg); } } else { semsg(e_trustfile, msg); -- cgit From cf8b2c0e74fd5e723b0c15c2ce84e6900fd322d3 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 30 Sep 2023 12:05:28 +0800 Subject: build(iwyu): add a few more _defs.h mappings (#25435) --- src/nvim/lua/converter.c | 1 - src/nvim/lua/executor.c | 4 ++-- src/nvim/lua/executor.h | 1 + src/nvim/lua/secure.c | 8 ++++++-- src/nvim/lua/spell.c | 1 - src/nvim/lua/stdlib.c | 2 -- src/nvim/lua/treesitter.c | 2 +- src/nvim/lua/xdiff.c | 1 + 8 files changed, 11 insertions(+), 9 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 385ac11546..c041fdb26c 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -22,7 +22,6 @@ #include "nvim/eval/typval_defs.h" #include "nvim/eval/typval_encode.h" #include "nvim/eval/userfunc.h" -#include "nvim/garray.h" #include "nvim/gettext.h" #include "nvim/lua/converter.h" #include "nvim/lua/executor.h" diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 32706b74f1..0c9bbebe26 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -7,6 +7,8 @@ #include #include #include +#include +#include #include #include #include @@ -17,7 +19,6 @@ #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" #include "nvim/ascii.h" -#include "nvim/buffer_defs.h" #include "nvim/change.h" #include "nvim/cursor.h" #include "nvim/drawscreen.h" @@ -59,7 +60,6 @@ #include "nvim/ui.h" #include "nvim/undo.h" #include "nvim/usercmd.h" -#include "nvim/version.h" #include "nvim/vim.h" #include "nvim/window.h" diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index f340d9d0d8..b132a0d147 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -13,6 +13,7 @@ #include "nvim/func_attr.h" #include "nvim/lua/converter.h" #include "nvim/macros.h" +#include "nvim/map.h" #include "nvim/types.h" #include "nvim/usercmd.h" diff --git a/src/nvim/lua/secure.c b/src/nvim/lua/secure.c index 2552e21884..194c80fdc9 100644 --- a/src/nvim/lua/secure.c +++ b/src/nvim/lua/secure.c @@ -1,13 +1,17 @@ // This is an open source non-commercial project. Dear PVS-Studio, please check // it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com -#include #include -#include +#include +#include #include "nvim/charset.h" +#include "nvim/ex_cmds_defs.h" +#include "nvim/gettext.h" +#include "nvim/globals.h" #include "nvim/lua/executor.h" #include "nvim/lua/secure.h" +#include "nvim/memory.h" #include "nvim/message.h" #ifdef INCLUDE_GENERATED_DECLARATIONS diff --git a/src/nvim/lua/spell.c b/src/nvim/lua/spell.c index 37f1c5216d..04304719d0 100644 --- a/src/nvim/lua/spell.c +++ b/src/nvim/lua/spell.c @@ -16,7 +16,6 @@ #include "nvim/lua/spell.h" #include "nvim/message.h" #include "nvim/spell.h" -#include "nvim/types.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/spell.c.generated.h" // IWYU pragma: export diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index f175a7abb0..7690853d57 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -15,7 +15,6 @@ # include "bit.h" #endif -#include "auto/config.h" #include "cjson/lua_cjson.h" #include "mpack/lmpack.h" #include "nvim/api/private/defs.h" @@ -23,7 +22,6 @@ #include "nvim/ascii.h" #include "nvim/buffer_defs.h" #include "nvim/eval/typval.h" -#include "nvim/eval/typval_defs.h" #include "nvim/eval/vars.h" #include "nvim/ex_eval.h" #include "nvim/fold.h" diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index d255dd56e5..265c4bf5ca 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -6,6 +6,7 @@ // trees and nodes, and could be broken out as a reusable lua package #include +#include #include #include #include @@ -20,7 +21,6 @@ #include "nvim/api/private/helpers.h" #include "nvim/buffer_defs.h" #include "nvim/globals.h" -#include "nvim/lua/executor.h" #include "nvim/lua/treesitter.h" #include "nvim/macros.h" #include "nvim/map.h" diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index b5575fe202..000aad85d1 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include "luaconf.h" -- cgit From dc6d0d2daf69e2fdadda81feb97906dbc962a239 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 30 Sep 2023 14:41:34 +0800 Subject: refactor: reorganize option header files (#25437) - Move vimoption_T to option.h - option_defs.h is for option-related types - option_vars.h corresponds to Vim's option.h - option_defs.h and option_vars.h don't include each other --- src/nvim/lua/executor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 0c9bbebe26..18da7af416 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -49,7 +49,7 @@ #include "nvim/memory.h" #include "nvim/message.h" #include "nvim/msgpack_rpc/channel.h" -#include "nvim/option_defs.h" +#include "nvim/option_vars.h" #include "nvim/os/fileio.h" #include "nvim/os/os.h" #include "nvim/path.h" -- cgit From 09a17f91d0d362c6e58bfdbe3ccdeacffb0b44b9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 2 Oct 2023 10:45:33 +0800 Subject: refactor: move cmdline completion types to cmdexpand_defs.h (#25465) --- src/nvim/lua/executor.c | 1 + src/nvim/lua/executor.h | 1 + 2 files changed, 2 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 18da7af416..d6fd14cc10 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -20,6 +20,7 @@ #include "nvim/api/private/helpers.h" #include "nvim/ascii.h" #include "nvim/change.h" +#include "nvim/cmdexpand_defs.h" #include "nvim/cursor.h" #include "nvim/drawscreen.h" #include "nvim/eval.h" diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index b132a0d147..f3e2b6d1d0 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -8,6 +8,7 @@ #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" #include "nvim/assert.h" +#include "nvim/cmdexpand_defs.h" #include "nvim/eval/typval_defs.h" #include "nvim/ex_cmds_defs.h" #include "nvim/func_attr.h" -- cgit From 8e932480f61d6101bf8bea1abc07ed93826221fd Mon Sep 17 00:00:00 2001 From: dundargoc Date: Fri, 29 Sep 2023 14:58:48 +0200 Subject: refactor: the long goodbye long is 32 bits on windows, while it is 64 bits on other architectures. This makes the type suboptimal for a codebase meant to be cross-platform. Replace it with more appropriate integer types. --- src/nvim/lua/treesitter.c | 2 +- src/nvim/lua/xdiff.c | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 265c4bf5ca..cd8a9b0739 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -694,7 +694,7 @@ static int parser_get_timeout(lua_State *L) return 0; } - lua_pushinteger(L, (long)ts_parser_timeout_micros(*p)); + lua_pushinteger(L, (lua_Integer)ts_parser_timeout_micros(*p)); return 1; } diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 000aad85d1..5aba1b839b 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -71,8 +71,8 @@ static void get_linematch_results(lua_State *lstate, mmfile_t *ma, mmfile_t *mb, const char *diff_begin[2] = { ma->ptr, mb->ptr }; int diff_length[2] = { (int)count_a, (int)count_b }; - fastforward_buf_to_lnum(&diff_begin[0], start_a + 1); - fastforward_buf_to_lnum(&diff_begin[1], start_b + 1); + fastforward_buf_to_lnum(&diff_begin[0], (linenr_T)start_a + 1); + fastforward_buf_to_lnum(&diff_begin[1], (linenr_T)start_b + 1); int *decisions = NULL; size_t decisions_length = linematch_nbuffers(diff_begin, diff_length, 2, &decisions, iwhite); @@ -125,7 +125,7 @@ static int write_string(void *priv, mmbuffer_t *mb, int nbuf) } // hunk_func callback used when opts.hunk_lines = true -static int hunk_locations_cb(long start_a, long count_a, long start_b, long count_b, void *cb_data) +static int hunk_locations_cb(int start_a, int count_a, int start_b, int count_b, void *cb_data) { hunkpriv_t *priv = (hunkpriv_t *)cb_data; lua_State *lstate = priv->lstate; @@ -140,7 +140,7 @@ static int hunk_locations_cb(long start_a, long count_a, long start_b, long coun } // hunk_func callback used when opts.on_hunk is given -static int call_on_hunk_cb(long start_a, long count_a, long start_b, long count_b, void *cb_data) +static int call_on_hunk_cb(int start_a, int count_a, int start_b, int count_b, void *cb_data) { // Mimic extra offsets done by xdiff, see: // src/xdiff/xemit.c:284 -- cgit From 13f55750e9bea8ec8f50550546edc64a0f0964d8 Mon Sep 17 00:00:00 2001 From: nwounkn Date: Fri, 13 Oct 2023 12:01:26 +0500 Subject: fix(ui): empty line before the next message after :silent command Problem: The next command after `silent !{cmd}` or `silent lua print('str')` prints an empty line before printing a message, because these commands set `msg_didout = true` despite not printing any messages. Solution: Set `msg_didout = true` only if `msg_silent == 0` --- src/nvim/lua/executor.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index d6fd14cc10..3975312703 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -955,7 +955,9 @@ static void nlua_print_event(void **argv) break; } msg(str + start, 0); - msg_didout = true; // Make blank lines work properly + if (msg_silent == 0) { + msg_didout = true; // Make blank lines work properly + } } if (len && str[len - 1] == NUL) { // Last was newline msg("", 0); -- cgit From 5f03a1eaabfc8de2b3a9c666fcd604763f41e152 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Fri, 20 Oct 2023 15:10:33 +0200 Subject: build(lint): remove unnecessary clint.py rules Uncrustify is the source of truth where possible. Remove any redundant checks from clint.py. --- src/nvim/lua/executor.h | 4 ++-- src/nvim/lua/stdlib.c | 2 +- src/nvim/lua/treesitter.c | 4 ++-- src/nvim/lua/xdiff.c | 10 +++++----- 4 files changed, 10 insertions(+), 10 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index f3e2b6d1d0..4b4d1dded8 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -45,7 +45,7 @@ typedef struct { # include "lua/executor.h.generated.h" #endif -EXTERN nlua_ref_state_t *nlua_global_refs INIT(= NULL); -EXTERN bool nlua_disable_preload INIT(= false); +EXTERN nlua_ref_state_t *nlua_global_refs INIT( = NULL); +EXTERN bool nlua_disable_preload INIT( = false); #endif // NVIM_LUA_EXECUTOR_H diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 7690853d57..14e9902ee2 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -515,7 +515,7 @@ static int nlua_iconv(lua_State *lstate) const char *str = lua_tolstring(lstate, 1, &str_len); char *from = enc_canonize(enc_skip((char *)lua_tolstring(lstate, 2, NULL))); - char *to = enc_canonize(enc_skip((char *)lua_tolstring(lstate, 3, NULL))); + char *to = enc_canonize(enc_skip((char *)lua_tolstring(lstate, 3, NULL))); vimconv_T vimconv; vimconv.vc_type = CONV_NONE; diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index cd8a9b0739..57469f6358 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -361,7 +361,7 @@ static int parser_tostring(lua_State *L) static const char *input_cb(void *payload, uint32_t byte_index, TSPoint position, uint32_t *bytes_read) { - buf_T *bp = payload; + buf_T *bp = payload; #define BUFSIZE 256 static char buf[BUFSIZE]; @@ -1585,7 +1585,7 @@ static void query_err_string(const char *src, int error_offset, TSQueryError err do { const char *src_tmp = src + line_start; end_str = strchr(src_tmp, '\n'); - int line_length = end_str != NULL ? (int)(end_str - src_tmp) : (int)strlen(src_tmp); + int line_length = end_str != NULL ? (int)(end_str - src_tmp) : (int)strlen(src_tmp); int line_end = line_start + line_length; if (line_end > error_offset) { error_line = src_tmp; diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 5aba1b839b..56d1fdb8ab 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -367,18 +367,18 @@ int nlua_xdl_diff(lua_State *lstate) cfg.hunk_func = call_on_hunk_cb; priv = (hunkpriv_t) { .lstate = lstate, - .err = &err, + .err = &err, }; ecb.priv = &priv; break; case kNluaXdiffModeLocations: cfg.hunk_func = hunk_locations_cb; priv = (hunkpriv_t) { - .lstate = lstate, - .ma = &ma, - .mb = &mb, + .lstate = lstate, + .ma = &ma, + .mb = &mb, .linematch = linematch, - .iwhite = (params.flags & XDF_IGNORE_WHITESPACE) > 0 + .iwhite = (params.flags & XDF_IGNORE_WHITESPACE) > 0 }; ecb.priv = &priv; lua_createtable(lstate, 0, 0); -- cgit From 224f303ee54c54d2147f03010385e8cc48e42869 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Tue, 31 Oct 2023 09:15:32 -0500 Subject: feat(stdlib): add vim.base64 module (#25843) Add base64 encode() and decode() functions to a vim.base64 module. --- src/nvim/lua/base64.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/nvim/lua/base64.h | 12 ++++++++++ src/nvim/lua/stdlib.c | 5 ++++ 3 files changed, 82 insertions(+) create mode 100644 src/nvim/lua/base64.c create mode 100644 src/nvim/lua/base64.h (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/base64.c b/src/nvim/lua/base64.c new file mode 100644 index 0000000000..3f246839d5 --- /dev/null +++ b/src/nvim/lua/base64.c @@ -0,0 +1,65 @@ +#include +#include +#include + +#include "nvim/base64.h" +#include "nvim/lua/base64.h" +#include "nvim/memory.h" + +static int nlua_base64_encode(lua_State *L) +{ + if (lua_gettop(L) < 1) { + return luaL_error(L, "Expected 1 argument"); + } + + if (lua_type(L, 1) != LUA_TSTRING) { + luaL_argerror(L, 1, "expected string"); + } + + size_t src_len = 0; + const char *src = lua_tolstring(L, 1, &src_len); + + const char *ret = base64_encode(src, src_len); + assert(ret != NULL); + lua_pushstring(L, ret); + xfree((void *)ret); + + return 1; +} + +static int nlua_base64_decode(lua_State *L) +{ + if (lua_gettop(L) < 1) { + return luaL_error(L, "Expected 1 argument"); + } + + if (lua_type(L, 1) != LUA_TSTRING) { + luaL_argerror(L, 1, "expected string"); + } + + size_t src_len = 0; + const char *src = lua_tolstring(L, 1, &src_len); + + const char *ret = base64_decode(src, src_len); + if (ret == NULL) { + return luaL_error(L, "Invalid input"); + } + + lua_pushstring(L, ret); + xfree((void *)ret); + + return 1; +} + +static const luaL_Reg base64_functions[] = { + { "encode", nlua_base64_encode }, + { "decode", nlua_base64_decode }, + { NULL, NULL }, +}; + +int luaopen_base64(lua_State *L) +{ + lua_newtable(L); + luaL_register(L, NULL, base64_functions); + return 1; +} diff --git a/src/nvim/lua/base64.h b/src/nvim/lua/base64.h new file mode 100644 index 0000000000..570d9eb677 --- /dev/null +++ b/src/nvim/lua/base64.h @@ -0,0 +1,12 @@ +#ifndef NVIM_LUA_BASE64_H +#define NVIM_LUA_BASE64_H + +#include +#include +#include + +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "lua/base64.h.generated.h" +#endif + +#endif // NVIM_LUA_BASE64_H diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 14e9902ee2..60be771b6c 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -26,6 +26,7 @@ #include "nvim/ex_eval.h" #include "nvim/fold.h" #include "nvim/globals.h" +#include "nvim/lua/base64.h" #include "nvim/lua/converter.h" #include "nvim/lua/spell.h" #include "nvim/lua/stdlib.h" @@ -606,6 +607,10 @@ void nlua_state_add_stdlib(lua_State *const lstate, bool is_thread) lua_pushcfunction(lstate, &nlua_iconv); lua_setfield(lstate, -2, "iconv"); + // vim.base64 + luaopen_base64(lstate); + lua_setfield(lstate, -2, "base64"); + nlua_state_add_internal(lstate); } -- cgit From acc646ad8fc3ef11fcc63b69f3d8484e4a91accd Mon Sep 17 00:00:00 2001 From: dundargoc Date: Fri, 29 Sep 2023 14:58:48 +0200 Subject: refactor: the long goodbye long is 32 bits on windows, while it is 64 bits on other architectures. This makes the type suboptimal for a codebase meant to be cross-platform. Replace it with more appropriate integer types. --- src/nvim/lua/executor.c | 2 +- src/nvim/lua/spell.c | 2 +- src/nvim/lua/stdlib.c | 8 ++++---- src/nvim/lua/xdiff.c | 20 ++++++++++---------- 4 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 3975312703..b389f33d8d 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -619,7 +619,7 @@ static bool nlua_init_packages(lua_State *lstate, bool is_standalone) lua_getfield(lstate, -1, "preload"); // [package, preload] for (size_t i = 0; i < ARRAY_SIZE(builtin_modules); i++) { ModuleDef def = builtin_modules[i]; - lua_pushinteger(lstate, (long)i); // [package, preload, i] + lua_pushinteger(lstate, (lua_Integer)i); // [package, preload, i] lua_pushcclosure(lstate, nlua_module_preloader, 1); // [package, preload, cclosure] lua_setfield(lstate, -2, def.name); // [package, preload] diff --git a/src/nvim/lua/spell.c b/src/nvim/lua/spell.c index 04304719d0..8f80744ac8 100644 --- a/src/nvim/lua/spell.c +++ b/src/nvim/lua/spell.c @@ -80,7 +80,7 @@ int nlua_spell_check(lua_State *lstate) lua_rawseti(lstate, -2, 2); // +1 for 1-indexing - lua_pushinteger(lstate, (long)pos + 1); + lua_pushinteger(lstate, (lua_Integer)pos + 1); lua_rawseti(lstate, -2, 3); lua_rawseti(lstate, -2, ++no_res); diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 60be771b6c..606c9878e6 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -183,8 +183,8 @@ int nlua_str_utfindex(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL size_t codepoints = 0, codeunits = 0; mb_utflen(s1, (size_t)idx, &codepoints, &codeunits); - lua_pushinteger(lstate, (long)codepoints); - lua_pushinteger(lstate, (long)codeunits); + lua_pushinteger(lstate, (lua_Integer)codepoints); + lua_pushinteger(lstate, (lua_Integer)codeunits); return 2; } @@ -204,7 +204,7 @@ static int nlua_str_utf_pos(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL size_t clen; for (size_t i = 0; i < s1_len && s1[i] != NUL; i += clen) { clen = (size_t)utf_ptr2len_len(s1 + i, (int)(s1_len - i)); - lua_pushinteger(lstate, (long)i + 1); + lua_pushinteger(lstate, (lua_Integer)i + 1); lua_rawseti(lstate, -2, (int)idx); idx++; } @@ -276,7 +276,7 @@ int nlua_str_byteindex(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL return luaL_error(lstate, "index out of range"); } - lua_pushinteger(lstate, (long)byteidx); + lua_pushinteger(lstate, (lua_Integer)byteidx); return 1; } diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 56d1fdb8ab..e131bbb586 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -64,12 +64,12 @@ static void lua_pushhunk(lua_State *lstate, long start_a, long count_a, long sta lua_rawseti(lstate, -2, (signed)lua_objlen(lstate, -2) + 1); } -static void get_linematch_results(lua_State *lstate, mmfile_t *ma, mmfile_t *mb, long start_a, - long count_a, long start_b, long count_b, bool iwhite) +static void get_linematch_results(lua_State *lstate, mmfile_t *ma, mmfile_t *mb, int start_a, + int count_a, int start_b, int count_b, bool iwhite) { // get the pointer to char of the start of the diff to pass it to linematch algorithm const char *diff_begin[2] = { ma->ptr, mb->ptr }; - int diff_length[2] = { (int)count_a, (int)count_b }; + int diff_length[2] = { count_a, count_b }; fastforward_buf_to_lnum(&diff_begin[0], (linenr_T)start_a + 1); fastforward_buf_to_lnum(&diff_begin[1], (linenr_T)start_b + 1); @@ -77,12 +77,12 @@ static void get_linematch_results(lua_State *lstate, mmfile_t *ma, mmfile_t *mb, int *decisions = NULL; size_t decisions_length = linematch_nbuffers(diff_begin, diff_length, 2, &decisions, iwhite); - long lnuma = start_a, lnumb = start_b; + int lnuma = start_a, lnumb = start_b; - long hunkstarta = lnuma; - long hunkstartb = lnumb; - long hunkcounta = 0; - long hunkcountb = 0; + int hunkstarta = lnuma; + int hunkstartb = lnumb; + int hunkcounta = 0; + int hunkcountb = 0; for (size_t i = 0; i < decisions_length; i++) { if (i && (decisions[i - 1] != decisions[i])) { lua_pushhunk(lstate, hunkstarta, hunkcounta, hunkstartb, hunkcountb); @@ -110,8 +110,8 @@ static int write_string(void *priv, mmbuffer_t *mb, int nbuf) { luaL_Buffer *buf = (luaL_Buffer *)priv; for (int i = 0; i < nbuf; i++) { - const long size = mb[i].size; - for (long total = 0; total < size; total += LUAL_BUFFERSIZE) { + const int size = mb[i].size; + for (int total = 0; total < size; total += LUAL_BUFFERSIZE) { const int tocopy = MIN((int)(size - total), LUAL_BUFFERSIZE); char *p = luaL_prepbuffer(buf); if (!p) { -- cgit From 08847a9ea15a50aba041ee621d71b9884f5fea97 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Wed, 8 Nov 2023 09:33:37 -0600 Subject: refactor: move defaults into separate module (#25929) Move default mappings and autocommands into a separate module and add comments and docstrings to document each of the defaults. --- src/nvim/lua/executor.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index b389f33d8d..36cb8a6756 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -2291,3 +2291,17 @@ plain: kv_printf(str, "", ref); return str.items; } + +/// Execute the vim._defaults module to set up default mappings and autocommands +void nlua_init_defaults(void) +{ + lua_State *const L = global_lstate; + assert(L); + + lua_getglobal(L, "require"); + lua_pushstring(L, "vim._defaults"); + if (nlua_pcall(L, 1, 0)) { + os_errmsg(lua_tostring(L, -1)); + os_errmsg("\n"); + } +} -- cgit From bb7324292cda2354511d3332aecb0b9748021764 Mon Sep 17 00:00:00 2001 From: Gregory Anders <8965202+gpanders@users.noreply.github.com> Date: Wed, 8 Nov 2023 12:16:45 -0600 Subject: fix: flush UI state before blocking in vim.wait (#25938) --- src/nvim/lua/executor.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 36cb8a6756..3333c73bfd 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -463,6 +463,9 @@ static int nlua_wait(lua_State *lstate) int pcall_status = 0; bool callback_result = false; + // Flush UI before blocking + ui_flush(); + LOOP_PROCESS_EVENTS_UNTIL(&main_loop, loop_events, (int)timeout, -- cgit From d5a85d737aa2a5c3a64ef0aa5b01672b7ed49c09 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 10 Nov 2023 15:24:36 +0800 Subject: fix(f_wait): flush UI before blocking (#25962) --- src/nvim/lua/executor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 3333c73bfd..876a5c34bd 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -463,7 +463,7 @@ static int nlua_wait(lua_State *lstate) int pcall_status = 0; bool callback_result = false; - // Flush UI before blocking + // Flush screen updates before blocking. ui_flush(); LOOP_PROCESS_EVENTS_UNTIL(&main_loop, -- cgit From c4ad15ae324f6460c683a64c44d65e693e1f39bb Mon Sep 17 00:00:00 2001 From: dundargoc Date: Sat, 11 Nov 2023 10:59:02 +0100 Subject: fix(PVS/V009): add top-level message --- src/nvim/lua/base64.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/base64.c b/src/nvim/lua/base64.c index 3f246839d5..b6382f6eab 100644 --- a/src/nvim/lua/base64.c +++ b/src/nvim/lua/base64.c @@ -1,3 +1,6 @@ +// This is an open source non-commercial project. Dear PVS-Studio, please check +// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com + #include #include #include -- cgit From 353a4be7e84fdc101318215bdcc8a7e780d737fe Mon Sep 17 00:00:00 2001 From: dundargoc Date: Sun, 12 Nov 2023 13:13:58 +0100 Subject: build: remove PVS We already have an extensive suite of static analysis tools we use, which causes a fair bit of redundancy as we get duplicate warnings. PVS is also prone to give false warnings which creates a lot of work to identify and disable. --- src/nvim/lua/base64.c | 3 --- src/nvim/lua/converter.c | 3 --- src/nvim/lua/executor.c | 3 --- src/nvim/lua/secure.c | 3 --- src/nvim/lua/spell.c | 3 --- src/nvim/lua/stdlib.c | 3 --- src/nvim/lua/treesitter.c | 3 --- src/nvim/lua/xdiff.c | 3 --- 8 files changed, 24 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/base64.c b/src/nvim/lua/base64.c index b6382f6eab..3f246839d5 100644 --- a/src/nvim/lua/base64.c +++ b/src/nvim/lua/base64.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - #include #include #include diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index c041fdb26c..fc39fb48b6 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - #include #include #include diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 876a5c34bd..716fe5e481 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - #include #include #include diff --git a/src/nvim/lua/secure.c b/src/nvim/lua/secure.c index 194c80fdc9..65c13f8872 100644 --- a/src/nvim/lua/secure.c +++ b/src/nvim/lua/secure.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - #include #include #include diff --git a/src/nvim/lua/spell.c b/src/nvim/lua/spell.c index 8f80744ac8..2575c3d95d 100644 --- a/src/nvim/lua/spell.c +++ b/src/nvim/lua/spell.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - #include #include #include diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 606c9878e6..5072d14c0e 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - #include #include #include diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 57469f6358..ff942630a0 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - // lua bindings for tree-sitter. // NB: this file mostly contains a generic lua interface for tree-sitter // trees and nodes, and could be broken out as a reusable lua package diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index e131bbb586..f3f78b79f5 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -1,6 +1,3 @@ -// This is an open source non-commercial project. Dear PVS-Studio, please check -// it. PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com - #include #include #include -- cgit From 4f8941c1a5f1ef6caa410feeb52e343db22763ce Mon Sep 17 00:00:00 2001 From: dundargoc Date: Fri, 10 Nov 2023 12:23:42 +0100 Subject: refactor: replace manual header guards with #pragma once It is less error-prone than manually defining header guards. Pretty much all compilers support it even if it's not part of the C standard. --- src/nvim/lua/base64.h | 5 +---- src/nvim/lua/converter.h | 4 +--- src/nvim/lua/executor.h | 5 +---- src/nvim/lua/secure.h | 5 +---- src/nvim/lua/spell.h | 5 +---- src/nvim/lua/stdlib.h | 5 +---- src/nvim/lua/treesitter.h | 5 +---- src/nvim/lua/xdiff.h | 5 +---- 8 files changed, 8 insertions(+), 31 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/base64.h b/src/nvim/lua/base64.h index 570d9eb677..feb409ef38 100644 --- a/src/nvim/lua/base64.h +++ b/src/nvim/lua/base64.h @@ -1,5 +1,4 @@ -#ifndef NVIM_LUA_BASE64_H -#define NVIM_LUA_BASE64_H +#pragma once #include #include @@ -8,5 +7,3 @@ #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/base64.h.generated.h" #endif - -#endif // NVIM_LUA_BASE64_H diff --git a/src/nvim/lua/converter.h b/src/nvim/lua/converter.h index d1d6b9d62a..501db36dbe 100644 --- a/src/nvim/lua/converter.h +++ b/src/nvim/lua/converter.h @@ -1,5 +1,4 @@ -#ifndef NVIM_LUA_CONVERTER_H -#define NVIM_LUA_CONVERTER_H +#pragma once #include #include @@ -16,4 +15,3 @@ #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/converter.h.generated.h" #endif -#endif // NVIM_LUA_CONVERTER_H diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index 4b4d1dded8..91775e0322 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -1,5 +1,4 @@ -#ifndef NVIM_LUA_EXECUTOR_H -#define NVIM_LUA_EXECUTOR_H +#pragma once #include #include @@ -47,5 +46,3 @@ typedef struct { EXTERN nlua_ref_state_t *nlua_global_refs INIT( = NULL); EXTERN bool nlua_disable_preload INIT( = false); - -#endif // NVIM_LUA_EXECUTOR_H diff --git a/src/nvim/lua/secure.h b/src/nvim/lua/secure.h index 87c468538e..b363a243a5 100644 --- a/src/nvim/lua/secure.h +++ b/src/nvim/lua/secure.h @@ -1,5 +1,4 @@ -#ifndef NVIM_LUA_SECURE_H -#define NVIM_LUA_SECURE_H +#pragma once #include @@ -8,5 +7,3 @@ #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/secure.h.generated.h" #endif - -#endif // NVIM_LUA_SECURE_H diff --git a/src/nvim/lua/spell.h b/src/nvim/lua/spell.h index 8f798a5191..adf3e9dd60 100644 --- a/src/nvim/lua/spell.h +++ b/src/nvim/lua/spell.h @@ -1,5 +1,4 @@ -#ifndef NVIM_LUA_SPELL_H -#define NVIM_LUA_SPELL_H +#pragma once #include #include @@ -8,5 +7,3 @@ #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/spell.h.generated.h" #endif - -#endif // NVIM_LUA_SPELL_H diff --git a/src/nvim/lua/stdlib.h b/src/nvim/lua/stdlib.h index 17aec6714d..b2be54af90 100644 --- a/src/nvim/lua/stdlib.h +++ b/src/nvim/lua/stdlib.h @@ -1,10 +1,7 @@ -#ifndef NVIM_LUA_STDLIB_H -#define NVIM_LUA_STDLIB_H +#pragma once #include #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/stdlib.h.generated.h" #endif - -#endif // NVIM_LUA_STDLIB_H diff --git a/src/nvim/lua/treesitter.h b/src/nvim/lua/treesitter.h index b69fb9dfae..c1c78b8a6a 100644 --- a/src/nvim/lua/treesitter.h +++ b/src/nvim/lua/treesitter.h @@ -1,5 +1,4 @@ -#ifndef NVIM_LUA_TREESITTER_H -#define NVIM_LUA_TREESITTER_H +#pragma once #include #include @@ -10,5 +9,3 @@ #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/treesitter.h.generated.h" #endif - -#endif // NVIM_LUA_TREESITTER_H diff --git a/src/nvim/lua/xdiff.h b/src/nvim/lua/xdiff.h index b172d2f922..9cb5cabbfb 100644 --- a/src/nvim/lua/xdiff.h +++ b/src/nvim/lua/xdiff.h @@ -1,5 +1,4 @@ -#ifndef NVIM_LUA_XDIFF_H -#define NVIM_LUA_XDIFF_H +#pragma once #include #include @@ -8,5 +7,3 @@ #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/xdiff.h.generated.h" #endif - -#endif // NVIM_LUA_XDIFF_H -- cgit From 22eb2ba18336df6cd70a88f666818ee5d8ba92d2 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Mon, 13 Nov 2023 13:39:04 -0600 Subject: fix(lua): do not schedule events if Nvim is exiting If Nvim is in the process of exiting then we do not want to allocate any new refs onto the event loop, because they will not be freed and will result in a memory leak. --- src/nvim/lua/executor.c | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 716fe5e481..aed96a539a 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -366,6 +366,12 @@ static void nlua_schedule_event(void **argv) static int nlua_schedule(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL { + // If Nvim is exiting don't schedule tasks to run in the future. Any refs + // allocated here will not be cleaned up otherwise + if (exiting) { + return 0; + } + if (lua_type(lstate, 1) != LUA_TFUNCTION) { lua_pushliteral(lstate, "vim.schedule: expected function"); return lua_error(lstate); -- cgit From bb4b4576e384c71890b4df4fa4f1ae76fad3a59d Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 16 Nov 2023 10:55:54 +0800 Subject: refactor: iwyu (#26062) --- src/nvim/lua/base64.c | 5 +++++ src/nvim/lua/xdiff.c | 1 + 2 files changed, 6 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/base64.c b/src/nvim/lua/base64.c index 3f246839d5..c1f43a37d7 100644 --- a/src/nvim/lua/base64.c +++ b/src/nvim/lua/base64.c @@ -1,11 +1,16 @@ #include #include #include +#include #include "nvim/base64.h" #include "nvim/lua/base64.h" #include "nvim/memory.h" +#ifdef INCLUDE_GENERATED_DECLARATIONS +# include "lua/base64.c.generated.h" +#endif + static int nlua_base64_encode(lua_State *L) { if (lua_gettop(L) < 1) { diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index f3f78b79f5..29e3bbefd0 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -13,6 +13,7 @@ #include "nvim/lua/xdiff.h" #include "nvim/macros.h" #include "nvim/memory.h" +#include "nvim/pos.h" #include "nvim/vim.h" #include "xdiff/xdiff.h" -- cgit From 20ec4c776a07492c2e3b995e10b40b1cdb52bc7a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 17 Nov 2023 18:34:48 +0800 Subject: fix(lua): only disable vim.schedule() when closing main loop (#26090) --- src/nvim/lua/executor.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index aed96a539a..12304b6f11 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -366,17 +366,17 @@ static void nlua_schedule_event(void **argv) static int nlua_schedule(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL { - // If Nvim is exiting don't schedule tasks to run in the future. Any refs - // allocated here will not be cleaned up otherwise - if (exiting) { - return 0; - } - if (lua_type(lstate, 1) != LUA_TFUNCTION) { lua_pushliteral(lstate, "vim.schedule: expected function"); return lua_error(lstate); } + // If main_loop is closing don't schedule tasks to run in the future, + // otherwise any refs allocated here will not be cleaned up. + if (main_loop.closing) { + return 0; + } + LuaRef cb = nlua_ref_global(lstate, 1); multiqueue_put(main_loop.events, nlua_schedule_event, -- cgit From b522cb1ac3fbdf6e68eed5d0b6e1cbeaf3ac2254 Mon Sep 17 00:00:00 2001 From: bfredl Date: Mon, 6 Nov 2023 14:52:27 +0100 Subject: refactor(grid): make screen rendering more multibyte than ever before Problem: buffer text with composing chars are converted from UTF-8 to an array of up to seven UTF-32 values and then converted back to UTF-8 strings. Solution: Convert buffer text directly to UTF-8 based schar_T values. The limit of the text size is now in schar_T bytes, which is currently 31+1 but easily could be raised as it no longer multiplies the size of the entire screen grid when not used, the full size is only required for temporary scratch buffers. Also does some general cleanup to win_line text handling, which was unnecessarily complicated due to multibyte rendering being an "opt-in" feature long ago. Nowadays, a char is just a char, regardless if it consists of one ASCII byte or multiple bytes. --- src/nvim/lua/stdlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 5072d14c0e..a200b0a32f 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -224,7 +224,7 @@ static int nlua_str_utf_start(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL if (offset < 0 || offset > (intptr_t)s1_len) { return luaL_error(lstate, "index out of range"); } - int head_offset = utf_cp_head_off(s1, s1 + offset - 1); + int head_offset = -utf_cp_head_off(s1, s1 + offset - 1); lua_pushinteger(lstate, head_offset); return 1; } -- cgit From 1798a4b5e9f0ae56cd800095f79423fea5cae8ca Mon Sep 17 00:00:00 2001 From: dundargoc Date: Thu, 16 Nov 2023 10:59:11 +0100 Subject: build: bump uncrustify version Biggest change is that uncrustify is silent during linting. --- src/nvim/lua/executor.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 12304b6f11..c61569eb9a 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -474,7 +474,8 @@ static int nlua_wait(lua_State *lstate) (int)timeout, got_int || (is_function ? nlua_wait_condition(lstate, &pcall_status, - &callback_result) : false)); + &callback_result) : + false)); // Stop dummy timer time_watcher_stop(tw); -- cgit From a6e3d93421ba13c407a96fac9cc01fa41ec7ad98 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Thu, 16 Nov 2023 10:59:11 +0100 Subject: refactor: enable formatting for ternaries This requires removing the "Inner expression should be aligned" rule from clint as it prevents essentially any formatting regarding ternary operators. --- src/nvim/lua/executor.c | 6 +++--- src/nvim/lua/spell.c | 10 +++++----- src/nvim/lua/xdiff.c | 10 +++++----- 3 files changed, 13 insertions(+), 13 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index c61569eb9a..ce2a247a6f 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -450,7 +450,7 @@ static int nlua_wait(lua_State *lstate) } MultiQueue *loop_events = fast_only || in_fast_callback > 0 - ? main_loop.fast_events : main_loop.events; + ? main_loop.fast_events : main_loop.events; TimeWatcher *tw = xmalloc(sizeof(TimeWatcher)); @@ -474,8 +474,8 @@ static int nlua_wait(lua_State *lstate) (int)timeout, got_int || (is_function ? nlua_wait_condition(lstate, &pcall_status, - &callback_result) : - false)); + &callback_result) + : false)); // Stop dummy timer time_watcher_stop(tw); diff --git a/src/nvim/lua/spell.c b/src/nvim/lua/spell.c index 2575c3d95d..8f4a684219 100644 --- a/src/nvim/lua/spell.c +++ b/src/nvim/lua/spell.c @@ -65,11 +65,11 @@ int nlua_spell_check(lua_State *lstate) lua_pushlstring(lstate, str, len); lua_rawseti(lstate, -2, 1); - result = attr == HLF_SPB ? "bad" : - attr == HLF_SPR ? "rare" : - attr == HLF_SPL ? "local" : - attr == HLF_SPC ? "caps" : - NULL; + result = attr == HLF_SPB + ? "bad" : (attr == HLF_SPR + ? "rare" : (attr == HLF_SPL + ? "local" : (attr == HLF_SPC + ? "caps" : NULL))); assert(result != NULL); diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 29e3bbefd0..bf52ae4232 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -192,11 +192,11 @@ static bool check_xdiff_opt(ObjectType actType, ObjectType expType, const char * { if (actType != expType) { const char *type_str = - expType == kObjectTypeString ? "string" : - expType == kObjectTypeInteger ? "integer" : - expType == kObjectTypeBoolean ? "boolean" : - expType == kObjectTypeLuaRef ? "function" : - "NA"; + expType == kObjectTypeString + ? "string" : (expType == kObjectTypeInteger + ? "integer" : (expType == kObjectTypeBoolean + ? "boolean" : (expType == kObjectTypeLuaRef + ? "function" : "NA"))); api_set_error(err, kErrorTypeValidation, "%s is not a %s", name, type_str); -- cgit From a827003e3052c6d9ee7bdb71518182e9bd76317d Mon Sep 17 00:00:00 2001 From: dundargoc Date: Sat, 25 Nov 2023 11:32:32 +0100 Subject: build: rework IWYU mapping files Create mapping to most of the C spec and some POSIX specific functions. This is more robust than relying files shipped with IWYU. --- src/nvim/lua/stdlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index a200b0a32f..c1e0af5aa1 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -5,8 +5,8 @@ #include #include #include +#include #include -#include #ifdef NVIM_VENDOR_BIT # include "bit.h" -- cgit From 7e2387f41be7cd8304fe48bfa089f2bea155dd5a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 27 Nov 2023 08:34:06 +0800 Subject: build(clint): more precise check for "defs" headers (#26236) --- src/nvim/lua/converter.h | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.h b/src/nvim/lua/converter.h index 501db36dbe..a502df80d9 100644 --- a/src/nvim/lua/converter.h +++ b/src/nvim/lua/converter.h @@ -1,12 +1,9 @@ #pragma once -#include -#include -#include +#include // IWYU pragma: keep -#include "nvim/api/private/defs.h" -#include "nvim/eval/typval_defs.h" -#include "nvim/func_attr.h" +#include "nvim/api/private/defs.h" // IWYU pragma: keep +#include "nvim/eval/typval_defs.h" // IWYU pragma: keep #define nlua_pop_Buffer nlua_pop_handle #define nlua_pop_Window nlua_pop_handle -- cgit From 84bbe4b0ca935db1f6202db339aee5594a3b3908 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Tue, 21 Nov 2023 11:24:30 +0000 Subject: fix(lua): disallow vim.wait() in fast contexts `vim.wait()` cannot be called in a fast callback since the main loop cannot be run in that context as it is not reentrant Fixes #26122 --- src/nvim/lua/executor.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index ce2a247a6f..04eda99d17 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -411,6 +411,10 @@ static bool nlua_wait_condition(lua_State *lstate, int *status, bool *callback_r static int nlua_wait(lua_State *lstate) FUNC_ATTR_NONNULL_ALL { + if (in_fast_callback) { + return luaL_error(lstate, e_luv_api_disabled, "vim.wait"); + } + intptr_t timeout = luaL_checkinteger(lstate, 1); if (timeout < 0) { return luaL_error(lstate, "timeout must be >= 0"); @@ -449,8 +453,7 @@ static int nlua_wait(lua_State *lstate) fast_only = lua_toboolean(lstate, 4); } - MultiQueue *loop_events = fast_only || in_fast_callback > 0 - ? main_loop.fast_events : main_loop.events; + MultiQueue *loop_events = fast_only ? main_loop.fast_events : main_loop.events; TimeWatcher *tw = xmalloc(sizeof(TimeWatcher)); -- cgit From 574d25642fc9ca65b396633aeab6e2d32778b642 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 27 Nov 2023 17:21:58 +0800 Subject: refactor: move Arena and ArenaMem to memory_defs.h (#26240) --- src/nvim/lua/base64.h | 4 +--- src/nvim/lua/secure.h | 4 +--- src/nvim/lua/spell.h | 4 +--- src/nvim/lua/stdlib.h | 2 +- src/nvim/lua/treesitter.c | 2 +- src/nvim/lua/treesitter.h | 6 +----- src/nvim/lua/xdiff.h | 4 +--- 7 files changed, 7 insertions(+), 19 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/base64.h b/src/nvim/lua/base64.h index feb409ef38..3c95968cda 100644 --- a/src/nvim/lua/base64.h +++ b/src/nvim/lua/base64.h @@ -1,8 +1,6 @@ #pragma once -#include -#include -#include +#include // IWYU pragma: keep #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/base64.h.generated.h" diff --git a/src/nvim/lua/secure.h b/src/nvim/lua/secure.h index b363a243a5..c69c0d4f8f 100644 --- a/src/nvim/lua/secure.h +++ b/src/nvim/lua/secure.h @@ -1,8 +1,6 @@ #pragma once -#include - -#include "nvim/ex_cmds_defs.h" +#include "nvim/ex_cmds_defs.h" // IWYU pragma: keep #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/secure.h.generated.h" diff --git a/src/nvim/lua/spell.h b/src/nvim/lua/spell.h index adf3e9dd60..6f1b322e5b 100644 --- a/src/nvim/lua/spell.h +++ b/src/nvim/lua/spell.h @@ -1,8 +1,6 @@ #pragma once -#include -#include -#include +#include // IWYU pragma: keep #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/spell.h.generated.h" diff --git a/src/nvim/lua/stdlib.h b/src/nvim/lua/stdlib.h index b2be54af90..26e96055ae 100644 --- a/src/nvim/lua/stdlib.h +++ b/src/nvim/lua/stdlib.h @@ -1,6 +1,6 @@ #pragma once -#include +#include // IWYU pragma: keep #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/stdlib.h.generated.h" diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index ff942630a0..e5de369ed9 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include "klib/kvec.h" @@ -26,7 +27,6 @@ #include "nvim/pos.h" #include "nvim/strings.h" #include "nvim/types.h" -#include "tree_sitter/api.h" #define TS_META_PARSER "treesitter_parser" #define TS_META_TREE "treesitter_tree" diff --git a/src/nvim/lua/treesitter.h b/src/nvim/lua/treesitter.h index c1c78b8a6a..4ef9a10602 100644 --- a/src/nvim/lua/treesitter.h +++ b/src/nvim/lua/treesitter.h @@ -1,10 +1,6 @@ #pragma once -#include -#include -#include - -#include "tree_sitter/api.h" +#include // IWYU pragma: keep #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/treesitter.h.generated.h" diff --git a/src/nvim/lua/xdiff.h b/src/nvim/lua/xdiff.h index 9cb5cabbfb..2ea74a79e8 100644 --- a/src/nvim/lua/xdiff.h +++ b/src/nvim/lua/xdiff.h @@ -1,8 +1,6 @@ #pragma once -#include -#include -#include +#include // IWYU pragma: keep #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/xdiff.h.generated.h" -- cgit From 2c16c6a6c42f46e290df5441c37572f296aeb09f Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Mon, 27 Nov 2023 10:43:13 +0100 Subject: docs: small fixes (#26154) --- src/nvim/lua/executor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 04eda99d17..9ff217af84 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -115,7 +115,7 @@ lua_State *get_global_lstate(void) /// Convert lua error into a Vim error message /// /// @param lstate Lua interpreter state. -/// @param[in] msg Message base, must contain one `%s`. +/// @param[in] msg Message base, must contain one `%*s`. void nlua_error(lua_State *const lstate, const char *const msg) FUNC_ATTR_NONNULL_ALL { -- cgit From 40139738eb479d0913ec6ce751ca5adfa50ad8c3 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Sun, 26 Nov 2023 21:36:02 +0100 Subject: build: enable IWYU on mac --- src/nvim/lua/stdlib.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index c1e0af5aa1..a200b0a32f 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -5,8 +5,8 @@ #include #include #include -#include #include +#include #ifdef NVIM_VENDOR_BIT # include "bit.h" -- cgit From 8b428ca8b79ebb7b36c3e403ff3bcb6924a635a6 Mon Sep 17 00:00:00 2001 From: dundargoc Date: Mon, 27 Nov 2023 16:00:21 +0100 Subject: build(IWYU): fix includes for func_attr.h --- src/nvim/lua/converter.c | 1 + src/nvim/lua/stdlib.c | 1 + 2 files changed, 2 insertions(+) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index fc39fb48b6..ed8cc3a612 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -19,6 +19,7 @@ #include "nvim/eval/typval_defs.h" #include "nvim/eval/typval_encode.h" #include "nvim/eval/userfunc.h" +#include "nvim/func_attr.h" #include "nvim/gettext.h" #include "nvim/lua/converter.h" #include "nvim/lua/executor.h" diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index a200b0a32f..dafc24fea1 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -22,6 +22,7 @@ #include "nvim/eval/vars.h" #include "nvim/ex_eval.h" #include "nvim/fold.h" +#include "nvim/func_attr.h" #include "nvim/globals.h" #include "nvim/lua/base64.h" #include "nvim/lua/converter.h" -- cgit From f4aedbae4cb1f206f5b7c6142697b71dd473059b Mon Sep 17 00:00:00 2001 From: dundargoc Date: Mon, 27 Nov 2023 18:39:38 +0100 Subject: build(IWYU): fix includes for undo_defs.h --- src/nvim/lua/executor.c | 2 +- src/nvim/lua/stdlib.c | 2 +- src/nvim/lua/treesitter.c | 2 +- src/nvim/lua/xdiff.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 9ff217af84..7e35296995 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -51,7 +51,7 @@ #include "nvim/os/fileio.h" #include "nvim/os/os.h" #include "nvim/path.h" -#include "nvim/pos.h" +#include "nvim/pos_defs.h" #include "nvim/profile.h" #include "nvim/runtime.h" #include "nvim/strings.h" diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index dafc24fea1..c0815133aa 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -33,7 +33,7 @@ #include "nvim/mbyte.h" #include "nvim/memline.h" #include "nvim/memory.h" -#include "nvim/pos.h" +#include "nvim/pos_defs.h" #include "nvim/regexp.h" #include "nvim/runtime.h" #include "nvim/types.h" diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index e5de369ed9..90767e0c50 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -24,7 +24,7 @@ #include "nvim/map.h" #include "nvim/memline.h" #include "nvim/memory.h" -#include "nvim/pos.h" +#include "nvim/pos_defs.h" #include "nvim/strings.h" #include "nvim/types.h" diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index bf52ae4232..6ce88400d1 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -13,7 +13,7 @@ #include "nvim/lua/xdiff.h" #include "nvim/macros.h" #include "nvim/memory.h" -#include "nvim/pos.h" +#include "nvim/pos_defs.h" #include "nvim/vim.h" #include "xdiff/xdiff.h" -- cgit From 6c14ae6bfaf51415b555e9a6b85d1d280976358d Mon Sep 17 00:00:00 2001 From: dundargoc Date: Mon, 27 Nov 2023 20:27:32 +0100 Subject: refactor: rename types.h to types_defs.h --- src/nvim/lua/converter.c | 2 +- src/nvim/lua/executor.h | 2 +- src/nvim/lua/stdlib.c | 2 +- src/nvim/lua/treesitter.c | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index ed8cc3a612..ca4812597e 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -25,7 +25,7 @@ #include "nvim/lua/executor.h" #include "nvim/macros.h" #include "nvim/message.h" -#include "nvim/types.h" +#include "nvim/types_defs.h" #include "nvim/vim.h" /// Determine, which keys lua table contains diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index 91775e0322..6caad4bd7a 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -14,7 +14,7 @@ #include "nvim/lua/converter.h" #include "nvim/macros.h" #include "nvim/map.h" -#include "nvim/types.h" +#include "nvim/types_defs.h" #include "nvim/usercmd.h" // Generated by msgpack-gen.lua diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index c0815133aa..14658a6bc1 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -36,7 +36,7 @@ #include "nvim/pos_defs.h" #include "nvim/regexp.h" #include "nvim/runtime.h" -#include "nvim/types.h" +#include "nvim/types_defs.h" #include "nvim/vim.h" #ifdef INCLUDE_GENERATED_DECLARATIONS diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 90767e0c50..fcdd0540e0 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -26,7 +26,7 @@ #include "nvim/memory.h" #include "nvim/pos_defs.h" #include "nvim/strings.h" -#include "nvim/types.h" +#include "nvim/types_defs.h" #define TS_META_PARSER "treesitter_parser" #define TS_META_TREE "treesitter_tree" -- cgit From 79b6ff28ad1204fbb4199b9092f5c578d88cb28e Mon Sep 17 00:00:00 2001 From: dundargoc Date: Tue, 28 Nov 2023 20:31:00 +0100 Subject: refactor: fix headers with IWYU --- src/nvim/lua/converter.c | 6 +++--- src/nvim/lua/executor.c | 6 +++--- src/nvim/lua/executor.h | 6 +++--- src/nvim/lua/spell.c | 2 +- src/nvim/lua/stdlib.c | 6 +++--- src/nvim/lua/treesitter.c | 4 ++-- src/nvim/lua/xdiff.c | 4 ++-- 7 files changed, 17 insertions(+), 17 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index ca4812597e..4c6e38ca57 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -13,7 +13,7 @@ // FIXME: vim.h is not actually needed, but otherwise it states MAXPATHL is // redefined #include "klib/kvec.h" -#include "nvim/ascii.h" +#include "nvim/ascii_defs.h" #include "nvim/eval/decode.h" #include "nvim/eval/typval.h" #include "nvim/eval/typval_defs.h" @@ -23,10 +23,10 @@ #include "nvim/gettext.h" #include "nvim/lua/converter.h" #include "nvim/lua/executor.h" -#include "nvim/macros.h" +#include "nvim/macros_defs.h" #include "nvim/message.h" #include "nvim/types_defs.h" -#include "nvim/vim.h" +#include "nvim/vim_defs.h" /// Determine, which keys lua table contains typedef struct { diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 7e35296995..06d16efb05 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -15,7 +15,7 @@ #include "nvim/api/extmark.h" #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" -#include "nvim/ascii.h" +#include "nvim/ascii_defs.h" #include "nvim/change.h" #include "nvim/cmdexpand_defs.h" #include "nvim/cursor.h" @@ -41,7 +41,7 @@ #include "nvim/lua/executor.h" #include "nvim/lua/stdlib.h" #include "nvim/lua/treesitter.h" -#include "nvim/macros.h" +#include "nvim/macros_defs.h" #include "nvim/main.h" #include "nvim/memline.h" #include "nvim/memory.h" @@ -58,7 +58,7 @@ #include "nvim/ui.h" #include "nvim/undo.h" #include "nvim/usercmd.h" -#include "nvim/vim.h" +#include "nvim/vim_defs.h" #include "nvim/window.h" static int in_fast_callback = 0; diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index 6caad4bd7a..b38faddbb3 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -6,14 +6,14 @@ #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" -#include "nvim/assert.h" +#include "nvim/assert_defs.h" #include "nvim/cmdexpand_defs.h" #include "nvim/eval/typval_defs.h" #include "nvim/ex_cmds_defs.h" #include "nvim/func_attr.h" #include "nvim/lua/converter.h" -#include "nvim/macros.h" -#include "nvim/map.h" +#include "nvim/macros_defs.h" +#include "nvim/map_defs.h" #include "nvim/types_defs.h" #include "nvim/usercmd.h" diff --git a/src/nvim/lua/spell.c b/src/nvim/lua/spell.c index 8f4a684219..c261c5105e 100644 --- a/src/nvim/lua/spell.c +++ b/src/nvim/lua/spell.c @@ -5,7 +5,7 @@ #include #include -#include "nvim/ascii.h" +#include "nvim/ascii_defs.h" #include "nvim/buffer_defs.h" #include "nvim/gettext.h" #include "nvim/globals.h" diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 14658a6bc1..9c46179269 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -16,7 +16,7 @@ #include "mpack/lmpack.h" #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" -#include "nvim/ascii.h" +#include "nvim/ascii_defs.h" #include "nvim/buffer_defs.h" #include "nvim/eval/typval.h" #include "nvim/eval/vars.h" @@ -29,7 +29,7 @@ #include "nvim/lua/spell.h" #include "nvim/lua/stdlib.h" #include "nvim/lua/xdiff.h" -#include "nvim/map.h" +#include "nvim/map_defs.h" #include "nvim/mbyte.h" #include "nvim/memline.h" #include "nvim/memory.h" @@ -37,7 +37,7 @@ #include "nvim/regexp.h" #include "nvim/runtime.h" #include "nvim/types_defs.h" -#include "nvim/vim.h" +#include "nvim/vim_defs.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/stdlib.c.generated.h" diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index fcdd0540e0..008b3f2e95 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -20,8 +20,8 @@ #include "nvim/buffer_defs.h" #include "nvim/globals.h" #include "nvim/lua/treesitter.h" -#include "nvim/macros.h" -#include "nvim/map.h" +#include "nvim/macros_defs.h" +#include "nvim/map_defs.h" #include "nvim/memline.h" #include "nvim/memory.h" #include "nvim/pos_defs.h" diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 6ce88400d1..93c8933649 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -11,10 +11,10 @@ #include "nvim/lua/converter.h" #include "nvim/lua/executor.h" #include "nvim/lua/xdiff.h" -#include "nvim/macros.h" +#include "nvim/macros_defs.h" #include "nvim/memory.h" #include "nvim/pos_defs.h" -#include "nvim/vim.h" +#include "nvim/vim_defs.h" #include "xdiff/xdiff.h" #define COMPARED_BUFFER0 (1 << 0) -- cgit From a6cba103cebce535279db197f9efeb34e9d1171f Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 29 Nov 2023 20:32:40 +0800 Subject: refactor: move some constants out of vim_defs.h (#26298) --- src/nvim/lua/converter.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 4c6e38ca57..4598d48c4a 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -7,12 +7,9 @@ #include #include +#include "klib/kvec.h" #include "nvim/api/private/defs.h" #include "nvim/api/private/helpers.h" -#include "nvim/memory.h" -// FIXME: vim.h is not actually needed, but otherwise it states MAXPATHL is -// redefined -#include "klib/kvec.h" #include "nvim/ascii_defs.h" #include "nvim/eval/decode.h" #include "nvim/eval/typval.h" @@ -24,6 +21,7 @@ #include "nvim/lua/converter.h" #include "nvim/lua/executor.h" #include "nvim/macros_defs.h" +#include "nvim/memory.h" #include "nvim/message.h" #include "nvim/types_defs.h" #include "nvim/vim_defs.h" -- cgit From 86cc791debba09c8ed1aa0d863be844108866a38 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 29 Nov 2023 23:10:21 +0800 Subject: refactor: move function macros out of vim_defs.h (#26300) --- src/nvim/lua/stdlib.c | 2 +- src/nvim/lua/xdiff.c | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nvim/lua') diff --git a/src/nvim/lua/stdlib.c b/src/nvim/lua/stdlib.c index 9c46179269..33770b2e62 100644 --- a/src/nvim/lua/stdlib.c +++ b/src/nvim/lua/stdlib.c @@ -36,8 +36,8 @@ #include "nvim/pos_defs.h" #include "nvim/regexp.h" #include "nvim/runtime.h" +#include "nvim/strings.h" #include "nvim/types_defs.h" -#include "nvim/vim_defs.h" #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/stdlib.c.generated.h" diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index 93c8933649..16c3aa5e11 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.c @@ -14,7 +14,6 @@ #include "nvim/macros_defs.h" #include "nvim/memory.h" #include "nvim/pos_defs.h" -#include "nvim/vim_defs.h" #include "xdiff/xdiff.h" #define COMPARED_BUFFER0 (1 << 0) -- cgit