aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorYuan Fu2022-12-26 00:43:42 -0800
committerYuan Fu2022-12-26 01:47:56 -0800
commit8f68b6497ee17791c3a1084ebef164f11cb089c6 (patch)
tree0afdb41572e8a824234f834a0eb8c2a34f34e511 /lisp/progmodes/python.el
parent28f26b11a1ebd46b9f599babf843f91871efb629 (diff)
downloademacs-8f68b6497ee17791c3a1084ebef164f11cb089c6.tar.gz
emacs-8f68b6497ee17791c3a1084ebef164f11cb089c6.zip
Clean up python-ts-mode font-lock features
* lisp/progmodes/python.el (python--treesit-settings): Remove unnecessary override flags, add function and variable feature, fix assignment feature. (python--treesit-variable-p) (python--treesit-fontify-variable): New functions. (python-ts-mode): Add function and variable feature.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el39
1 files changed, 30 insertions, 9 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 0cd0c6c225a..9a6f807f4f2 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1080,7 +1080,6 @@ fontified."
1080 1080
1081 :feature 'string 1081 :feature 'string
1082 :language 'python 1082 :language 'python
1083 :override t
1084 '((string) @python--treesit-fontify-string) 1083 '((string) @python--treesit-fontify-string)
1085 1084
1086 :feature 'string-interpolation 1085 :feature 'string-interpolation
@@ -1130,7 +1129,7 @@ fontified."
1130 @font-lock-variable-name-face) 1129 @font-lock-variable-name-face)
1131 (assignment left: (attribute 1130 (assignment left: (attribute
1132 attribute: (identifier) 1131 attribute: (identifier)
1133 @font-lock-variable-name-face)) 1132 @font-lock-property-face))
1134 (pattern_list (identifier) 1133 (pattern_list (identifier)
1135 @font-lock-variable-name-face) 1134 @font-lock-variable-name-face)
1136 (tuple_pattern (identifier) 1135 (tuple_pattern (identifier)
@@ -1162,12 +1161,10 @@ fontified."
1162 1161
1163 :feature 'number 1162 :feature 'number
1164 :language 'python 1163 :language 'python
1165 :override t
1166 '([(integer) (float)] @font-lock-number-face) 1164 '([(integer) (float)] @font-lock-number-face)
1167 1165
1168 :feature 'property 1166 :feature 'property
1169 :language 'python 1167 :language 'python
1170 :override t
1171 '((attribute 1168 '((attribute
1172 attribute: (identifier) @font-lock-property-face) 1169 attribute: (identifier) @font-lock-property-face)
1173 (class_definition 1170 (class_definition
@@ -1178,20 +1175,44 @@ fontified."
1178 1175
1179 :feature 'operator 1176 :feature 'operator
1180 :language 'python 1177 :language 'python
1181 :override t
1182 `([,@python--treesit-operators] @font-lock-operator-face) 1178 `([,@python--treesit-operators] @font-lock-operator-face)
1183 1179
1184 :feature 'bracket 1180 :feature 'bracket
1185 :language 'python 1181 :language 'python
1186 :override t
1187 '(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face) 1182 '(["(" ")" "[" "]" "{" "}"] @font-lock-bracket-face)
1188 1183
1189 :feature 'delimiter 1184 :feature 'delimiter
1190 :language 'python 1185 :language 'python
1191 :override t 1186 '(["," "." ":" ";" (ellipsis)] @font-lock-delimiter-face)
1192 '(["," "." ":" ";" (ellipsis)] @font-lock-delimiter-face)) 1187
1188 :feature 'variable
1189 :language 'python
1190 '((identifier) @python--treesit-fontify-variable))
1193 "Tree-sitter font-lock settings.") 1191 "Tree-sitter font-lock settings.")
1194 1192
1193(defun python--treesit-variable-p (node)
1194 "Check whether NODE is a variable.
1195NODE's type should be \"identifier\"."
1196 ;; An identifier can be a function/class name, a property, or a
1197 ;; variables. This funtion filters out function/class names and
1198 ;; properties.
1199 (pcase (treesit-node-type (treesit-node-parent node))
1200 ((or "function_definition" "class_definition") nil)
1201 ("attribute"
1202 (pcase (treesit-node-field-name node)
1203 ("object" t)
1204 (_ nil)))
1205 (_ t)))
1206
1207(defun python--treesit-fontify-variable (node override start end &rest _)
1208 "Fontify an identifier node if it is a variable.
1209For NODE, OVERRIDE, START, END, and ARGS, see
1210`treesit-font-lock-rules'."
1211 (when (python--treesit-variable-p node)
1212 (treesit-fontify-with-override
1213 (treesit-node-start node) (treesit-node-end node)
1214 'font-lock-variable-name-face override start end)))
1215
1195 1216
1196;;; Indentation 1217;;; Indentation
1197 1218
@@ -6646,7 +6667,7 @@ implementations: `python-mode' and `python-ts-mode'."
6646 ( keyword string type) 6667 ( keyword string type)
6647 ( assignment builtin constant decorator 6668 ( assignment builtin constant decorator
6648 escape-sequence number property string-interpolation ) 6669 escape-sequence number property string-interpolation )
6649 ( function bracket delimiter operator))) 6670 ( bracket delimiter function operator variable)))
6650 (setq-local treesit-font-lock-settings python--treesit-settings) 6671 (setq-local treesit-font-lock-settings python--treesit-settings)
6651 (setq-local imenu-create-index-function 6672 (setq-local imenu-create-index-function
6652 #'python-imenu-treesit-create-index) 6673 #'python-imenu-treesit-create-index)