2020-08-11 23:20:21 +02:00
|
|
|
local query = require"vim.treesitter.query"
|
2020-07-20 17:48:58 +02:00
|
|
|
|
2020-08-11 23:20:21 +02:00
|
|
|
local function error(str)
|
|
|
|
|
vim.api.nvim_err_writeln(str)
|
2020-07-20 17:48:58 +02:00
|
|
|
end
|
|
|
|
|
|
2020-08-11 23:20:21 +02:00
|
|
|
query.add_predicate("nth?", function(match, pattern, bufnr, pred)
|
|
|
|
|
if #pred ~= 3 then
|
|
|
|
|
error("nth? must hav exactly two arguments")
|
|
|
|
|
return
|
2020-07-31 08:01:07 -05:00
|
|
|
end
|
|
|
|
|
|
2020-08-11 23:20:21 +02:00
|
|
|
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
|
2020-07-20 17:48:58 +02:00
|
|
|
end
|
|
|
|
|
|
2020-08-11 23:20:21 +02:00
|
|
|
return false
|
|
|
|
|
end)
|
2020-07-20 17:48:58 +02:00
|
|
|
|
2020-08-11 23:20:21 +02:00
|
|
|
query.add_predicate('has-ancestor?', function(match, pattern, bufnr, pred)
|
|
|
|
|
if #pred ~= 3 then error("has-ancestor? must have exactly two arguments!") return end
|
2020-07-20 17:48:58 +02:00
|
|
|
|
2020-08-11 23:20:21 +02:00
|
|
|
local node = match[pred[2]]
|
2020-07-24 21:03:46 +02:00
|
|
|
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
|
2020-08-11 23:20:21 +02:00
|
|
|
end)
|