Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: replace all Range with TSNode as much as possible #674

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 11 additions & 39 deletions lua/nvim-treesitter-textobjects/shared.lua
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
local ts = vim.treesitter
local add_bytes = require("vim.treesitter._range").add_bytes

-- luacheck: push ignore 631
---@alias TSTextObjects.Metadata {range: {[1]: number, [2]: number, [3]: number, [4]: number, [5]: number, [6]: number, [7]: string}}
-- luacheck: pop

local M = {}

---@param object any
Expand Down Expand Up @@ -76,42 +72,18 @@ local get_query_matches = memoize(function(bufnr, query_group, root, root_lang)
local matches = {}
local start_row, _, end_row, _ = root:range()
-- The end row is exclusive so we need to add 1 to it.
for pattern, match, metadata in query:iter_matches(root, bufnr, start_row, end_row + 1) do
if pattern then
local prepared_match = {}

-- Extract capture names from each match
for id, nodes in pairs(match) do
local query_name = query.captures[id] -- name of the capture in the query
if query_name ~= nil then
local path = vim.split(query_name, "%.")
if metadata[id] and metadata[id].range then
insert_to_path(prepared_match, path, add_bytes(bufnr, metadata[id].range))
else
local srow, scol, sbyte, erow, ecol, ebyte = nodes[1]:range(true)
if #nodes > 1 then
local _, _, _, e_erow, e_ecol, e_ebyte = nodes[#nodes]:range(true)
erow = e_erow
ecol = e_ecol
ebyte = e_ebyte
end
insert_to_path(prepared_match, path, { srow, scol, sbyte, erow, ecol, ebyte })
end
end
end
for id, node, metadata in query:iter_captures(root, bufnr, start_row, end_row + 1) do
local prepared_match = {}

if metadata.range and metadata.range[7] then
---@cast metadata TSTextObjects.Metadata
local query_name = metadata.range[7]
local path = vim.split(query_name, "%.")
insert_to_path(prepared_match, path, {
metadata.range[1],
metadata.range[2],
metadata.range[3],
metadata.range[4],
metadata.range[5],
metadata.range[6],
})
-- Extract capture names from each match
local query_name = query.captures[id] -- name of the capture in the query
if query_name ~= nil then
local path = vim.split(query_name, "%.")

if metadata[id] and metadata[id].range then
insert_to_path(prepared_match, path, add_bytes(bufnr, metadata[id].range))
else
insert_to_path(prepared_match, path, { node:range(true) })
end

matches[#matches + 1] = prepared_match
Expand Down