Skip to content

Commit e83e3eb

Browse files
committed
refactor: iter_captures instead of iter_matches
1 parent c4f31d6 commit e83e3eb

File tree

1 file changed

+12
-22
lines changed

1 file changed

+12
-22
lines changed

lua/nvim-treesitter-textobjects/shared.lua

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -72,28 +72,18 @@ local get_query_matches = memoize(function(bufnr, query_group, root, root_lang)
7272
local matches = {}
7373
local start_row, _, end_row, _ = root:range()
7474
-- The end row is exclusive so we need to add 1 to it.
75-
for pattern, match, metadata in query:iter_matches(root, bufnr, start_row, end_row + 1) do
76-
if pattern then
77-
local prepared_match = {}
78-
79-
-- Extract capture names from each match
80-
for id, nodes in pairs(match) do
81-
local query_name = query.captures[id] -- name of the capture in the query
82-
if query_name ~= nil then
83-
local path = vim.split(query_name, "%.")
84-
if metadata[id] and metadata[id].range then
85-
insert_to_path(prepared_match, path, add_bytes(bufnr, metadata[id].range))
86-
else
87-
local srow, scol, sbyte, erow, ecol, ebyte = nodes[1]:range(true)
88-
if #nodes > 1 then
89-
local _, _, _, e_erow, e_ecol, e_ebyte = nodes[#nodes]:range(true)
90-
erow = e_erow
91-
ecol = e_ecol
92-
ebyte = e_ebyte
93-
end
94-
insert_to_path(prepared_match, path, { srow, scol, sbyte, erow, ecol, ebyte })
95-
end
96-
end
75+
for id, node, metadata in query:iter_captures(root, bufnr, start_row, end_row + 1) do
76+
local prepared_match = {}
77+
78+
-- Extract capture names from each match
79+
local query_name = query.captures[id] -- name of the capture in the query
80+
if query_name ~= nil then
81+
local path = vim.split(query_name, "%.")
82+
83+
if metadata[id] and metadata[id].range then
84+
insert_to_path(prepared_match, path, add_bytes(bufnr, metadata[id].range))
85+
else
86+
insert_to_path(prepared_match, path, { node:range(true) })
9787
end
9888

9989
matches[#matches + 1] = prepared_match

0 commit comments

Comments
 (0)