]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix python-ts-mode triple quote syntax (bug#67262)
authorYuan Fu <casouri@gmail.com>
Sat, 23 Dec 2023 23:49:32 +0000 (15:49 -0800)
committerEshel Yaron <me@eshelyaron.com>
Tue, 2 Jan 2024 07:16:09 +0000 (08:16 +0100)
* lisp/progmodes/python.el (python--treesit-syntax-propertize): New function.
(python-ts-mode): Activate python--treesit-syntax-propertize.

(cherry picked from commit 2701da0eee54d85f79104c7a91610bf591159a51)

lisp/progmodes/python.el

index 211969140ab742cfc941a50e05a5609b4421fa25..ff9402eaaaf745ae804f57ffc2813ecb4be95eab 100644 (file)
@@ -1238,6 +1238,21 @@ For NODE, OVERRIDE, START, END, and ARGS, see
      (treesit-node-start node) (treesit-node-end node)
      'font-lock-variable-use-face override start end)))
 
+(defun python--treesit-syntax-propertize (start end)
+  "Propertize triple-quote strings between START and END."
+  (save-excursion
+    (goto-char start)
+    (while (re-search-forward (rx (or "\"\"\"" "'''")) end t)
+      (let ((node (treesit-node-at (point))))
+        ;; The triple quotes surround a non-empty string.
+        (when (equal (treesit-node-type node) "string_content")
+          (let ((start (treesit-node-start node))
+                (end (treesit-node-end node)))
+            (put-text-property (1- start) start
+                               'syntax-table (string-to-syntax "|"))
+            (put-text-property end (min (1+ end) (point-max))
+                               'syntax-table (string-to-syntax "|"))))))))
+
 \f
 ;;; Indentation
 
@@ -6854,6 +6869,8 @@ implementations: `python-mode' and `python-ts-mode'."
                 #'python--treesit-defun-name)
     (treesit-major-mode-setup)
 
+    (setq-local syntax-propertize-function #'python--treesit-syntax-propertize)
+
     (python-skeleton-add-menu-items)
 
     (when python-indent-guess-indent-offset