From 50e33639fe190f0a1c47b8e4c0fcc4735cb60909 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Tue, 1 Nov 2022 13:26:16 -0700 Subject: [PATCH] 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. --- lisp/progmodes/python.el | 16 ++++++++++------ 1 file 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.") (defun python--treesit-fontify-string (_beg _end node override &rest _) "Fontify string. -NODE is the last quote in the string. Do not fontify the initial +NODE is the leading quote in the string. Do not fontify the initial f for f-strings. OVERRIDE is the override flag described in `treesit-font-lock-rules'." (let* ((string (treesit-node-parent node)) @@ -1035,6 +1035,12 @@ f for f-strings. OVERRIDE is the override flag described in (cl-incf string-beg)) (treesit-fontify-with-override string-beg string-end face override))) +(defun python--treesit-fontify-string-end (_beg _end node &rest _) + "Mark the whole string as to-be-fontified. +NODE is the ending quote of a string." + (let ((string (treesit-node-parent node))) + (setq jit-lock-context-unfontify-pos (treesit-node-start string)))) + (defvar python--treesit-settings (treesit-font-lock-rules :feature 'comment @@ -1044,11 +1050,9 @@ f for f-strings. OVERRIDE is the override flag described in :feature 'string :language 'python :override t - ;; Capture the last quote node rather than the whole string. The - ;; whole string might not be captured if it's not contained in the - ;; region being fontified. E.g., the user inserts a quote, that - ;; single quote is the whole region we are fontifying. - '((string "\"" @python--treesit-fontify-string :anchor)) + ;; TODO Document on why we do this. + '((string "\"" @python--treesit-fontify-string-end :anchor) + (string :anchor "\"" @python--treesit-fontify-string :anchor)) :feature 'string-interpolation :language 'python -- 2.39.5