From 93ad652b887105345fcaf8c0c5a99fc025dd1d71 Mon Sep 17 00:00:00 2001 From: arne314 <73391160+arne314@users.noreply.github.com> Date: Sun, 4 May 2025 15:50:21 +0200 Subject: [PATCH] fix: breaking treesitter changes of nvim 0.11 --- lua/typstar/snippets/visual.lua | 6 +++--- lua/typstar/utils.lua | 22 +++++++++++++++++++--- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lua/typstar/snippets/visual.lua b/lua/typstar/snippets/visual.lua index fa4faef..228d9d4 100644 --- a/lua/typstar/snippets/visual.lua +++ b/lua/typstar/snippets/visual.lua @@ -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 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 - if match then - local start_row, start_col, end_row, end_col = utils.treesitter_match_start_end(match) + for _, match in ipairs(utils.treesitter_iter_matches(root, query, bufnr, cursor[1], cursor[1] + 1)) do + for _, nodes in pairs(match) do + 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 vim.schedule(function() -- to not interfere with luasnip local cursor_offset = 0 diff --git a/lua/typstar/utils.lua b/lua/typstar/utils.lua index 6dccdae..3a9c9a8 100644 --- a/lua/typstar/utils.lua +++ b/lua/typstar/utils.lua @@ -71,6 +71,21 @@ 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) local start_row, start_col, _, _ = match[1]:range() local _, _, end_row, end_col = match[#match]:range() @@ -80,9 +95,10 @@ end function M.cursor_within_treesitter_query(query, match_tolerance, cursor) cursor = cursor or M.get_cursor_pos() 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 - if match then - local start_row, start_col, end_row, end_col = M.treesitter_match_start_end(match) + local root = M.get_treesitter_root(bufnr) + for _, match in ipairs(M.treesitter_iter_matches(root, query, bufnr, cursor[1], cursor[1] + 1)) do + 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) if matched then return true end end