mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-02 11:36:54 -04:00
Merge pull request #296 from steelsojka/feat-is-predicate
feat(highlights): add is predicate
This commit is contained in:
commit
4a747aa30c
16 changed files with 102 additions and 39 deletions
|
|
@ -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()
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue