mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-02 11:36:54 -04:00
37 lines
868 B
Lua
37 lines
868 B
Lua
local query = require"vim.treesitter.query"
|
|
|
|
local function error(str)
|
|
vim.api.nvim_err_writeln(str)
|
|
end
|
|
|
|
query.add_predicate("nth?", function(match, pattern, bufnr, pred)
|
|
if #pred ~= 3 then
|
|
error("nth? must hav exactly two arguments")
|
|
return
|
|
end
|
|
|
|
local node = match[pred[2]]
|
|
local n = pred[3] - 1
|
|
if node and node:parent() and node:named_child_count() > n then
|
|
return node:named_child(n) == node
|
|
end
|
|
|
|
return false
|
|
end)
|
|
|
|
query.add_predicate('has-ancestor?', function(match, pattern, bufnr, pred)
|
|
if #pred ~= 3 then error("has-ancestor? must have exactly two arguments!") return end
|
|
|
|
local node = match[pred[2]]
|
|
local ancestor_type = pred[3]
|
|
if not node then return true end
|
|
|
|
node = node:parent()
|
|
while node do
|
|
if node:type() == ancestor_type then
|
|
return true
|
|
end
|
|
node = node:parent()
|
|
end
|
|
return false
|
|
end)
|