perf: add on_range in treesitter highlighting

This commit is contained in:
vanaigr
2025-08-03 21:39:55 -05:00
parent 118e7e7111
commit 5edbabdbec
17 changed files with 615 additions and 193 deletions

View File

@@ -1361,34 +1361,46 @@ static int tslua_push_querycursor(lua_State *L)
TSQuery *query = query_check(L, 2);
TSQueryCursor *cursor = ts_query_cursor_new();
if (lua_gettop(L) >= 3 && !lua_isnil(L, 3)) {
luaL_argcheck(L, lua_istable(L, 3), 3, "table expected");
}
lua_getfield(L, 3, "start_row");
uint32_t start_row = (uint32_t)luaL_checkinteger(L, -1);
lua_pop(L, 1);
lua_getfield(L, 3, "start_col");
uint32_t start_col = (uint32_t)luaL_checkinteger(L, -1);
lua_pop(L, 1);
lua_getfield(L, 3, "end_row");
uint32_t end_row = (uint32_t)luaL_checkinteger(L, -1);
lua_pop(L, 1);
lua_getfield(L, 3, "end_col");
uint32_t end_col = (uint32_t)luaL_checkinteger(L, -1);
lua_pop(L, 1);
ts_query_cursor_set_point_range(cursor, (TSPoint){ start_row, start_col },
(TSPoint){ end_row, end_col });
lua_getfield(L, 3, "max_start_depth");
if (!lua_isnil(L, -1)) {
uint32_t max_start_depth = (uint32_t)luaL_checkinteger(L, -1);
ts_query_cursor_set_max_start_depth(cursor, max_start_depth);
}
lua_pop(L, 1);
lua_getfield(L, 3, "match_limit");
if (!lua_isnil(L, -1)) {
uint32_t match_limit = (uint32_t)luaL_checkinteger(L, -1);
ts_query_cursor_set_match_limit(cursor, match_limit);
}
lua_pop(L, 1);
ts_query_cursor_exec(cursor, query, node);
if (lua_gettop(L) >= 3) {
uint32_t start = (uint32_t)luaL_checkinteger(L, 3);
uint32_t end = lua_gettop(L) >= 4 ? (uint32_t)luaL_checkinteger(L, 4) : MAXLNUM;
ts_query_cursor_set_point_range(cursor, (TSPoint){ start, 0 }, (TSPoint){ end, 0 });
}
if (lua_gettop(L) >= 5 && !lua_isnil(L, 5)) {
luaL_argcheck(L, lua_istable(L, 5), 5, "table expected");
lua_pushnil(L); // [dict, ..., nil]
while (lua_next(L, 5)) {
// [dict, ..., key, value]
if (lua_type(L, -2) == LUA_TSTRING) {
char *k = (char *)lua_tostring(L, -2);
if (strequal("max_start_depth", k)) {
uint32_t max_start_depth = (uint32_t)lua_tointeger(L, -1);
ts_query_cursor_set_max_start_depth(cursor, max_start_depth);
} else if (strequal("match_limit", k)) {
uint32_t match_limit = (uint32_t)lua_tointeger(L, -1);
ts_query_cursor_set_match_limit(cursor, match_limit);
}
}
// pop the value; lua_next will pop the key.
lua_pop(L, 1); // [dict, ..., key]
}
}
TSQueryCursor **ud = lua_newuserdata(L, sizeof(*ud)); // [node, query, ..., udata]
*ud = cursor;
lua_getfield(L, LUA_REGISTRYINDEX, TS_META_QUERYCURSOR); // [node, query, ..., udata, meta]