aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2025-01-14 23:52:37 -0800
committerYuan Fu2025-01-17 17:27:23 -0800
commite3b9de03e641cef5daeada4912fa711470697ba3 (patch)
tree87adc0979b8015542f7b3a6aac38793e4c5f72a2
parent814b45775ffcfa91c03fb54b78611eb97973c387 (diff)
downloademacs-e3b9de03e641cef5daeada4912fa711470697ba3.tar.gz
emacs-e3b9de03e641cef5daeada4912fa711470697ba3.zip
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.
-rw-r--r--lisp/progmodes/c-ts-mode.el37
1 files changed, 6 insertions, 31 deletions
diff --git a/lisp/progmodes/c-ts-mode.el b/lisp/progmodes/c-ts-mode.el
index 213c748ad2d..c5bf135e286 100644
--- a/lisp/progmodes/c-ts-mode.el
+++ b/lisp/progmodes/c-ts-mode.el
@@ -706,22 +706,16 @@ MODE is either `c' or `cpp'."
706 (rx (| "/**" "/*!" "//!" "///")) 706 (rx (| "/**" "/*!" "//!" "///"))
707 "A regexp that matches all doxygen comment styles.") 707 "A regexp that matches all doxygen comment styles.")
708 708
709(defun c-ts-mode--test-virtual-named-p ()
710 "Return t if the virtual keyword is a namded node, nil otherwise."
711 (ignore-errors
712 (progn (treesit-query-compile 'cpp "(virtual)" t) t)))
713
714(defun c-ts-mode--font-lock-settings (mode) 709(defun c-ts-mode--font-lock-settings (mode)
715 "Tree-sitter font-lock settings. 710 "Tree-sitter font-lock settings.
716MODE is either `c' or `cpp'." 711MODE is either `c' or `cpp'."
717 (treesit-font-lock-rules 712 (treesit-font-lock-rules
718 :language mode 713 :default-language mode
719 :feature 'comment 714 :feature 'comment
720 `(((comment) @font-lock-doc-face 715 `(((comment) @font-lock-doc-face
721 (:match ,(rx bos "/**") @font-lock-doc-face)) 716 (:match ,(rx bos "/**") @font-lock-doc-face))
722 (comment) @font-lock-comment-face) 717 (comment) @font-lock-comment-face)
723 718
724 :language mode
725 :feature 'preprocessor 719 :feature 'preprocessor
726 `((preproc_directive) @font-lock-preprocessor-face 720 `((preproc_directive) @font-lock-preprocessor-face
727 721
@@ -744,45 +738,37 @@ MODE is either `c' or `cpp'."
744 ")" @font-lock-preprocessor-face) 738 ")" @font-lock-preprocessor-face)
745 [,@c-ts-mode--preproc-keywords] @font-lock-preprocessor-face) 739 [,@c-ts-mode--preproc-keywords] @font-lock-preprocessor-face)
746 740
747 :language mode
748 :feature 'constant 741 :feature 'constant
749 `((true) @font-lock-constant-face 742 `((true) @font-lock-constant-face
750 (false) @font-lock-constant-face 743 (false) @font-lock-constant-face
751 (null) @font-lock-constant-face) 744 (null) @font-lock-constant-face)
752 745
753 :language mode
754 :feature 'keyword 746 :feature 'keyword
755 `([,@(c-ts-mode--keywords mode)] @font-lock-keyword-face 747 `([,@(c-ts-mode--keywords mode)] @font-lock-keyword-face
756 ,@(when (eq mode 'cpp) 748 ,@(when (eq mode 'cpp)
757 '((auto) @font-lock-keyword-face 749 '((auto) @font-lock-keyword-face
758 (this) @font-lock-keyword-face)) 750 (this) @font-lock-keyword-face))
759 ,@(when (and (eq mode 'cpp) 751 ,@(when (eq mode 'cpp)
760 (c-ts-mode--test-virtual-named-p)) 752 (treesit-query-first-valid 'cpp
761 '((virtual) @font-lock-keyword-face)) 753 '((virtual) @font-lock-keyword-face)
762 ,@(when (and (eq mode 'cpp) 754 '("virtual" @font-lock-keyword-face))))
763 (not (c-ts-mode--test-virtual-named-p))) 755
764 '("virtual" @font-lock-keyword-face)))
765
766 :language mode
767 :feature 'operator 756 :feature 'operator
768 `([,@c-ts-mode--operators 757 `([,@c-ts-mode--operators
769 ,@(when (eq mode 'cpp) c-ts-mode--c++-operators)] 758 ,@(when (eq mode 'cpp) c-ts-mode--c++-operators)]
770 @font-lock-operator-face 759 @font-lock-operator-face
771 "!" @font-lock-negation-char-face) 760 "!" @font-lock-negation-char-face)
772 761
773 :language mode
774 :feature 'string 762 :feature 'string
775 `((string_literal) @font-lock-string-face 763 `((string_literal) @font-lock-string-face
776 (system_lib_string) @font-lock-string-face 764 (system_lib_string) @font-lock-string-face
777 ,@(when (eq mode 'cpp) 765 ,@(when (eq mode 'cpp)
778 '((raw_string_literal) @font-lock-string-face))) 766 '((raw_string_literal) @font-lock-string-face)))
779 767
780 :language mode
781 :feature 'literal 768 :feature 'literal
782 `((number_literal) @font-lock-number-face 769 `((number_literal) @font-lock-number-face
783 (char_literal) @font-lock-constant-face) 770 (char_literal) @font-lock-constant-face)
784 771
785 :language mode
786 :feature 'type 772 :feature 'type
787 `((primitive_type) @font-lock-type-face 773 `((primitive_type) @font-lock-type-face
788 (type_identifier) @font-lock-type-face 774 (type_identifier) @font-lock-type-face
@@ -798,7 +784,6 @@ MODE is either `c' or `cpp'."
798 (namespace_identifier) @font-lock-constant-face)) 784 (namespace_identifier) @font-lock-constant-face))
799 [,@c-ts-mode--type-keywords] @font-lock-type-face) 785 [,@c-ts-mode--type-keywords] @font-lock-type-face)
800 786
801 :language mode
802 :feature 'definition 787 :feature 'definition
803 ;; Highlights identifiers in declarations. 788 ;; Highlights identifiers in declarations.
804 `(,@(when (eq mode 'cpp) 789 `(,@(when (eq mode 'cpp)
@@ -825,7 +810,6 @@ MODE is either `c' or `cpp'."
825 (enumerator 810 (enumerator
826 name: (identifier) @font-lock-property-name-face)) 811 name: (identifier) @font-lock-property-name-face))
827 812
828 :language mode
829 :feature 'assignment 813 :feature 'assignment
830 ;; TODO: Recursively highlight identifiers in parenthesized 814 ;; TODO: Recursively highlight identifiers in parenthesized
831 ;; expressions, see `c-ts-mode--fontify-declarator' for 815 ;; expressions, see `c-ts-mode--fontify-declarator' for
@@ -842,44 +826,35 @@ MODE is either `c' or `cpp'."
842 (identifier) @font-lock-variable-name-face)) 826 (identifier) @font-lock-variable-name-face))
843 (init_declarator declarator: (_) @c-ts-mode--fontify-declarator)) 827 (init_declarator declarator: (_) @c-ts-mode--fontify-declarator))
844 828
845 :language mode
846 :feature 'function 829 :feature 'function
847 '((call_expression 830 '((call_expression
848 function: 831 function:
849 [(identifier) @font-lock-function-call-face 832 [(identifier) @font-lock-function-call-face
850 (field_expression field: (field_identifier) @font-lock-function-call-face)])) 833 (field_expression field: (field_identifier) @font-lock-function-call-face)]))
851 834
852 :language mode
853 :feature 'variable 835 :feature 'variable
854 '((identifier) @c-ts-mode--fontify-variable) 836 '((identifier) @c-ts-mode--fontify-variable)
855 837
856 :language mode
857 :feature 'label 838 :feature 'label
858 '((labeled_statement 839 '((labeled_statement
859 label: (statement_identifier) @font-lock-constant-face)) 840 label: (statement_identifier) @font-lock-constant-face))
860 841
861 :language mode
862 :feature 'error 842 :feature 'error
863 '((ERROR) @c-ts-mode--fontify-error) 843 '((ERROR) @c-ts-mode--fontify-error)
864 844
865 :feature 'escape-sequence 845 :feature 'escape-sequence
866 :language mode
867 :override t 846 :override t
868 '((escape_sequence) @font-lock-escape-face) 847 '((escape_sequence) @font-lock-escape-face)
869 848
870 :language mode
871 :feature 'property 849 :feature 'property
872 '((field_identifier) @font-lock-property-use-face) 850 '((field_identifier) @font-lock-property-use-face)
873 851
874 :language mode
875 :feature 'bracket 852 :feature 'bracket
876 '((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face) 853 '((["(" ")" "[" "]" "{" "}"]) @font-lock-bracket-face)
877 854
878 :language mode
879 :feature 'delimiter 855 :feature 'delimiter
880 '((["," ":" ";"]) @font-lock-delimiter-face) 856 '((["," ":" ";"]) @font-lock-delimiter-face)
881 857
882 :language mode
883 :feature 'emacs-devel 858 :feature 'emacs-devel
884 :override t 859 :override t
885 `(((call_expression 860 `(((call_expression