diff options
author | Lewis Russell <lewis6991@gmail.com> | 2023-03-09 16:09:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-09 16:09:39 +0000 |
commit | ae263aff9547b8b513c4fedaceb4cbf93c57b866 (patch) | |
tree | 75ff0d6ff424a2bf5e582da29ff87c2911db33e7 /src/nvim/lua/treesitter.c | |
parent | 64d3f68c07f7e67bbbd5db11b289705f7569b949 (diff) | |
download | rneovim-ae263aff9547b8b513c4fedaceb4cbf93c57b866.tar.gz rneovim-ae263aff9547b8b513c4fedaceb4cbf93c57b866.tar.bz2 rneovim-ae263aff9547b8b513c4fedaceb4cbf93c57b866.zip |
refactor(treesitter): use byte ranges from treesitter (#22589)
Diffstat (limited to 'src/nvim/lua/treesitter.c')
-rw-r--r-- | src/nvim/lua/treesitter.c | 35 |
1 files changed, 23 insertions, 12 deletions
diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 4cbe7329a6..ae69f3f120 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -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); |