Merge pull request #296 from steelsojka/feat-is-predicate

feat(highlights): add is predicate
This commit is contained in:
Steven Sojka 2020-08-16 11:27:15 -05:00 committed by GitHub
commit 4a747aa30c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 102 additions and 39 deletions

View file

@ -5,6 +5,9 @@ local info = require'nvim-treesitter.info'
local configs = require'nvim-treesitter.configs'
local parsers = require'nvim-treesitter.parsers'
-- Registers all query predicates
require"nvim-treesitter.query_predicates"
local M = {}
function M.setup()

View file

@ -35,6 +35,7 @@ hlmap["function"] = "TSFunction"
hlmap["function.builtin"] = "TSFuncBuiltin"
hlmap["function.macro"] = "TSFuncMacro"
hlmap["parameter"] = "TSParameter"
hlmap["parameter.reference"] = "TSParameterReference"
hlmap["method"] = "TSMethod"
hlmap["field"] = "TSField"
hlmap["property"] = "TSProperty"

View file

@ -4,12 +4,25 @@ 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
local function valid_args(name, pred, count, strict_count)
local arg_count = #pred - 1
if strict_count then
if arg_count ~= count then
error(string.format("%s must have exactly %d arguments", name, count))
return false
end
elseif arg_count < count then
error(string.format("%s must have at least %d arguments", name, count))
return false
end
return true
end
query.add_predicate("nth?", function(match, pattern, bufnr, pred)
if not valid_args("nth?", pred, 2, true) then return end
local node = match[pred[2]]
local n = pred[3] - 1
if node and node:parent() and node:named_child_count() > n then
@ -20,7 +33,7 @@ query.add_predicate("nth?", function(match, pattern, bufnr, pred)
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
if not valid_args("has-ancestor?", pred, 2, true) then return end
local node = match[pred[2]]
local ancestor_type = pred[3]
@ -35,3 +48,18 @@ query.add_predicate('has-ancestor?', function(match, pattern, bufnr, pred)
end
return false
end)
query.add_predicate('is?', function(match, pattern, bufnr, pred)
if not valid_args("is?", pred, 2) then return end
-- Avoid circular dependencies
local locals = require"nvim-treesitter.locals"
local node = match[pred[2]]
local types = {unpack(pred, 3)}
if not node then return true end
local _, _, kind = locals.find_definition(node, bufnr)
return vim.tbl_contains(types, kind)
end)