mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-21 21:00:08 -04:00
feat(highlights): add is predicate
This commit is contained in:
parent
994baf4539
commit
ae2b88e45f
4 changed files with 43 additions and 18 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,3 +35,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 #pred < 3 then error("is? must have at least two arguments!") 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)
|
||||
|
|
|
|||
|
|
@ -1,23 +1,37 @@
|
|||
; Types
|
||||
|
||||
; Javascript
|
||||
|
||||
; Properties
|
||||
;-----------
|
||||
|
||||
(property_identifier) @property
|
||||
|
||||
; Special identifiers
|
||||
;--------------------
|
||||
|
||||
(identifier) @variable
|
||||
|
||||
((identifier) @constant
|
||||
(#match? @constant "^[A-Z_][A-Z\\d_]+$"))
|
||||
(match? @constant "^[A-Z_][A-Z\\d_]+$"))
|
||||
|
||||
((shorthand_property_identifier) @constant
|
||||
(#match? @constant "^[A-Z_][A-Z\\d_]+$"))
|
||||
(match? @constant "^[A-Z_][A-Z\\d_]+$"))
|
||||
|
||||
((identifier) @constructor
|
||||
(#match? @constructor "^[A-Z]"))
|
||||
(match? @constructor "^[A-Z]"))
|
||||
|
||||
((identifier) @variable.builtin
|
||||
(#match? @variable.builtin "^(arguments|module|console|window|document)$"))
|
||||
(not-is? @variable.builtin import var parameter)
|
||||
(match? @variable.builtin "^(arguments|module|console|window|document)$"))
|
||||
|
||||
((identifier) @function.builtin
|
||||
(#eq? @function.builtin "require"))
|
||||
(not-is? @function.builtin import var parameter)
|
||||
(eq? @function.builtin "require"))
|
||||
|
||||
((identifier) @parameter
|
||||
(is? @parameter parameter))
|
||||
|
||||
|
||||
; Function and method definitions
|
||||
;--------------------------------
|
||||
|
|
@ -78,13 +92,6 @@
|
|||
(rest_parameter
|
||||
(identifier) @parameter))
|
||||
|
||||
(identifier) @variable
|
||||
|
||||
; Properties
|
||||
;-----------
|
||||
|
||||
(property_identifier) @property
|
||||
|
||||
; Literals
|
||||
;---------
|
||||
|
||||
|
|
|
|||
|
|
@ -14,28 +14,28 @@
|
|||
;------------
|
||||
|
||||
(formal_parameters
|
||||
(identifier) @definition.var)
|
||||
(identifier) @definition.parameter)
|
||||
|
||||
(formal_parameters
|
||||
(object_pattern
|
||||
(identifier) @definition.var))
|
||||
(identifier) @definition.parameter))
|
||||
|
||||
; function(arg = []) {
|
||||
(formal_parameters
|
||||
(assignment_pattern
|
||||
(shorthand_property_identifier) @definition.var))
|
||||
(shorthand_property_identifier) @definition.parameter))
|
||||
|
||||
(formal_parameters
|
||||
(object_pattern
|
||||
(shorthand_property_identifier) @definition.var))
|
||||
(shorthand_property_identifier) @definition.parameter))
|
||||
|
||||
(formal_parameters
|
||||
(array_pattern
|
||||
(identifier) @definition.var))
|
||||
(identifier) @definition.parameter))
|
||||
|
||||
(formal_parameters
|
||||
(rest_parameter
|
||||
(identifier) @definition.var))
|
||||
(identifier) @definition.parameter))
|
||||
|
||||
(variable_declarator
|
||||
name: (identifier) @definition.var)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue