Add predicates module

This commit is contained in:
Stephan Seitz 2020-07-20 17:48:58 +02:00 committed by Thomas Vigouroux
parent 6f01384cb2
commit 5462fc92cb
2 changed files with 70 additions and 1 deletions

View file

@ -2,6 +2,7 @@ local api = vim.api
local ts = vim.treesitter
local utils = require'nvim-treesitter.utils'
local parsers = require'nvim-treesitter.parsers'
local predicates = require'nvim-treesitter.query_predicates'
local M = {}
@ -129,7 +130,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 iterator()
local pattern, match = matches()
if pattern ~= nil then
local prepared_match = {}
@ -147,15 +148,25 @@ function M.iter_prepared_matches(query, qnode, bufnr, start_row, end_row)
local preds = query.info.patterns[pattern]
if preds then
for _, pred in pairs(preds) do
-- functions
if pred[1] == "set!" and type(pred[2]) == "string" then
insert_to_path(prepared_match, split(pred[2]), pred[3])
end
-- predicates
if type(pred[1]) == 'string' then
if not predicates.check_predicate(query, prepared_match, pred) or
not predicates.check_negated_predicate(query, prepared_match, pred) then
return iterator()
end
end
end
end
return prepared_match
end
end
return iterator
end
--- Return all nodes corresponding to a specific capture path (like @definition.var, @reference.type)