diff options
author | Christian Clason <c.clason@uni-graz.at> | 2022-06-02 17:35:16 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-02 17:35:16 +0200 |
commit | 26966688aa622b448e3ef46d8f1155d57b099211 (patch) | |
tree | 8dc3237bfc8f640eb893014a3ce0cf83701a9f10 | |
parent | d93ba03c717bee05fe6d239fd7faefe6e9698c85 (diff) | |
download | rneovim-26966688aa622b448e3ef46d8f1155d57b099211.tar.gz rneovim-26966688aa622b448e3ef46d8f1155d57b099211.tar.bz2 rneovim-26966688aa622b448e3ef46d8f1155d57b099211.zip |
fix(treesitter): correct region for string parser (#18794)
fixes injections for string parsers after https://github.com/neovim/neovim/commit/eab4d03a3264b2afaf803ed839fa25bc4e7acedd
-rw-r--r-- | runtime/doc/treesitter.txt | 4 | ||||
-rw-r--r-- | runtime/lua/vim/treesitter/languagetree.lua | 31 |
2 files changed, 20 insertions, 15 deletions
diff --git a/runtime/doc/treesitter.txt b/runtime/doc/treesitter.txt index 52b415bbd1..eb19bf5934 100644 --- a/runtime/doc/treesitter.txt +++ b/runtime/doc/treesitter.txt @@ -754,8 +754,8 @@ LanguageTree:set_included_regions({self}, {regions}) parsed again. Parameters: ~ - {regions} A list of regions this tree should manage and - parse. + {regions} (table) list of regions this tree should manage + and parse. {self} LanguageTree:source({self}) *LanguageTree:source()* diff --git a/runtime/lua/vim/treesitter/languagetree.lua b/runtime/lua/vim/treesitter/languagetree.lua index 5a05a29da8..767573e345 100644 --- a/runtime/lua/vim/treesitter/languagetree.lua +++ b/runtime/lua/vim/treesitter/languagetree.lua @@ -261,22 +261,27 @@ end --- --- Note, this call invalidates the tree and requires it to be parsed again. --- ----@param regions A list of regions this tree should manage and parse. +---@param regions (table) list of regions this tree should manage and parse. function LanguageTree:set_included_regions(regions) - -- TODO(vigoux): I don't think string parsers are useful for now - if type(self._source) == 'number' then - -- Transform the tables from 4 element long to 6 element long (with byte offset) - for _, region in ipairs(regions) do - for i, range in ipairs(region) do - if type(range) == 'table' and #range == 4 then - local start_row, start_col, end_row, end_col = unpack(range) + -- Transform the tables from 4 element long to 6 element long (with byte offset) + for _, region in ipairs(regions) do + for i, range in ipairs(region) do + if type(range) == 'table' and #range == 4 then + local start_row, start_col, end_row, end_col = unpack(range) + local start_byte = 0 + local end_byte = 0 + -- TODO(vigoux): proper byte computation here, and account for EOL ? + if type(self._source) == 'number' then -- Easy case, this is a buffer parser - -- TODO(vigoux): proper byte computation here, and account for EOL ? - local start_byte = a.nvim_buf_get_offset(self._source, start_row) + start_col - local end_byte = a.nvim_buf_get_offset(self._source, end_row) + end_col - - region[i] = { start_row, start_col, start_byte, end_row, end_col, end_byte } + start_byte = a.nvim_buf_get_offset(self._source, start_row) + start_col + end_byte = a.nvim_buf_get_offset(self._source, end_row) + end_col + elseif type(self._source) == 'string' then + -- string parser, single `\n` delimited string + start_byte = vim.fn.byteidx(self._source, start_col) + end_byte = vim.fn.byteidx(self._source, end_col) end + + region[i] = { start_row, start_col, start_byte, end_row, end_col, end_byte } end end end |