aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorYuan Fu2022-10-30 20:59:50 -0700
committerYuan Fu2022-10-31 00:09:36 -0700
commit79ee266f13fa5c657b24dc45d5f573c393a671b6 (patch)
tree88390bd22acebc84e253d1eaf8ac400e2572d8c0 /lisp/progmodes/python.el
parent377ee8158b2fab3efccb4e9b262e6129b51e28f8 (diff)
downloademacs-79ee266f13fa5c657b24dc45d5f573c393a671b6.tar.gz
emacs-79ee266f13fa5c657b24dc45d5f573c393a671b6.zip
Improve python tree-sitter's string fontification
* lisp/progmodes/python.el (python--treesit-fontify-string): Handle not only f-strings, but also docstrings, and NODE is now the last quote rather than the whole string. (python--treesit-settings): Use python--treesit-fontify-string for every occasion.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el32
1 files changed, 23 insertions, 9 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 558868efdf7..a9aff167767 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1015,12 +1015,24 @@ It makes underscores and dots word constituent chars.")
1015 "VMSError" "WindowsError" 1015 "VMSError" "WindowsError"
1016 )) 1016 ))
1017 1017
1018(defun python--treesit-fontify-string (beg end _) 1018(defun python--treesit-fontify-string (_beg _end node)
1019 "Fontify string between BEG and END. 1019 "Fontify string.
1020Do not fontify the initial f for f-strings." 1020NODE is the last quote in the string. Do not fontify the initial
1021 (let ((beg (if (eq (char-after beg) ?f) 1021f for f-strings."
1022 (1+ beg) beg))) 1022 (let* ((string (treesit-node-parent node))
1023 (put-text-property beg end 'face 'font-lock-string-face))) 1023 (string-beg (treesit-node-start string))
1024 (string-end (treesit-node-end string))
1025 (maybe-defun (treesit-node-parent
1026 (treesit-node-parent
1027 (treesit-node-parent string))))
1028 (face (if (member (treesit-node-type maybe-defun)
1029 '("function_definition"
1030 "class_definition"))
1031 'font-lock-doc-face
1032 'font-lock-string-face)))
1033 (when (eq (char-after string-beg) ?f)
1034 (cl-incf string-beg))
1035 (put-text-property string-beg string-end 'face face)))
1024 1036
1025(defvar python--treesit-settings 1037(defvar python--treesit-settings
1026 (treesit-font-lock-rules 1038 (treesit-font-lock-rules
@@ -1031,9 +1043,11 @@ Do not fontify the initial f for f-strings."
1031 :feature 'string 1043 :feature 'string
1032 :language 'python 1044 :language 'python
1033 :override t 1045 :override t
1034 '((string) @python--treesit-fontify-string 1046 ;; Capture the last quote node rather than the whole string. The
1035 ((string) @font-lock-doc-face 1047 ;; whole string might not be captured if it's not contained in the
1036 (:match "^\"\"\"" @font-lock-doc-face))) 1048 ;; region being fontified. E.g., the user inserts a quote, that
1049 ;; single quote is the whole region we are fontifying.
1050 '((string "\"" @python--treesit-fontify-string :anchor))
1037 1051
1038 :feature 'string-interpolation 1052 :feature 'string-interpolation
1039 :language 'python 1053 :language 'python