From 437ddcbc87aedcab5c3b70266e43682abd4703dd Mon Sep 17 00:00:00 2001 From: "Haoyuan (Bill) Xing" Date: Wed, 16 Apr 2025 21:44:39 -0700 Subject: [PATCH] fixed tree-sitter iterators. --- lua/orgmode-babel/init.lua | 47 +++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/lua/orgmode-babel/init.lua b/lua/orgmode-babel/init.lua index b076c7f..ffe7e29 100644 --- a/lua/orgmode-babel/init.lua +++ b/lua/orgmode-babel/init.lua @@ -89,26 +89,29 @@ function M.get_names_in_buffer(bufnr, line1, line2) local passed = not range local name - for id, node in pairs(match) do - local row1, col1, row2, col2 = node:range() - local text = vim.api.nvim_buf_get_text(bufnr, row1, col1, row2, col2, {})[1] + for id, nodes in pairs(match) do + for _, node in ipairs(nodes) do + local row1, col1, row2, col2 = node:range() - if named_blocks_query.captures[id] == "block" and not passed then - if single and line1 - 1 > row1 and line1 - 1 < row2 then - passed = true - elseif not single and line1 - 1 <= row1 and row2 <= line2 - 1 then - passed = true + local text = vim.api.nvim_buf_get_text(bufnr, row1, col1, row2, col2, {})[1] + + if named_blocks_query.captures[id] == "block" and not passed then + if single and line1 - 1 > row1 and line1 - 1 < row2 then + passed = true + elseif not single and line1 - 1 <= row1 and row2 <= line2 - 1 then + passed = true + end + end + + if named_blocks_query.captures[id] == "name" then + name = text end end - if named_blocks_query.captures[id] == "name" then - name = text + if passed then + table.insert(names, name) end end - - if passed then - table.insert(names, name) - end end return names end @@ -130,14 +133,16 @@ function M.get_blocks_in_buffer(bufnr, line1, line2) for _, match in unnamed_blocks_query:iter_matches(root, bufnr, 0, -1) do local passed = not range - for id, node in pairs(match) do - local row1, _, row2 = node:range() + for id, nodes in pairs(match) do + for _, node in ipairs(nodes) do + local row1, _, row2 = node:range() - if unnamed_blocks_query.captures[id] == "block" and not passed then - if single and line1 - 1 > row1 and line1 - 1 < row2 then - passed = true - elseif not single and line1 - 1 <= row1 and row2 <= line2 - 1 then - passed = true + if unnamed_blocks_query.captures[id] == "block" and not passed then + if single and line1 - 1 > row1 and line1 - 1 < row2 then + passed = true + elseif not single and line1 - 1 <= row1 and row2 <= line2 - 1 then + passed = true + end end end end