Skip to content

Commit 2696668

Browse files
authored
fix(treesitter): correct region for string parser (#18794)
fixes injections for string parsers after eab4d03
1 parent d93ba03 commit 2696668

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

runtime/doc/treesitter.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,8 @@ LanguageTree:set_included_regions({self}, {regions})
754754
parsed again.
755755

756756
Parameters: ~
757-
{regions} A list of regions this tree should manage and
758-
parse.
757+
{regions} (table) list of regions this tree should manage
758+
and parse.
759759
{self}
760760

761761
LanguageTree:source({self}) *LanguageTree:source()*

runtime/lua/vim/treesitter/languagetree.lua

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -261,22 +261,27 @@ end
261261
---
262262
--- Note, this call invalidates the tree and requires it to be parsed again.
263263
---
264-
---@param regions A list of regions this tree should manage and parse.
264+
---@param regions (table) list of regions this tree should manage and parse.
265265
function LanguageTree:set_included_regions(regions)
266-
-- TODO(vigoux): I don't think string parsers are useful for now
267-
if type(self._source) == 'number' then
268-
-- Transform the tables from 4 element long to 6 element long (with byte offset)
269-
for _, region in ipairs(regions) do
270-
for i, range in ipairs(region) do
271-
if type(range) == 'table' and #range == 4 then
272-
local start_row, start_col, end_row, end_col = unpack(range)
266+
-- Transform the tables from 4 element long to 6 element long (with byte offset)
267+
for _, region in ipairs(regions) do
268+
for i, range in ipairs(region) do
269+
if type(range) == 'table' and #range == 4 then
270+
local start_row, start_col, end_row, end_col = unpack(range)
271+
local start_byte = 0
272+
local end_byte = 0
273+
-- TODO(vigoux): proper byte computation here, and account for EOL ?
274+
if type(self._source) == 'number' then
273275
-- Easy case, this is a buffer parser
274-
-- TODO(vigoux): proper byte computation here, and account for EOL ?
275-
local start_byte = a.nvim_buf_get_offset(self._source, start_row) + start_col
276-
local end_byte = a.nvim_buf_get_offset(self._source, end_row) + end_col
277-
278-
region[i] = { start_row, start_col, start_byte, end_row, end_col, end_byte }
276+
start_byte = a.nvim_buf_get_offset(self._source, start_row) + start_col
277+
end_byte = a.nvim_buf_get_offset(self._source, end_row) + end_col
278+
elseif type(self._source) == 'string' then
279+
-- string parser, single `\n` delimited string
280+
start_byte = vim.fn.byteidx(self._source, start_col)
281+
end_byte = vim.fn.byteidx(self._source, end_col)
279282
end
283+
284+
region[i] = { start_row, start_col, start_byte, end_row, end_col, end_byte }
280285
end
281286
end
282287
end

0 commit comments

Comments
 (0)