diff options
Diffstat (limited to 'src/nvim/lua')
-rw-r--r-- | src/nvim/lua/converter.c | 52 | ||||
-rw-r--r-- | src/nvim/lua/converter.h | 2 | ||||
-rw-r--r-- | src/nvim/lua/executor.c | 10 | ||||
-rw-r--r-- | src/nvim/lua/executor.h | 16 | ||||
-rw-r--r-- | src/nvim/lua/treesitter.c | 14 | ||||
-rw-r--r-- | src/nvim/lua/treesitter.h | 2 | ||||
-rw-r--r-- | src/nvim/lua/vim.lua | 54 | ||||
-rw-r--r-- | src/nvim/lua/xdiff.c | 21 | ||||
-rw-r--r-- | src/nvim/lua/xdiff.h | 2 |
9 files changed, 112 insertions, 61 deletions
diff --git a/src/nvim/lua/converter.c b/src/nvim/lua/converter.c index 07c53299fa..ca3c4f604a 100644 --- a/src/nvim/lua/converter.c +++ b/src/nvim/lua/converter.c @@ -755,7 +755,7 @@ void nlua_push_Array(lua_State *lstate, const Array array, bool special) FUNC_ATTR_NONNULL_ALL \ { \ lua_pushnumber(lstate, (lua_Number)(item)); \ -} + } GENERATE_INDEX_FUNCTION(Buffer) GENERATE_INDEX_FUNCTION(Window) @@ -1126,9 +1126,9 @@ Object nlua_pop_Object(lua_State *const lstate, bool ref, Error *const err) size_t len; const char *s = lua_tolstring(lstate, -1, &len); *cur.obj = STRING_OBJ(((String) { - .data = xmemdupz(s, len), - .size = len, - })); + .data = xmemdupz(s, len), + .size = len, + })); break; } case LUA_TNUMBER: { @@ -1147,10 +1147,10 @@ Object nlua_pop_Object(lua_State *const lstate, bool ref, Error *const err) switch (table_props.type) { case kObjectTypeArray: *cur.obj = ARRAY_OBJ(((Array) { - .items = NULL, - .size = 0, - .capacity = 0, - })); + .items = NULL, + .size = 0, + .capacity = 0, + })); if (table_props.maxidx != 0) { cur.obj->data.array.items = xcalloc(table_props.maxidx, @@ -1162,10 +1162,10 @@ Object nlua_pop_Object(lua_State *const lstate, bool ref, Error *const err) break; case kObjectTypeDictionary: *cur.obj = DICTIONARY_OBJ(((Dictionary) { - .items = NULL, - .size = 0, - .capacity = 0, - })); + .items = NULL, + .size = 0, + .capacity = 0, + })); if (table_props.string_keys_num != 0) { cur.obj->data.dictionary.items = xcalloc(table_props.string_keys_num, @@ -1299,3 +1299,31 @@ 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) +{ + if (!lua_istable(L, -1)) { + api_set_error(err, kErrorTypeValidation, "Expected lua table"); + lua_pop(L, -1); + return; + } + + lua_pushnil(L); // [dict, nil] + while (lua_next(L, -2)) { + // [dict, key, value] + size_t len; + const char *s = lua_tolstring(L, -2, &len); + Object *field = hashy(retval, 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); + } + // [dict] + lua_pop(L, 1); + // [] +} diff --git a/src/nvim/lua/converter.h b/src/nvim/lua/converter.h index 43a7e06019..1c9e60e4b2 100644 --- a/src/nvim/lua/converter.h +++ b/src/nvim/lua/converter.h @@ -6,8 +6,8 @@ #include <stdint.h> #include "nvim/api/private/defs.h" -#include "nvim/func_attr.h" #include "nvim/eval.h" +#include "nvim/func_attr.h" typedef struct { LuaRef func_ref; diff --git a/src/nvim/lua/executor.c b/src/nvim/lua/executor.c index 8c7dc90111..59ebafd9c8 100644 --- a/src/nvim/lua/executor.c +++ b/src/nvim/lua/executor.c @@ -5,6 +5,7 @@ #include <lua.h> #include <lualib.h> +#include "cjson/lua_cjson.h" #include "luv/luv.h" #include "mpack/lmpack.h" #include "nvim/api/private/defs.h" @@ -531,6 +532,9 @@ static int nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL lua_pushcfunction(lstate, &nlua_xdl_diff); lua_setfield(lstate, -2, "diff"); + lua_cjson_new(lstate); + lua_setfield(lstate, -2, "json"); + lua_setglobal(lstate, "vim"); { @@ -786,7 +790,7 @@ int nlua_call(lua_State *lstate) Error err = ERROR_INIT; size_t name_len; const char_u *name = (const char_u *)luaL_checklstring(lstate, 1, &name_len); - if (!nlua_is_deferred_safe(lstate)) { + if (!nlua_is_deferred_safe()) { return luaL_error(lstate, e_luv_api_disabled, "vimL function"); } @@ -842,7 +846,7 @@ free_vim_args: static int nlua_rpcrequest(lua_State *lstate) { - if (!nlua_is_deferred_safe(lstate)) { + if (!nlua_is_deferred_safe()) { return luaL_error(lstate, e_luv_api_disabled, "rpcrequest"); } return nlua_rpc(lstate, true); @@ -1312,7 +1316,7 @@ Object nlua_call_ref(LuaRef ref, const char *name, Array args, bool retval, Erro /// check if the current execution context is safe for calling deferred API /// methods. Luv callbacks are unsafe as they are called inside the uv loop. -bool nlua_is_deferred_safe(lua_State *lstate) +bool nlua_is_deferred_safe(void) { return in_fast_callback == 0; } diff --git a/src/nvim/lua/executor.h b/src/nvim/lua/executor.h index ea774ac2e3..a1f66bd02b 100644 --- a/src/nvim/lua/executor.h +++ b/src/nvim/lua/executor.h @@ -1,13 +1,13 @@ #ifndef NVIM_LUA_EXECUTOR_H #define NVIM_LUA_EXECUTOR_H -#include <lua.h> #include <lauxlib.h> +#include <lua.h> #include "nvim/api/private/defs.h" -#include "nvim/func_attr.h" #include "nvim/eval/typval.h" #include "nvim/ex_cmds_defs.h" +#include "nvim/func_attr.h" #include "nvim/lua/converter.h" // Generated by msgpack-gen.lua @@ -19,12 +19,12 @@ EXTERN LuaRef nlua_empty_dict_ref INIT(= LUA_NOREF); EXTERN int nlua_refcount INIT(= 0); #define set_api_error(s, err) \ - do { \ - Error *err_ = (err); \ - err_->type = kErrorTypeException; \ - err_->set = true; \ - memcpy(&err_->msg[0], s, sizeof(s)); \ - } while (0) + do { \ + Error *err_ = (err); \ + err_->type = kErrorTypeException; \ + err_->set = true; \ + memcpy(&err_->msg[0], s, sizeof(s)); \ + } while (0) #define NLUA_CLEAR_REF(x) \ do { \ diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index e19274ced9..02bd612149 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -30,6 +30,7 @@ typedef struct { TSQueryCursor *cursor; int predicated_match; + int max_match_id; } TSLua_cursor; #ifdef INCLUDE_GENERATED_DECLARATIONS @@ -260,7 +261,7 @@ int tslua_push_parser(lua_State *L) return 1; } -static TSParser ** parser_check(lua_State *L, uint16_t index) +static TSParser **parser_check(lua_State *L, uint16_t index) { return luaL_checkudata(L, index, TS_META_PARSER); } @@ -933,7 +934,7 @@ push: ts_tree_cursor_current_node(ud), lua_upvalueindex(2)); // [node] - const char * field = ts_tree_cursor_current_field_name(ud); + const char *field = ts_tree_cursor_current_field_name(ud); if (field != NULL) { lua_pushstring(L, ts_tree_cursor_current_field_name(ud)); @@ -1055,6 +1056,8 @@ static int query_next_match(lua_State *L) static int query_next_capture(lua_State *L) { + // Upvalues are: + // [ cursor, node, query, current_match ] TSLua_cursor *ud = lua_touserdata(L, lua_upvalueindex(1)); TSQueryCursor *cursor = ud->cursor; @@ -1078,9 +1081,13 @@ static int query_next_capture(lua_State *L) lua_pushinteger(L, capture.index+1); // [index] push_node(L, capture.node, lua_upvalueindex(2)); // [index, node] + // Now check if we need to run the predicates uint32_t n_pred; ts_query_predicates_for_pattern(query, match.pattern_index, &n_pred); - if (n_pred > 0 && capture_index == 0) { + + if (n_pred > 0 && (ud->max_match_id < (int)match.id)) { + ud->max_match_id = match.id; + lua_pushvalue(L, lua_upvalueindex(4)); // [index, node, match] set_match(L, &match, lua_upvalueindex(2)); lua_pushinteger(L, match.pattern_index+1); @@ -1127,6 +1134,7 @@ static int node_rawquery(lua_State *L) TSLua_cursor *ud = lua_newuserdata(L, sizeof(*ud)); // [udata] ud->cursor = cursor; ud->predicated_match = -1; + ud->max_match_id = -1; lua_getfield(L, LUA_REGISTRYINDEX, TS_META_QUERYCURSOR); lua_setmetatable(L, -2); // [udata] diff --git a/src/nvim/lua/treesitter.h b/src/nvim/lua/treesitter.h index 812166f67b..b69fb9dfae 100644 --- a/src/nvim/lua/treesitter.h +++ b/src/nvim/lua/treesitter.h @@ -1,9 +1,9 @@ #ifndef NVIM_LUA_TREESITTER_H #define NVIM_LUA_TREESITTER_H +#include <lauxlib.h> #include <lua.h> #include <lualib.h> -#include <lauxlib.h> #include "tree_sitter/api.h" diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index ba124c41ad..30c7034209 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -57,28 +57,29 @@ end function vim._load_package(name) local basename = name:gsub('%.', '/') local paths = {"lua/"..basename..".lua", "lua/"..basename.."/init.lua"} - for _,path in ipairs(paths) do - local found = vim.api.nvim_get_runtime_file(path, false) - if #found > 0 then - local f, err = loadfile(found[1]) - return f or error(err) - end + local found = vim.api.nvim__get_runtime(paths, false, {is_lua=true}) + if #found > 0 then + local f, err = loadfile(found[1]) + return f or error(err) end + local so_paths = {} for _,trail in ipairs(vim._so_trails) do local path = "lua"..trail:gsub('?', basename) -- so_trails contains a leading slash - local found = vim.api.nvim_get_runtime_file(path, false) - if #found > 0 then - -- Making function name in Lua 5.1 (see src/loadlib.c:mkfuncname) is - -- a) strip prefix up to and including the first dash, if any - -- b) replace all dots by underscores - -- c) prepend "luaopen_" - -- So "foo-bar.baz" should result in "luaopen_bar_baz" - local dash = name:find("-", 1, true) - local modname = dash and name:sub(dash + 1) or name - local f, err = package.loadlib(found[1], "luaopen_"..modname:gsub("%.", "_")) - return f or error(err) - end + table.insert(so_paths, path) + end + + found = vim.api.nvim__get_runtime(so_paths, false, {is_lua=true}) + if #found > 0 then + -- Making function name in Lua 5.1 (see src/loadlib.c:mkfuncname) is + -- a) strip prefix up to and including the first dash, if any + -- b) replace all dots by underscores + -- c) prepend "luaopen_" + -- So "foo-bar.baz" should result in "luaopen_bar_baz" + local dash = name:find("-", 1, true) + local modname = dash and name:sub(dash + 1) or name + local f, err = package.loadlib(found[1], "luaopen_"..modname:gsub("%.", "_")) + return f or error(err) end return nil end @@ -108,6 +109,9 @@ setmetatable(vim, { elseif key == 'diagnostic' then t.diagnostic = require('vim.diagnostic') return t.diagnostic + elseif key == 'ui' then + t.ui = require('vim.ui') + return t.ui end end }) @@ -319,22 +323,25 @@ end do local validate = vim.validate - local function make_dict_accessor(scope) + local function make_dict_accessor(scope, handle) validate { scope = {scope, 's'}; } local mt = {} function mt:__newindex(k, v) - return vim._setvar(scope, 0, k, v) + return vim._setvar(scope, handle or 0, k, v) end function mt:__index(k) - return vim._getvar(scope, 0, k) + if handle == nil and type(k) == 'number' then + return make_dict_accessor(scope, k) + end + return vim._getvar(scope, handle or 0, k) end return setmetatable({}, mt) end - vim.g = make_dict_accessor('g') - vim.v = make_dict_accessor('v') + vim.g = make_dict_accessor('g', false) + vim.v = make_dict_accessor('v', false) vim.b = make_dict_accessor('b') vim.w = make_dict_accessor('w') vim.t = make_dict_accessor('t') @@ -430,6 +437,7 @@ function vim.notify(msg, log_level, _opts) end +---@private function vim.register_keystroke_callback() error('vim.register_keystroke_callback is deprecated, instead use: vim.on_key') end diff --git a/src/nvim/lua/xdiff.c b/src/nvim/lua/xdiff.c index dc2675a1fe..7eda9b6270 100644 --- a/src/nvim/lua/xdiff.c +++ b/src/nvim/lua/xdiff.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 <errno.h> #include <lauxlib.h> #include <lua.h> @@ -50,8 +53,8 @@ static int write_string(void *priv, mmbuffer_t *mb, int nbuf) static int hunk_locations_cb(long start_a, long count_a, long start_b, long count_b, void *cb_data) { // Mimic extra offsets done by xdiff, see: - // src/nvim/xdiff/xemit.c:284 - // src/nvim/xdiff/xutils.c:(356,368) + // src/xdiff/xemit.c:284 + // src/xdiff/xutils.c:(356,368) if (count_a > 0) { start_a += 1; } @@ -59,7 +62,7 @@ static int hunk_locations_cb(long start_a, long count_a, long start_b, long coun start_b += 1; } - lua_State * lstate = (lua_State *)cb_data; + lua_State *lstate = (lua_State *)cb_data; lua_createtable(lstate, 0, 0); lua_pushinteger(lstate, start_a); @@ -80,8 +83,8 @@ static int hunk_locations_cb(long start_a, long count_a, long start_b, long coun static int call_on_hunk_cb(long start_a, long count_a, long start_b, long count_b, void *cb_data) { // Mimic extra offsets done by xdiff, see: - // src/nvim/xdiff/xemit.c:284 - // src/nvim/xdiff/xutils.c:(356,368) + // src/xdiff/xemit.c:284 + // src/xdiff/xutils.c:(356,368) if (count_a > 0) { start_a += 1; } @@ -90,7 +93,7 @@ static int call_on_hunk_cb(long start_a, long count_a, long start_b, long count_ } hunkpriv_t *priv = (hunkpriv_t *)cb_data; - lua_State * lstate = priv->lstate; + lua_State *lstate = priv->lstate; Error *err = priv->err; const int fidx = lua_gettop(lstate); lua_pushvalue(lstate, fidx); @@ -130,7 +133,7 @@ static mmfile_t get_string_arg(lua_State *lstate, int idx) static bool check_xdiff_opt(ObjectType actType, ObjectType expType, const char *name, Error *err) { if (actType != expType) { - const char * type_str = + const char *type_str = expType == kObjectTypeString ? "string" : expType == kObjectTypeInteger ? "integer" : expType == kObjectTypeBoolean ? "boolean" : @@ -262,8 +265,8 @@ int nlua_xdl_diff(lua_State *lstate) Error err = ERROR_INIT; xdemitconf_t cfg; - xpparam_t params; - xdemitcb_t ecb; + xpparam_t params; + xdemitcb_t ecb; memset(&cfg, 0, sizeof(cfg)); memset(¶ms, 0, sizeof(params)); diff --git a/src/nvim/lua/xdiff.h b/src/nvim/lua/xdiff.h index cae7c98e81..b172d2f922 100644 --- a/src/nvim/lua/xdiff.h +++ b/src/nvim/lua/xdiff.h @@ -1,9 +1,9 @@ #ifndef NVIM_LUA_XDIFF_H #define NVIM_LUA_XDIFF_H +#include <lauxlib.h> #include <lua.h> #include <lualib.h> -#include <lauxlib.h> #ifdef INCLUDE_GENERATED_DECLARATIONS # include "lua/xdiff.h.generated.h" |