fix: breaking treesitter changes of nvim 0.11

This commit is contained in:
arne314
2025-05-04 15:50:21 +02:00
parent bd94cf402e
commit 93ad652b88
2 changed files with 22 additions and 6 deletions

View File

@@ -43,9 +43,9 @@ local ts_wrap_query = ts.query.parse('typst', '[(call) (ident) (letter) (number)
local ts_wrapnobrackets_query = ts.query.parse('typst', '(group) @wrapnobrackets') local ts_wrapnobrackets_query = ts.query.parse('typst', '(group) @wrapnobrackets')
local process_ts_query = function(bufnr, cursor, query, root, insert1, insert2, cut_offset) local process_ts_query = function(bufnr, cursor, query, root, insert1, insert2, cut_offset)
for _, match, _ in query:iter_matches(root, bufnr, cursor[1], cursor[1] + 1) do for _, match in ipairs(utils.treesitter_iter_matches(root, query, bufnr, cursor[1], cursor[1] + 1)) do
if match then for _, nodes in pairs(match) do
local start_row, start_col, end_row, end_col = utils.treesitter_match_start_end(match) local start_row, start_col, end_row, end_col = utils.treesitter_match_start_end(nodes)
if end_row == cursor[1] and end_col == cursor[2] then if end_row == cursor[1] and end_col == cursor[2] then
vim.schedule(function() -- to not interfere with luasnip vim.schedule(function() -- to not interfere with luasnip
local cursor_offset = 0 local cursor_offset = 0

View File

@@ -71,6 +71,21 @@ end
function M.get_treesitter_root(bufnr) return ts.get_parser(bufnr):parse()[1]:root() end function M.get_treesitter_root(bufnr) return ts.get_parser(bufnr):parse()[1]:root() end
function M.treesitter_iter_matches(root, query, bufnr, start, stop)
local result = {}
local idx = 1
for _, matches, _ in query:iter_matches(root, bufnr, start, stop) do
if #matches then
if type(matches[1]) == 'userdata' then -- nvim version < 0.11
matches = { matches }
end
result[idx] = matches
idx = idx + 1
end
end
return result
end
function M.treesitter_match_start_end(match) function M.treesitter_match_start_end(match)
local start_row, start_col, _, _ = match[1]:range() local start_row, start_col, _, _ = match[1]:range()
local _, _, end_row, end_col = match[#match]:range() local _, _, end_row, end_col = match[#match]:range()
@@ -80,9 +95,10 @@ end
function M.cursor_within_treesitter_query(query, match_tolerance, cursor) function M.cursor_within_treesitter_query(query, match_tolerance, cursor)
cursor = cursor or M.get_cursor_pos() cursor = cursor or M.get_cursor_pos()
local bufnr = vim.api.nvim_get_current_buf() local bufnr = vim.api.nvim_get_current_buf()
for _, match, _ in query:iter_matches(M.get_treesitter_root(bufnr), bufnr, cursor[1], cursor[1] + 1) do local root = M.get_treesitter_root(bufnr)
if match then for _, match in ipairs(M.treesitter_iter_matches(root, query, bufnr, cursor[1], cursor[1] + 1)) do
local start_row, start_col, end_row, end_col = M.treesitter_match_start_end(match) for _, nodes in pairs(match) do
local start_row, start_col, end_row, end_col = M.treesitter_match_start_end(nodes)
local matched = M.cursor_within_coords(cursor, start_row, end_row, start_col, end_col, match_tolerance) local matched = M.cursor_within_coords(cursor, start_row, end_row, start_col, end_col, match_tolerance)
if matched then return true end if matched then return true end
end end