aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYuan Fu2024-09-20 01:16:44 -0700
committerYuan Fu2024-10-02 22:22:28 -0700
commitbf26ff0dc8d1591c9c75e12cfdf057353b3869bf (patch)
tree8f2de444421795d859959f8fd888bd20ccad66c3
parent4c866abab966e85ed1fb57aea9971ce8d1495970 (diff)
downloademacs-bf26ff0dc8d1591c9c75e12cfdf057353b3869bf.tar.gz
emacs-bf26ff0dc8d1591c9c75e12cfdf057353b3869bf.zip
Update csharp-ts-mode font-lock (bug#73369)
Adapt to the latest c-sharp grammar. * lisp/progmodes/csharp-mode.el: (csharp-ts-mode--test-this-expression): (csharp-ts-mode--test-interpolated-string-text): (csharp-ts-mode--test-type-constraint): (csharp-ts-mode--test-type-of-expression): (csharp-ts-mode--test-name-equals): (csharp-ts-mode--test-if-directive): (csharp-ts-mode--test-method-declaration-type-field): New functions. (csharp-ts-mode--type-field): New variable. (csharp-ts-mode--font-lock-settings): Fix font-lock rules.
-rw-r--r--lisp/progmodes/csharp-mode.el155
1 files changed, 118 insertions, 37 deletions
diff --git a/lisp/progmodes/csharp-mode.el b/lisp/progmodes/csharp-mode.el
index 29325ab9632..755303a158d 100644
--- a/lisp/progmodes/csharp-mode.el
+++ b/lisp/progmodes/csharp-mode.el
@@ -730,6 +730,52 @@ compilation and evaluation time conflicts."
730 "readonly" "unmanaged") 730 "readonly" "unmanaged")
731 "C# keywords for tree-sitter font-locking.") 731 "C# keywords for tree-sitter font-locking.")
732 732
733(defun csharp-ts-mode--test-this-expression ()
734 "Return non-nil if (this_expression) is named in csharp grammar."
735 (ignore-errors
736 (treesit-query-compile 'c-sharp "(this_expression)" t)
737 t))
738
739(defun csharp-ts-mode--test-interpolated-string-text ()
740 "Return non-nil if (interpolated_string_text) is in the grammar."
741 (ignore-errors
742 (treesit-query-compile 'c-sharp "(interpolated_string_text)" t)
743 t))
744
745(defun csharp-ts-mode--test-type-constraint ()
746 "Return non-nil if (type_constraint) is in the grammar."
747 (ignore-errors
748 (treesit-query-compile 'c-sharp "(type_constraint)" t)
749 t))
750
751(defun csharp-ts-mode--test-type-of-expression ()
752 "Return non-nil if (type_of_expression) is in the grammar."
753 (ignore-errors
754 (treesit-query-compile 'c-sharp "(type_of_expression)" t)
755 t))
756
757(defun csharp-ts-mode--test-name-equals ()
758 "Return non-nil if (name_equals) is in the grammar."
759 (ignore-errors
760 (treesit-query-compile 'c-sharp "(name_equals)" t)
761 t))
762
763(defun csharp-ts-mode--test-if-directive ()
764 "Return non-nil if (if_directive) is in the grammar."
765 (ignore-errors
766 (treesit-query-compile 'c-sharp "(if_directive)" t)
767 t))
768
769(defun csharp-ts-mode--test-method-declaration-type-field ()
770 "Return non-nil if (method_declaration) has a type field."
771 (ignore-errors
772 (treesit-query-compile 'c-sharp "(method_declaration type: (_))" t)
773 t))
774
775(defvar csharp-ts-mode--type-field
776 (if (csharp-ts-mode--test-method-declaration-type-field)
777 'type: 'returns:))
778
733(defvar csharp-ts-mode--font-lock-settings 779(defvar csharp-ts-mode--font-lock-settings
734 (treesit-font-lock-rules 780 (treesit-font-lock-rules
735 :language 'c-sharp 781 :language 'c-sharp
@@ -760,7 +806,9 @@ compilation and evaluation time conflicts."
760 :feature 'keyword 806 :feature 'keyword
761 `([,@csharp-ts-mode--keywords] @font-lock-keyword-face 807 `([,@csharp-ts-mode--keywords] @font-lock-keyword-face
762 (modifier) @font-lock-keyword-face 808 (modifier) @font-lock-keyword-face
763 (this_expression) @font-lock-keyword-face) 809 ,@(if (csharp-ts-mode--test-this-expression)
810 '((this_expression) @font-lock-keyword-face)
811 '("this" @font-lock-keyword-face)))
764 812
765 :language 'c-sharp 813 :language 'c-sharp
766 :override t 814 :override t
@@ -786,18 +834,23 @@ compilation and evaluation time conflicts."
786 :feature 'string 834 :feature 'string
787 `([(string_literal) 835 `([(string_literal)
788 (verbatim_string_literal) 836 (verbatim_string_literal)
789 (interpolated_string_text) 837 ,@(if (csharp-ts-mode--test-interpolated-string-text)
790 (interpolated_verbatim_string_text) 838 '((interpolated_string_text)
791 (character_literal) 839 (interpolated_verbatim_string_text)
792 "\"" 840 (character_literal)
793 "$\"" 841 "\""
794 "@$\"" 842 "$\""
795 "$@\""] @font-lock-string-face) 843 "@$\""
844 "$@\"")
845 '((interpolated_string_expression)
846 (interpolation_start)
847 (interpolation_quote)))]
848 @font-lock-string-face)
796 849
797 :language 'c-sharp 850 :language 'c-sharp
798 :override t 851 :override t
799 :feature 'type 852 :feature 'type
800 '((predefined_type) @font-lock-type-face 853 `((predefined_type) @font-lock-type-face
801 (implicit_type) @font-lock-type-face 854 (implicit_type) @font-lock-type-face
802 (nullable_type) @font-lock-type-face 855 (nullable_type) @font-lock-type-face
803 (type_parameter 856 (type_parameter
@@ -816,10 +869,17 @@ compilation and evaluation time conflicts."
816 (cast_expression (generic_name (identifier) @font-lock-type-face)) 869 (cast_expression (generic_name (identifier) @font-lock-type-face))
817 ["operator"] @font-lock-type-face 870 ["operator"] @font-lock-type-face
818 (type_parameter_constraints_clause 871 (type_parameter_constraints_clause
819 target: (identifier) @font-lock-type-face) 872 (identifier) @font-lock-type-face)
820 (type_constraint type: (identifier) @font-lock-type-face) 873 ,@(if (csharp-ts-mode--test-type-constraint)
821 (type_constraint type: (generic_name (identifier) @font-lock-type-face)) 874 '((type_constraint type: (identifier) @font-lock-type-face)
822 (type_of_expression (identifier) @font-lock-type-face) 875 (type_constraint type: (generic_name (identifier) @font-lock-type-face)))
876 '((type_parameter_constraint (type type: (identifier) @font-lock-type-face))
877 (type_parameter_constraint (type type: (generic_name (identifier) @font-lock-type-face)))))
878
879 ,@(when (csharp-ts-mode--test-type-of-expression)
880 '((type_of_expression (identifier) @font-lock-type-face))
881 '((typeof_expression (identifier) @font-lock-type-face)))
882
823 (object_creation_expression 883 (object_creation_expression
824 type: (identifier) @font-lock-type-face) 884 type: (identifier) @font-lock-type-face)
825 (object_creation_expression 885 (object_creation_expression
@@ -832,8 +892,9 @@ compilation and evaluation time conflicts."
832 :override t 892 :override t
833 `((qualified_name (identifier) @font-lock-type-face) 893 `((qualified_name (identifier) @font-lock-type-face)
834 (using_directive (identifier) @font-lock-type-face) 894 (using_directive (identifier) @font-lock-type-face)
835 (using_directive (name_equals 895 ,@(when (csharp-ts-mode--test-name-equals)
836 (identifier) @font-lock-type-face)) 896 '((using_directive (name_equals
897 (identifier) @font-lock-type-face))))
837 898
838 (enum_declaration (identifier) @font-lock-type-face) 899 (enum_declaration (identifier) @font-lock-type-face)
839 (enum_member_declaration (identifier) @font-lock-variable-name-face) 900 (enum_member_declaration (identifier) @font-lock-variable-name-face)
@@ -861,10 +922,10 @@ compilation and evaluation time conflicts."
861 ;;; Check if keyword void_keyword is available, then return the correct rule." 922 ;;; Check if keyword void_keyword is available, then return the correct rule."
862 ,@(condition-case nil 923 ,@(condition-case nil
863 (progn (treesit-query-capture 'csharp '((void_keyword) @capture)) 924 (progn (treesit-query-capture 'csharp '((void_keyword) @capture))
864 `((method_declaration type: [(identifier) (void_keyword)] @font-lock-type-face))) 925 `((method_declaration ,csharp-ts-mode--type-field [(identifier) (void_keyword)] @font-lock-type-face)))
865 (error 926 (error
866 `((method_declaration type: [(identifier) (predefined_type)] @font-lock-type-face)))) 927 `((method_declaration ,csharp-ts-mode--type-field [(identifier) (predefined_type)] @font-lock-type-face))))
867 (method_declaration type: (generic_name (identifier) @font-lock-type-face)) 928 (method_declaration ,csharp-ts-mode--type-field (generic_name (identifier) @font-lock-type-face))
868 (method_declaration name: (_) @font-lock-function-name-face) 929 (method_declaration name: (_) @font-lock-function-name-face)
869 930
870 (catch_declaration 931 (catch_declaration
@@ -907,25 +968,45 @@ compilation and evaluation time conflicts."
907 :language 'c-sharp 968 :language 'c-sharp
908 :feature 'directives 969 :feature 'directives
909 :override t 970 :override t
910 '((if_directive 971 (if (csharp-ts-mode--test-if-directive)
911 "if" @font-lock-preprocessor-face 972 '((if_directive
912 (identifier) @font-lock-variable-use-face) 973 "if" @font-lock-preprocessor-face
913 (elif_directive 974 (identifier) @font-lock-variable-use-face)
914 "elif" @font-lock-preprocessor-face 975 (elif_directive
915 (identifier) @font-lock-variable-use-face) 976 "elif" @font-lock-preprocessor-face
916 (else_directive) @font-lock-preprocessor-face 977 (identifier) @font-lock-variable-use-face)
917 (endif_directive) @font-lock-preprocessor-face 978 (else_directive) @font-lock-preprocessor-face
918 (define_directive 979 (endif_directive) @font-lock-preprocessor-face
919 "define" @font-lock-preprocessor-face 980 (define_directive
920 (identifier) @font-lock-variable-use-face) 981 "define" @font-lock-preprocessor-face
921 (nullable_directive) @font-lock-preprocessor-face 982 (identifier) @font-lock-variable-use-face)
922 (pragma_directive) @font-lock-preprocessor-face 983 (nullable_directive) @font-lock-preprocessor-face
923 (region_directive) @font-lock-preprocessor-face 984 (pragma_directive) @font-lock-preprocessor-face
924 (endregion_directive) @font-lock-preprocessor-face 985 (region_directive) @font-lock-preprocessor-face
925 (region_directive 986 (endregion_directive) @font-lock-preprocessor-face
926 (preproc_message) @font-lock-variable-use-face) 987 (region_directive
927 (endregion_directive 988 (preproc_message) @font-lock-variable-use-face)
928 (preproc_message) @font-lock-variable-use-face)))) 989 (endregion_directive
990 (preproc_message) @font-lock-variable-use-face))
991 '((preproc_if
992 "#if" @font-lock-preprocessor-face
993 (identifier) @font-lock-variable-use-face)
994 (preproc_elif
995 "#elif" @font-lock-preprocessor-face
996 (identifier) @font-lock-variable-use-face)
997 (preproc_else) @font-lock-preprocessor-face
998 "#endif" @font-lock-preprocessor-face
999 (preproc_define
1000 "#define" @font-lock-preprocessor-face
1001 (preproc_arg) @font-lock-variable-use-face)
1002 (preproc_nullable) @font-lock-preprocessor-face
1003 (preproc_pragma) @font-lock-preprocessor-face
1004 (preproc_region) @font-lock-preprocessor-face
1005 (preproc_endregion) @font-lock-preprocessor-face
1006 (preproc_region
1007 (preproc_arg) @font-lock-variable-use-face)
1008 (preproc_endregion
1009 (preproc_arg) @font-lock-variable-use-face)))))
929 1010
930;;;###autoload 1011;;;###autoload
931(add-to-list 'auto-mode-alist '("\\.cs\\'" . csharp-mode)) 1012(add-to-list 'auto-mode-alist '("\\.cs\\'" . csharp-mode))