diff options
author | Thomas Vigouroux <tomvig38@gmail.com> | 2020-09-21 14:53:32 +0200 |
---|---|---|
committer | Thomas Vigouroux <tomvig38@gmail.com> | 2020-10-12 18:23:14 +0200 |
commit | bdbc56f931a68bad65e0ba5a3fe3968403995da2 (patch) | |
tree | 7e5608474459a6bbb49c33b65d557d45985692c4 /src | |
parent | d198aa511a90713aea9939e145c34a5b2e828e4d (diff) | |
download | rneovim-bdbc56f931a68bad65e0ba5a3fe3968403995da2.tar.gz rneovim-bdbc56f931a68bad65e0ba5a3fe3968403995da2.tar.bz2 rneovim-bdbc56f931a68bad65e0ba5a3fe3968403995da2.zip |
treesitter: allow custom parser for highlighter
Also allow to get parser ranges.
This will be useful for language injection, allowing us to tweak the
parser's ranges on the fly.
Update runtime/lua/vim/treesitter.lua
Co-authored-by: Paul Burlumi <paul@burlumi.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/lua/treesitter.c | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/src/nvim/lua/treesitter.c b/src/nvim/lua/treesitter.c index 33d9772bec..5258352e72 100644 --- a/src/nvim/lua/treesitter.c +++ b/src/nvim/lua/treesitter.c @@ -43,6 +43,7 @@ static struct luaL_Reg parser_meta[] = { { "edit", parser_edit }, { "tree", parser_tree }, { "set_included_ranges", parser_set_ranges }, + { "included_ranges", parser_get_ranges }, { NULL, NULL } }; @@ -314,6 +315,26 @@ static const char *input_cb(void *payload, uint32_t byte_index, #undef BUFSIZE } +static void push_ranges(lua_State *L, + const TSRange *ranges, + const unsigned int length) +{ + lua_createtable(L, length, 0); + for (size_t i = 0; i < length; i++) { + lua_createtable(L, 4, 0); + lua_pushinteger(L, ranges[i].start_point.row); + lua_rawseti(L, -2, 1); + lua_pushinteger(L, ranges[i].start_point.column); + lua_rawseti(L, -2, 2); + lua_pushinteger(L, ranges[i].end_point.row); + lua_rawseti(L, -2, 3); + lua_pushinteger(L, ranges[i].end_point.column); + lua_rawseti(L, -2, 4); + + lua_rawseti(L, -2, i+1); + } +} + static int parser_parse(lua_State *L) { TSLua_parser *p = parser_check(L); @@ -363,20 +384,8 @@ static int parser_parse(lua_State *L) tslua_push_tree(L, p->tree); - lua_createtable(L, n_ranges, 0); - for (size_t i = 0; i < n_ranges; i++) { - lua_createtable(L, 4, 0); - lua_pushinteger(L, changed[i].start_point.row); - lua_rawseti(L, -2, 1); - lua_pushinteger(L, changed[i].start_point.column); - lua_rawseti(L, -2, 2); - lua_pushinteger(L, changed[i].end_point.row); - lua_rawseti(L, -2, 3); - lua_pushinteger(L, changed[i].end_point.column); - lua_rawseti(L, -2, 4); + push_ranges(L, changed, n_ranges); - lua_rawseti(L, -2, i+1); - } xfree(changed); return 2; } @@ -474,6 +483,21 @@ static int parser_set_ranges(lua_State *L) return 0; } +static int parser_get_ranges(lua_State *L) +{ + TSLua_parser *p = parser_check(L); + if (!p || !p->parser) { + return 0; + } + + unsigned int len; + const TSRange *ranges = ts_parser_included_ranges(p->parser, &len); + + push_ranges(L, ranges, len); + + return 1; +} + // Tree methods |