Use treesit-query-first-valid in c-ts-mode

* lisp/progmodes/c-ts-mode.el (c-ts-mode--font-lock-settings):
Use treesit-query-first-valid.  Use :default-language keyword.
(c-ts-mode--test-virtual-named-p): Remove function.
This commit is contained in:
Yuan Fu 2025-01-14 23:52:37 -08:00
parent 814b45775f
commit e3b9de03e6
No known key found for this signature in database
GPG key ID: 56E19BC57664A442

View file

@ -706,22 +706,16 @@ MODE is either `c' or `cpp'."
(rx (| "/**" "/*!" "//!" "///"))
"A regexp that matches all doxygen comment styles.")
(defun c-ts-mode--test-virtual-named-p ()
"Return t if the virtual keyword is a namded node, nil otherwise."
(ignore-errors
(progn (treesit-query-compile 'cpp "(virtual)" t) t)))
(defun c-ts-mode--font-lock-settings (mode)
"Tree-sitter font-lock settings.
MODE is either `c' or `cpp'."
(treesit-font-lock-rules
:language mode
:default-language mode
:feature 'comment
`(((comment) @font-lock-doc-face
(:match ,(rx bos "/**") @font-lock-doc-face))
(comment) @font-lock-comment-face)
:language mode
:feature 'preprocessor
`((preproc_directive) @font-lock-preprocessor-face
@ -744,45 +738,37 @@ MODE is either `c' or `cpp'."
")" @font-lock-preprocessor-face)
[,@c-ts-mode--preproc-keywords] @font-lock-preprocessor-face)
:language mode
:feature 'constant
`((true) @font-lock-constant-face
(false) @font-lock-constant-face
(null) @font-lock-constant-face)
:language mode
:feature 'keyword
`([,@(c-ts-mode--keywords mode)] @font-lock-keyword-face
,@(when (eq mode 'cpp)
'((auto) @font-lock-keyword-face
(this) @font-lock-keyword-face))
,@(when (and (eq mode 'cpp)
(c-ts-mode--test-virtual-named-p))
'((virtual) @font-lock-keyword-face))
,@(when (and (eq mode 'cpp)
(not (c-ts-mode--test-virtual-named-p)))
'("virtual" @font-lock-keyword-face)))
,@(when (eq mode 'cpp)
(treesit-query-first-valid 'cpp
'((virtual) @font-lock-keyword-face)
'("virtual" @font-lock-keyword-face))))
:language mode
:feature 'operator
`([,@c-ts-mode--operators
,@(when (eq mode 'cpp) c-ts-mode--c++-operators)]
@font-lock-operator-face
"!" @font-lock-negation-char-face)
:language mode
:feature 'string
`((string_literal) @font-lock-string-face
(system_lib_string) @font-lock-string-face
,@(when (eq mode 'cpp)
'((raw_string_literal) @font-lock-string-face)))
:language mode
:feature 'literal
`((number_literal) @font-lock-number-face
(char_literal) @font-lock-constant-face)
:language mode
:feature 'type
`((primitive_type) @font-lock-type-face
(type_identifier) @font-lock-type-face
@ -798,7 +784,6 @@ MODE is either `c' or `cpp'."
(namespace_identifier) @font-lock-constant-face))
[,@c-ts-mode--type-keywords] @font-lock-type-face)
:language mode
:feature 'definition
;; Highlights identifiers in declarations.
`(,@(when (eq mode 'cpp)
@ -825,7 +810,6 @@ MODE is either `c' or `cpp'."
(enumerator
name: (identifier) @font-lock-property-name-face))
:language mode
:feature 'assignment
;; TODO: Recursively highlight identifiers in parenthesized
;; expressions, see `c-ts-mode--fontify-declarator' for
@ -842,44 +826,35 @@ MODE is either `c' or `cpp'."
(identifier) @font-lock-variable-name-face))
(init_declarator declarator: (_) @c-ts-mode--fontify-declarator))
:language mode
:feature 'function
'((call_expression
function:
[(identifier) @font-lock-function-call-face
(field_expression field: (field_identifier) @font-lock-function-call-face)]))
:language mode
:feature 'variable
'((identifier) @c-ts-mode--fontify-variable)
:language mode
:feature 'label
'((labeled_statement
label: (statement_identifier) @font-lock-constant-face))
:language mode
:feature 'error
'((ERROR) @c-ts-mode--fontify-error)
:feature 'escape-sequence
:language mode
:override t
'((escape_sequence) @font-lock-escape-face)
:language mode
:feature 'property
'((field_identifier) @font-lock-property-use-face)
:language mode
:feature 'bracket
'((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)
:language mode
:feature 'delimiter
'((["," ":" ";"]) @font-lock-delimiter-face)
:language mode
:feature 'emacs-devel
:override t
`(((call_expression