fix(treesitter): update c queries

This commit is contained in:
Christian Clason 2023-05-13 12:56:21 +02:00
parent c97de026e3
commit 9ff59517cb
4 changed files with 63 additions and 5 deletions

View File

@ -221,8 +221,7 @@ The following predicates are built in:
similar to `match?`
`contains?` *treesitter-predicate-contains?*
Match a string against parts of the text corresponding to a node:
>query
Match a string against parts of the text corresponding to a node: >query
((identifier) @foo (#contains? @foo "foo"))
((identifier) @foo-bar (#contains? @foo-bar "foo" "bar"))
<
@ -234,6 +233,19 @@ The following predicates are built in:
This is the recommended way to check if the node matches one of many
keywords, as it has been optimized for this.
`has-ancestor?` *treesitter-predicate-has-ancestor?*
Match any of the given node types against all ancestors of a node: >query
((identifier) @variable.builtin
(#any-of? @variable.builtin "begin" "end")
(#has-ancestor? @variable.builtin range_expression))
<
`has-parent?` *treesitter-predicate-has-parent?*
Match any of the given node types against the direct ancestor of a
node: >query
(((field_expression
(field_identifier) @method)) @_parent
(#has-parent? @_parent template_method function_declarator))
<
*lua-treesitter-not-predicate*
Each predicate has a `not-` prefixed predicate that is just the negation of
the predicate.

View File

@ -382,6 +382,39 @@ local predicate_handlers = {
return string_set[node_text]
end,
['has-ancestor?'] = function(match, _, _, predicate)
local node = match[predicate[2]]
if not node then
return true
end
local ancestor_types = {}
for _, type in ipairs({ unpack(predicate, 3) }) do
ancestor_types[type] = true
end
node = node:parent()
while node do
if ancestor_types[node:type()] then
return true
end
node = node:parent()
end
return false
end,
['has-parent?'] = function(match, _, _, predicate)
local node = match[predicate[2]]
if not node then
return true
end
if vim.list_contains({ unpack(predicate, 3) }, node:parent():type()) then
return true
end
return false
end,
}
-- As we provide lua-match? also expose vim-match?

View File

@ -117,12 +117,19 @@
(preproc_defined)
] @function.macro
(field_identifier) @property
(((field_expression
(field_identifier) @property)) @_parent
(#not-has-parent? @_parent template_method function_declarator call_expression))
(field_designator) @property
(((field_identifier) @property)
(#has-ancestor? @property field_declaration)
(#not-has-ancestor? @property function_declarator))
(statement_identifier) @label
[
(type_identifier)
(sized_type_specifier)
(type_descriptor)
] @type
@ -138,6 +145,8 @@
(primitive_type) @type.builtin
(sized_type_specifier _ @type.builtin type: _?)
((identifier) @constant
(#lua-match? @constant "^[A-Z][A-Z0-9_]+$"))
(enumerator
@ -163,6 +172,10 @@
field: (field_identifier) @function.call))
(function_declarator
declarator: (identifier) @function)
(function_declarator
declarator: (parenthesized_declarator
(pointer_declarator
declarator: (field_identifier) @function)))
(preproc_function_def
name: (identifier) @function.macro)

View File

@ -483,7 +483,7 @@ end]]
return list
]]
eq({ 'any-of?', 'contains?', 'eq?', 'is-main?', 'lua-match?', 'match?', 'vim-match?' }, res_list)
eq({ 'any-of?', 'contains?', 'eq?', 'has-ancestor?', 'has-parent?', 'is-main?', 'lua-match?', 'match?', 'vim-match?' }, res_list)
end)