mirror of
https://github.com/nvim-treesitter/nvim-treesitter.git
synced 2026-07-23 05:40:11 -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 configs = require'nvim-treesitter.configs'
|
||||||
local parsers = require'nvim-treesitter.parsers'
|
local parsers = require'nvim-treesitter.parsers'
|
||||||
|
|
||||||
|
-- Registers all query predicates
|
||||||
|
require"nvim-treesitter.query_predicates"
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
function M.setup()
|
function M.setup()
|
||||||
|
|
|
||||||
|
|
@ -35,3 +35,18 @@ query.add_predicate('has-ancestor?', function(match, pattern, bufnr, pred)
|
||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end)
|
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
|
; Types
|
||||||
|
|
||||||
; Javascript
|
; Javascript
|
||||||
|
|
||||||
|
; Properties
|
||||||
|
;-----------
|
||||||
|
|
||||||
|
(property_identifier) @property
|
||||||
|
|
||||||
; Special identifiers
|
; Special identifiers
|
||||||
;--------------------
|
;--------------------
|
||||||
|
|
||||||
|
(identifier) @variable
|
||||||
|
|
||||||
((identifier) @constant
|
((identifier) @constant
|
||||||
(#match? @constant "^[A-Z_][A-Z\\d_]+$"))
|
(match? @constant "^[A-Z_][A-Z\\d_]+$"))
|
||||||
|
|
||||||
((shorthand_property_identifier) @constant
|
((shorthand_property_identifier) @constant
|
||||||
(#match? @constant "^[A-Z_][A-Z\\d_]+$"))
|
(match? @constant "^[A-Z_][A-Z\\d_]+$"))
|
||||||
|
|
||||||
((identifier) @constructor
|
((identifier) @constructor
|
||||||
(#match? @constructor "^[A-Z]"))
|
(match? @constructor "^[A-Z]"))
|
||||||
|
|
||||||
((identifier) @variable.builtin
|
((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
|
((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
|
; Function and method definitions
|
||||||
;--------------------------------
|
;--------------------------------
|
||||||
|
|
@ -78,13 +92,6 @@
|
||||||
(rest_parameter
|
(rest_parameter
|
||||||
(identifier) @parameter))
|
(identifier) @parameter))
|
||||||
|
|
||||||
(identifier) @variable
|
|
||||||
|
|
||||||
; Properties
|
|
||||||
;-----------
|
|
||||||
|
|
||||||
(property_identifier) @property
|
|
||||||
|
|
||||||
; Literals
|
; Literals
|
||||||
;---------
|
;---------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,28 +14,28 @@
|
||||||
;------------
|
;------------
|
||||||
|
|
||||||
(formal_parameters
|
(formal_parameters
|
||||||
(identifier) @definition.var)
|
(identifier) @definition.parameter)
|
||||||
|
|
||||||
(formal_parameters
|
(formal_parameters
|
||||||
(object_pattern
|
(object_pattern
|
||||||
(identifier) @definition.var))
|
(identifier) @definition.parameter))
|
||||||
|
|
||||||
; function(arg = []) {
|
; function(arg = []) {
|
||||||
(formal_parameters
|
(formal_parameters
|
||||||
(assignment_pattern
|
(assignment_pattern
|
||||||
(shorthand_property_identifier) @definition.var))
|
(shorthand_property_identifier) @definition.parameter))
|
||||||
|
|
||||||
(formal_parameters
|
(formal_parameters
|
||||||
(object_pattern
|
(object_pattern
|
||||||
(shorthand_property_identifier) @definition.var))
|
(shorthand_property_identifier) @definition.parameter))
|
||||||
|
|
||||||
(formal_parameters
|
(formal_parameters
|
||||||
(array_pattern
|
(array_pattern
|
||||||
(identifier) @definition.var))
|
(identifier) @definition.parameter))
|
||||||
|
|
||||||
(formal_parameters
|
(formal_parameters
|
||||||
(rest_parameter
|
(rest_parameter
|
||||||
(identifier) @definition.var))
|
(identifier) @definition.parameter))
|
||||||
|
|
||||||
(variable_declarator
|
(variable_declarator
|
||||||
name: (identifier) @definition.var)
|
name: (identifier) @definition.var)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue