From 969983ea1fe4ecca6c714c84b033fa5d0195b753 Mon Sep 17 00:00:00 2001 From: Laurence Warne Date: Tue, 6 Sep 2022 12:28:12 +0200 Subject: [PATCH] Apply syntax highlighting for all python f-strings * lisp/progmodes/python.el (python--f-string-p) (python--font-lock-f-strings): Edit functions to use a regular expression matching all f-strings (bug#56757). --- lisp/progmodes/python.el | 17 ++++++++++++++--- test/lisp/progmodes/python-tests.el | 12 ++++++++---- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 147c5f248d2..3247d7ad507 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -546,11 +546,22 @@ The type returned can be `comment', `string' or `paren'." font-lock-string-face) font-lock-comment-face)) +(defconst python--f-string-start-regexp + (rx bow + (or "f" "F" "fr" "Fr" "fR" "FR" "rf" "rF" "Rf" "RF") + (or "\"" "\"\"\"" "'" "'''")) + "A regular expression matching the beginning of an f-string. + +See URL `https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals'.") + (defun python--f-string-p (ppss) "Return non-nil if the pos where PPSS was found is inside an f-string." (and (nth 3 ppss) - (let ((spos (1- (nth 8 ppss)))) - (and (memq (char-after spos) '(?f ?F)) + (let* ((spos (1- (nth 8 ppss))) + (before-quote + (buffer-substring-no-properties (max (- spos 4) (point-min)) + (min (+ spos 2) (point-max))))) + (and (string-match-p python--f-string-start-regexp before-quote) (or (< (point-min) spos) (not (memq (char-syntax (char-before spos)) '(?w ?_)))))))) @@ -569,7 +580,7 @@ the {...} holes that appear within f-strings." (while (progn (while (and (not (python--f-string-p ppss)) - (re-search-forward "\\