aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorYuan Fu2022-10-29 19:59:50 -0700
committerYuan Fu2022-10-29 19:59:50 -0700
commit8c385f05120bdf905a027fe99eab23a59d0f837e (patch)
treed44f6ea7ee283a1c2d14aec64b49812fb77bc7df /lisp/progmodes/python.el
parentf6483526cd4b4b9ed7d830599add82ecb45009e6 (diff)
downloademacs-8c385f05120bdf905a027fe99eab23a59d0f837e.tar.gz
emacs-8c385f05120bdf905a027fe99eab23a59d0f837e.zip
Separate python tree-sitter fontification into features
Level 1: comment string function-name class-name Level 2: keyword builtin constant type Level 3: assignment decorator escape-sequence string-interpolation * lisp/progmodes/python.el (python--treesit-settings): Separate. (python-mode): Set features.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el82
1 files changed, 51 insertions, 31 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 97839366350..558868efdf7 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1024,44 +1024,55 @@ Do not fontify the initial f for f-strings."
1024 1024
1025(defvar python--treesit-settings 1025(defvar python--treesit-settings
1026 (treesit-font-lock-rules 1026 (treesit-font-lock-rules
1027 :feature 'comment
1027 :language 'python 1028 :language 'python
1028 :feature 'basic 1029 '((comment) @font-lock-comment-face)
1029 '(;; Queries for def and class.
1030 (function_definition
1031 name: (identifier) @font-lock-function-name-face)
1032 1030
1033 (class_definition 1031 :feature 'string
1034 name: (identifier) @font-lock-type-face) 1032 :language 'python
1035 1033 :override t
1036 ;; Comment and string. 1034 '((string) @python--treesit-fontify-string
1037 (comment) @font-lock-comment-face
1038
1039 (string) @python--treesit-fontify-string
1040 ((string) @font-lock-doc-face 1035 ((string) @font-lock-doc-face
1041 (:match "^\"\"\"" @font-lock-doc-face)) 1036 (:match "^\"\"\"" @font-lock-doc-face)))
1042 (interpolation (identifier) @font-lock-variable-name-face)) 1037
1038 :feature 'string-interpolation
1043 :language 'python 1039 :language 'python
1044 :feature 'moderate
1045 :override t 1040 :override t
1046 `(;; Keywords, builtins, and constants. 1041 '((interpolation (identifier) @font-lock-variable-name-face))
1047 [,@python--treesit-keywords] @font-lock-keyword-face
1048 1042
1043 :feature 'function-name
1044 :language 'python
1045 '((function_definition
1046 name: (identifier) @font-lock-function-name-face))
1047
1048 :feature 'class-name
1049 :language 'python
1050 '((class_definition
1051 name: (identifier) @font-lock-type-face))
1052
1053 :feature 'keyword
1054 :language 'python
1055 `([,@python--treesit-keywords] @font-lock-keyword-face
1049 ((identifier) @font-lock-keyword-face 1056 ((identifier) @font-lock-keyword-face
1050 (:match "^self$" @font-lock-keyword-face)) 1057 (:match "^self$" @font-lock-keyword-face)))
1051 1058
1052 ((identifier) @font-lock-builtin-face 1059 :feature 'builtin
1060 :language 'python
1061 `(((identifier) @font-lock-builtin-face
1053 (:match ,(rx-to-string 1062 (:match ,(rx-to-string
1054 `(seq bol 1063 `(seq bol
1055 (or ,@python--treesit-builtins 1064 (or ,@python--treesit-builtins
1056 ,@python--treesit-special-attributes) 1065 ,@python--treesit-special-attributes)
1057 eol)) 1066 eol))
1058 @font-lock-builtin-face)) 1067 @font-lock-builtin-face)))
1059 1068
1060 [(true) (false) (none)] @font-lock-constant-face) 1069 :feature 'constant
1061 :language 'python 1070 :language 'python
1062 :feature 'elaborate 1071 '([(true) (false) (none)] @font-lock-constant-face)
1063 :override t 1072
1064 `(;; Variable names. 1073 :feature 'assignment
1074 :language 'python
1075 `(;; Variable names and LHS.
1065 (assignment left: (identifier) 1076 (assignment left: (identifier)
1066 @font-lock-variable-name-face) 1077 @font-lock-variable-name-face)
1067 (assignment left: (attribute 1078 (assignment left: (attribute
@@ -1074,19 +1085,25 @@ Do not fontify the initial f for f-strings."
1074 (list_pattern (identifier) 1085 (list_pattern (identifier)
1075 @font-lock-variable-name-face) 1086 @font-lock-variable-name-face)
1076 (list_splat_pattern (identifier) 1087 (list_splat_pattern (identifier)
1077 @font-lock-variable-name-face) 1088 @font-lock-variable-name-face))
1078 1089
1079 ;; Types and decorators. 1090 :feature 'decorator
1080 (decorator) @font-lock-type-face 1091 :language 'python
1081 ((identifier) @font-lock-type-face 1092 '((decorator) @font-lock-type-face)
1093
1094 :feature 'type
1095 :language 'python
1096 `(((identifier) @font-lock-type-face
1082 (:match ,(rx-to-string 1097 (:match ,(rx-to-string
1083 `(seq bol (or ,@python--treesit-exceptions) 1098 `(seq bol (or ,@python--treesit-exceptions)
1084 eol)) 1099 eol))
1085 @font-lock-type-face)) 1100 @font-lock-type-face))
1086 (type (identifier) @font-lock-type-face) 1101 (type (identifier) @font-lock-type-face))
1087 1102
1088 ;; Escape sequences 1103 :feature 'escape-sequence
1089 (escape_sequence) @font-lock-constant-face)) 1104 :language 'python
1105 :override t
1106 '((escape_sequence) @font-lock-constant-face))
1090 "Tree-sitter font-lock settings.") 1107 "Tree-sitter font-lock settings.")
1091 1108
1092 1109
@@ -6469,7 +6486,10 @@ Add import for undefined name `%s' (empty to skip): "
6469 ;; Tree-sitter. 6486 ;; Tree-sitter.
6470 ((treesit-ready-p 'python-mode 'python) 6487 ((treesit-ready-p 'python-mode 'python)
6471 (setq-local treesit-font-lock-feature-list 6488 (setq-local treesit-font-lock-feature-list
6472 '((basic) (moderate) (elaborate))) 6489 '(( comment string function-name class-name)
6490 ( keyword builtin constant type)
6491 ( assignment decorator escape-sequence
6492 string-interpolation)))
6473 (setq-local treesit-font-lock-settings python--treesit-settings) 6493 (setq-local treesit-font-lock-settings python--treesit-settings)
6474 (setq-local imenu-create-index-function 6494 (setq-local imenu-create-index-function
6475 #'python-imenu-treesit-create-index) 6495 #'python-imenu-treesit-create-index)