refactor(treesitter): move functions from executor.c to treesitter.c

This commit is contained in:
Lewis Russell
2025-05-11 09:14:57 +01:00
committed by Lewis Russell
parent cc78f88201
commit 40b64e9100
2 changed files with 63 additions and 61 deletions

View File

@ -820,7 +820,7 @@ static bool nlua_state_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
}
// internal vim._treesitter... API
nlua_add_treesitter(lstate);
nlua_treesitter_init(lstate);
nlua_state_add_stdlib(lstate, false);
@ -938,7 +938,7 @@ void nlua_free_all_mem(void)
lua_State *lstate = global_lstate;
nlua_unref_global(lstate, require_ref);
nlua_common_free_all_mem(lstate);
tslua_free();
nlua_treesitter_free();
}
static void nlua_common_free_all_mem(lua_State *lstate)
@ -1870,55 +1870,6 @@ bool nlua_exec_file(const char *path)
return true;
}
int tslua_get_language_version(lua_State *L)
{
lua_pushnumber(L, TREE_SITTER_LANGUAGE_VERSION);
return 1;
}
int tslua_get_minimum_language_version(lua_State *L)
{
lua_pushnumber(L, TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION);
return 1;
}
static void nlua_add_treesitter(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
{
tslua_init(lstate);
lua_pushcfunction(lstate, tslua_push_parser);
lua_setfield(lstate, -2, "_create_ts_parser");
lua_pushcfunction(lstate, tslua_push_querycursor);
lua_setfield(lstate, -2, "_create_ts_querycursor");
lua_pushcfunction(lstate, tslua_add_language_from_object);
lua_setfield(lstate, -2, "_ts_add_language_from_object");
#ifdef HAVE_WASMTIME
lua_pushcfunction(lstate, tslua_add_language_from_wasm);
lua_setfield(lstate, -2, "_ts_add_language_from_wasm");
#endif
lua_pushcfunction(lstate, tslua_has_language);
lua_setfield(lstate, -2, "_ts_has_language");
lua_pushcfunction(lstate, tslua_remove_lang);
lua_setfield(lstate, -2, "_ts_remove_language");
lua_pushcfunction(lstate, tslua_inspect_lang);
lua_setfield(lstate, -2, "_ts_inspect_language");
lua_pushcfunction(lstate, tslua_parse_query);
lua_setfield(lstate, -2, "_ts_parse_query");
lua_pushcfunction(lstate, tslua_get_language_version);
lua_setfield(lstate, -2, "_ts_get_language_version");
lua_pushcfunction(lstate, tslua_get_minimum_language_version);
lua_setfield(lstate, -2, "_ts_get_minimum_language_version");
}
static garray_T expand_result_array = GA_EMPTY_INIT_VALUE;
/// Finds matches for Lua cmdline completion and advances xp->xp_pattern after prefix.

View File

@ -72,7 +72,7 @@ static TSWasmStore *ts_wasmstore;
// TSLanguage
int tslua_has_language(lua_State *L)
static int tslua_has_language(lua_State *L)
{
const char *lang_name = luaL_checkstring(L, 1);
lua_pushboolean(L, map_has(cstr_t, &langs, lang_name));
@ -117,15 +117,17 @@ static const char *wasmerr_to_str(TSWasmErrorKind werr)
}
#endif
int tslua_add_language_from_wasm(lua_State *L)
#ifdef HAVE_WASMTIME
static int tslua_add_language_from_wasm(lua_State *L)
{
return add_language(L, true);
}
#endif
// Creates the language into the internal language map.
//
// Returns true if the language is correctly loaded in the language map
int tslua_add_language_from_object(lua_State *L)
static int tslua_add_language_from_object(lua_State *L)
{
return add_language(L, false);
}
@ -241,7 +243,7 @@ static int add_language(lua_State *L, bool is_wasm)
return 1;
}
int tslua_remove_lang(lua_State *L)
static int tslua_remove_lang(lua_State *L)
{
const char *lang_name = luaL_checkstring(L, 1);
bool present = map_has(cstr_t, &langs, lang_name);
@ -264,7 +266,7 @@ static TSLanguage *lang_check(lua_State *L, int index)
return lang;
}
int tslua_inspect_lang(lua_State *L)
static int tslua_inspect_lang(lua_State *L)
{
TSLanguage *lang = lang_check(L, 1);
@ -374,7 +376,7 @@ static struct luaL_Reg parser_meta[] = {
{ NULL, NULL }
};
int tslua_push_parser(lua_State *L)
static int tslua_push_parser(lua_State *L)
{
TSLanguage *lang = lang_check(L, 1);
@ -1337,7 +1339,7 @@ static struct luaL_Reg querycursor_meta[] = {
{ NULL, NULL }
};
int tslua_push_querycursor(lua_State *L)
static int tslua_push_querycursor(lua_State *L)
{
TSNode node = node_check(L, 1);
@ -1497,7 +1499,7 @@ static struct luaL_Reg query_meta[] = {
{ NULL, NULL }
};
int tslua_parse_query(lua_State *L)
static int tslua_parse_query(lua_State *L)
{
if (lua_gettop(L) < 2 || !lua_isstring(L, 1) || !lua_isstring(L, 2)) {
return luaL_error(L, "string expected");
@ -1725,7 +1727,7 @@ static void build_meta(lua_State *L, const char *tname, const luaL_Reg *meta)
/// Init the tslua library.
///
/// All global state is stored in the registry of the lua_State.
void tslua_init(lua_State *L)
static void tslua_init(lua_State *L)
{
// type metatables
build_meta(L, TS_META_PARSER, parser_meta);
@ -1738,7 +1740,19 @@ void tslua_init(lua_State *L)
ts_set_allocator(xmalloc, xcalloc, xrealloc, xfree);
}
void tslua_free(void)
static int tslua_get_language_version(lua_State *L)
{
lua_pushnumber(L, TREE_SITTER_LANGUAGE_VERSION);
return 1;
}
static int tslua_get_minimum_language_version(lua_State *L)
{
lua_pushnumber(L, TREE_SITTER_MIN_COMPATIBLE_LANGUAGE_VERSION);
return 1;
}
void nlua_treesitter_free(void)
{
#ifdef HAVE_WASMTIME
if (wasmengine != NULL) {
@ -1749,3 +1763,40 @@ void tslua_free(void)
}
#endif
}
void nlua_treesitter_init(lua_State *const lstate) FUNC_ATTR_NONNULL_ALL
{
tslua_init(lstate);
lua_pushcfunction(lstate, tslua_push_parser);
lua_setfield(lstate, -2, "_create_ts_parser");
lua_pushcfunction(lstate, tslua_push_querycursor);
lua_setfield(lstate, -2, "_create_ts_querycursor");
lua_pushcfunction(lstate, tslua_add_language_from_object);
lua_setfield(lstate, -2, "_ts_add_language_from_object");
#ifdef HAVE_WASMTIME
lua_pushcfunction(lstate, tslua_add_language_from_wasm);
lua_setfield(lstate, -2, "_ts_add_language_from_wasm");
#endif
lua_pushcfunction(lstate, tslua_has_language);
lua_setfield(lstate, -2, "_ts_has_language");
lua_pushcfunction(lstate, tslua_remove_lang);
lua_setfield(lstate, -2, "_ts_remove_language");
lua_pushcfunction(lstate, tslua_inspect_lang);
lua_setfield(lstate, -2, "_ts_inspect_language");
lua_pushcfunction(lstate, tslua_parse_query);
lua_setfield(lstate, -2, "_ts_parse_query");
lua_pushcfunction(lstate, tslua_get_language_version);
lua_setfield(lstate, -2, "_ts_get_language_version");
lua_pushcfunction(lstate, tslua_get_minimum_language_version);
lua_setfield(lstate, -2, "_ts_get_minimum_language_version");
}