queries: add is? predicate

This commit is contained in:
Thomas Vigouroux 2020-06-26 22:15:30 +02:00
parent 255d1756b0
commit a2ba854001
2 changed files with 19 additions and 1 deletions

View file

@ -89,4 +89,14 @@ function M.get_references(bufnr)
return refs
end
function M.is(node, deftype, bufnr)
for def in M.get_definitions(bufnr) do
if def[deftype] and def[deftype].node == node then
return true
end
end
return false
end
return M

View file

@ -1,5 +1,6 @@
local api = vim.api
local ts = vim.treesitter
-- local locals = require'nvim-treesitter.locals'
local M = {}
@ -99,7 +100,7 @@ function M.iter_prepared_matches(query, qnode, bufnr, start_row, end_row)
local matches = query:iter_matches(qnode, bufnr, start_row, end_row)
return function()
local function iter()
local pattern, match = matches()
if pattern ~= nil then
local prepared_match = {}
@ -120,12 +121,19 @@ function M.iter_prepared_matches(query, qnode, bufnr, start_row, end_row)
if pred[1] == "set!" and type(pred[2]) == "string" then
insert_to_path(prepared_match, split(pred[2]), pred[3])
end
if pred[1] == "is?" and type(pred[3]) == "string" then
if not locals.is(pred[2], pred[3], bufnr) then
return iter() -- We should ignore this one, tail call
end
end
end
end
return prepared_match
end
end
return iter
end
return M