]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix font-lock of Python f-strings
authorkobarity <kobarity@gmail.com>
Wed, 11 Dec 2024 14:21:04 +0000 (23:21 +0900)
committerEshel Yaron <me@eshelyaron.com>
Sun, 15 Dec 2024 16:39:14 +0000 (17:39 +0100)
* lisp/progmodes/python.el (python--font-lock-f-strings): Bind
'parse-sexp-ignore-comments' to nil so that we can look for
closing braces even if a hash is used in the format specifier.
* test/lisp/progmodes/python-tests.el
(python-font-lock-f-string-1): New test.  (Bug#74738)

(cherry picked from commit 71cd290c20dbbd8b14f09818b3c1b58623aea350)

lisp/progmodes/python.el
test/lisp/progmodes/python-tests.el

index 95bd1e49cd040042990eda908cff1ce16ea938ec..477200b57207de10a89eb0c4d59c274b34519b39 100644 (file)
@@ -617,7 +617,9 @@ the {...} holes that appear within f-strings."
               (forward-char 1)          ;Just skip over {{
             (let ((beg (match-beginning 0))
                   (end (condition-case nil
-                           (progn (up-list 1) (min send (point)))
+                           (let ((parse-sexp-ignore-comments))
+                             (up-list 1)
+                             (min send (point)))
                          (scan-error send))))
               (goto-char end)
               (put-text-property beg end 'face nil))))
index 197f639823fd620a8e5183997a83cce4f6f107f7..ae37bcfffce44af892fa87ea2ae59c810b64c49f 100644 (file)
@@ -676,6 +676,14 @@ y = \"confused\""
      (17 . font-lock-operator-face) (18)
      (19 . font-lock-string-face))))
 
+(ert-deftest python-font-lock-f-string-1 ()
+  "Test for bug#74738."
+  (python-tests-assert-faces
+   "print(f\"{value:#x} is a value\")"
+   '((1 . font-lock-builtin-face) (6)
+     (8 . font-lock-string-face) (9)
+     (19 . font-lock-string-face) (31))))
+
 \f
 ;;; Indentation