refactor(treesitter): use byte ranges from treesitter (#22589)

This commit is contained in:
Lewis Russell
2023-03-09 16:09:39 +00:00
committed by GitHub
parent 64d3f68c07
commit ae263aff95
6 changed files with 337 additions and 191 deletions

View File

@@ -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);