diff --git a/runtime/lua/vim/treesitter.lua b/runtime/lua/vim/treesitter.lua index d5bc8e0a1b..f356673839 100644 --- a/runtime/lua/vim/treesitter.lua +++ b/runtime/lua/vim/treesitter.lua @@ -31,7 +31,7 @@ function Parser:_on_lines(bufnr, _, start_row, old_stop_row, stop_row, old_byte_ end function Parser:set_included_ranges(ranges) - self._parser:set_included_ranges(self.bufnr, ranges) + self._parser:set_included_ranges(ranges) -- The buffer will need to be parsed again later self.valid = false end diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 6e554d9b54..aa76ff33a4 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -386,8 +386,8 @@ static int parser_edit(lua_State *L) static int parser_set_ranges(lua_State *L) { - if (lua_gettop(L) < 3) { - lua_pushstring(L, "not enough args to parser:set_ranges()"); + if (lua_gettop(L) < 2) { + lua_pushstring(L, "not enough args to parser:set_included_ranges()"); return lua_error(L); } @@ -396,20 +396,18 @@ static int parser_set_ranges(lua_State *L) return 0; } - int bufnr = lua_tointeger(L, 2); - - if (!lua_istable(L, 3)) { + if (!lua_istable(L, 2)) { lua_pushstring(L, "argument for parser:set_included_ranges() should be a table."); return lua_error(L); } - size_t tbl_len = lua_objlen(L, 3); + size_t tbl_len = lua_objlen(L, 2); TSRange *ranges = xmalloc(sizeof(TSRange) * tbl_len); // [ parser, ranges ] for (size_t index = 0; index < tbl_len; index++) { - lua_rawgeti(L, 3, index + 1); // [ parser, ranges, range ] + lua_rawgeti(L, 2, index + 1); // [ parser, ranges, range ] if (!lua_istable(L, -1)) { xfree(ranges); @@ -419,60 +417,47 @@ static int parser_set_ranges(lua_State *L) return lua_error(L); } - if (lua_objlen(L, -1) < 4) { + if (lua_objlen(L, -1) != 2) { xfree(ranges); lua_pushstring( L, - "argument for parser:set_included_ranges() should be a table of ranges of 4 elements."); + "argument for parser:set_included_ranges() should be a table of ranges of 2 elements."); return lua_error(L); } - lua_rawgeti(L, -1, 1); // [ parser, ranges, range, num ] - unsigned int start_row = lua_tointeger(L, -1); - lua_pop(L, 1); // [ parser, ranges, range ] - - lua_rawgeti(L, -1, 2); // [ parser, ranges, range, num ] - unsigned int start_col = lua_tointeger(L, -1); - lua_pop(L, 1); // [ parser, ranges, range ] - - lua_rawgeti(L, -1, 3); // [ parser, ranges, range, num ] - unsigned int stop_row = lua_tointeger(L, -1); - lua_pop(L, 1); // [ parser, ranges, range ] - - lua_rawgeti(L, -1, 4); // [ parser, ranges, range, num ] - unsigned int stop_col = lua_tointeger(L, -1); - lua_pop(L, 1); // [ parser, ranges, range ] - - buf_T * buf = buflist_findnr(bufnr); - - if (!buf) { - buf = curbuf; + lua_rawgeti(L, -1, 1); // [ parser, ranges, range, start_node ] + TSNode start_node; + if (!node_check(L, -1, &start_node)) { + xfree(ranges); + lua_pushstring( + L, + "ranges should be tables of nodes."); + return lua_error(L); } + lua_pop(L, 1); // [ parser, ranges, range ] - // TODO(vigoux): For sure that's wrong, try to find a way to get the - // byte offset directly - // Lines are 0 based for consistency - uint32_t start_byte = - ml_find_line_or_offset(buf, start_row + 1, NULL, false) + start_col; - uint32_t stop_byte = - ml_find_line_or_offset(buf, stop_row + 1, NULL, false) + stop_col; + lua_rawgeti(L, -1, 1); // [ parser, ranges, range, stop_node ] + TSNode stop_node; + if (!node_check(L, -1, &stop_node)) { + xfree(ranges); + lua_pushstring( + L, + "ranges should be tables of nodes."); + return lua_error(L); + } + lua_pop(L, 1); // [ parser, ranges, range ] ranges[index] = (TSRange) { - .start_point = (TSPoint) { - .row = start_row, - .column = start_col - }, - .end_point = (TSPoint) { - .row = stop_row, - .column = stop_col - }, - .start_byte = start_byte, - .end_byte = stop_byte + .start_point = ts_node_start_point(start_node), + .end_point = ts_node_end_point(stop_node), + .start_byte = ts_node_start_byte(start_node), + .end_byte = ts_node_end_byte(stop_node) }; + lua_pop(L, 1); // [ parser, ranges ] } - // This memcpies ranges, thus we can free it. + // This memcpies ranges, thus we can free it afterwards ts_parser_set_included_ranges(p->parser, ranges, tbl_len); xfree(ranges); @@ -561,9 +546,9 @@ static void push_node(lua_State *L, TSNode node, int uindex) lua_setfenv(L, -2); // [udata] } -static bool node_check(lua_State *L, TSNode *res) +static bool node_check(lua_State *L, int index, TSNode *res) { - TSNode *ud = luaL_checkudata(L, 1, "treesitter_node"); + TSNode *ud = luaL_checkudata(L, index, "treesitter_node"); if (ud) { *res = *ud; return true; @@ -575,7 +560,7 @@ static bool node_check(lua_State *L, TSNode *res) static int node_tostring(lua_State *L) { TSNode node; - if (!node_check(L, &node)) { + if (!node_check(L,1, &node)) { return 0; } lua_pushstring(L, "