aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorYuan Fu2022-11-01 13:26:16 -0700
committerYuan Fu2022-11-01 13:26:16 -0700
commit50e33639fe190f0a1c47b8e4c0fcc4735cb60909 (patch)
tree5c4e00c02b1c974812a6173a5fdee88376731397 /lisp/progmodes/python.el
parentccd2509ed31f953408240357e9b84c3d3b3f6a2b (diff)
downloademacs-50e33639fe190f0a1c47b8e4c0fcc4735cb60909.tar.gz
emacs-50e33639fe190f0a1c47b8e4c0fcc4735cb60909.zip
Fix string fontification for tree-sitter python-mode
Now when user inserts a ending quote, the whole string is ganranteed to be refontified and redisplayed. * lisp/progmodes/python.el (python--treesit-fontify-string): Change docstring, now it's called on the leading quote. (python--treesit-fontify-string-end): New function. (python--treesit-settings): Capture both leading and ending quote in a string.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el16
1 files changed, 10 insertions, 6 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 916b098aefa..f741688363e 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1017,7 +1017,7 @@ It makes underscores and dots word constituent chars.")
1017 1017
1018(defun python--treesit-fontify-string (_beg _end node override &rest _) 1018(defun python--treesit-fontify-string (_beg _end node override &rest _)
1019 "Fontify string. 1019 "Fontify string.
1020NODE is the last quote in the string. Do not fontify the initial 1020NODE is the leading quote in the string. Do not fontify the initial
1021f for f-strings. OVERRIDE is the override flag described in 1021f for f-strings. OVERRIDE is the override flag described in
1022`treesit-font-lock-rules'." 1022`treesit-font-lock-rules'."
1023 (let* ((string (treesit-node-parent node)) 1023 (let* ((string (treesit-node-parent node))
@@ -1035,6 +1035,12 @@ f for f-strings. OVERRIDE is the override flag described in
1035 (cl-incf string-beg)) 1035 (cl-incf string-beg))
1036 (treesit-fontify-with-override string-beg string-end face override))) 1036 (treesit-fontify-with-override string-beg string-end face override)))
1037 1037
1038(defun python--treesit-fontify-string-end (_beg _end node &rest _)
1039 "Mark the whole string as to-be-fontified.
1040NODE is the ending quote of a string."
1041 (let ((string (treesit-node-parent node)))
1042 (setq jit-lock-context-unfontify-pos (treesit-node-start string))))
1043
1038(defvar python--treesit-settings 1044(defvar python--treesit-settings
1039 (treesit-font-lock-rules 1045 (treesit-font-lock-rules
1040 :feature 'comment 1046 :feature 'comment
@@ -1044,11 +1050,9 @@ f for f-strings. OVERRIDE is the override flag described in
1044 :feature 'string 1050 :feature 'string
1045 :language 'python 1051 :language 'python
1046 :override t 1052 :override t
1047 ;; Capture the last quote node rather than the whole string. The 1053 ;; TODO Document on why we do this.
1048 ;; whole string might not be captured if it's not contained in the 1054 '((string "\"" @python--treesit-fontify-string-end :anchor)
1049 ;; region being fontified. E.g., the user inserts a quote, that 1055 (string :anchor "\"" @python--treesit-fontify-string :anchor))
1050 ;; single quote is the whole region we are fontifying.
1051 '((string "\"" @python--treesit-fontify-string :anchor))
1052 1056
1053 :feature 'string-interpolation 1057 :feature 'string-interpolation
1054 :language 'python 1058 :language 'python