fix(treesitter): correct region for string parser (#18794)

fixes injections for string parsers after eab4d03a32
This commit is contained in:
Christian Clason 2022-06-02 17:35:16 +02:00 committed by GitHub
parent d93ba03c71
commit 26966688aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 15 deletions

View File

@ -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()*

View File

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