diff options
| author | Laurence Warne | 2022-09-06 12:28:12 +0200 |
|---|---|---|
| committer | Lars Ingebrigtsen | 2022-09-06 12:54:42 +0200 |
| commit | 969983ea1fe4ecca6c714c84b033fa5d0195b753 (patch) | |
| tree | aead65f51af6b2c219ca69f71abc55dd6b04f69f /lisp/progmodes/python.el | |
| parent | 91423627b40fd7a0d2342dc7a419ba8e3bd35fc0 (diff) | |
| download | emacs-969983ea1fe4ecca6c714c84b033fa5d0195b753.tar.gz emacs-969983ea1fe4ecca6c714c84b033fa5d0195b753.zip | |
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).
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 17 |
1 files changed, 14 insertions, 3 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'." | |||
| 546 | font-lock-string-face) | 546 | font-lock-string-face) |
| 547 | font-lock-comment-face)) | 547 | font-lock-comment-face)) |
| 548 | 548 | ||
| 549 | (defconst python--f-string-start-regexp | ||
| 550 | (rx bow | ||
| 551 | (or "f" "F" "fr" "Fr" "fR" "FR" "rf" "rF" "Rf" "RF") | ||
| 552 | (or "\"" "\"\"\"" "'" "'''")) | ||
| 553 | "A regular expression matching the beginning of an f-string. | ||
| 554 | |||
| 555 | See URL `https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals'.") | ||
| 556 | |||
| 549 | (defun python--f-string-p (ppss) | 557 | (defun python--f-string-p (ppss) |
| 550 | "Return non-nil if the pos where PPSS was found is inside an f-string." | 558 | "Return non-nil if the pos where PPSS was found is inside an f-string." |
| 551 | (and (nth 3 ppss) | 559 | (and (nth 3 ppss) |
| 552 | (let ((spos (1- (nth 8 ppss)))) | 560 | (let* ((spos (1- (nth 8 ppss))) |
| 553 | (and (memq (char-after spos) '(?f ?F)) | 561 | (before-quote |
| 562 | (buffer-substring-no-properties (max (- spos 4) (point-min)) | ||
| 563 | (min (+ spos 2) (point-max))))) | ||
| 564 | (and (string-match-p python--f-string-start-regexp before-quote) | ||
| 554 | (or (< (point-min) spos) | 565 | (or (< (point-min) spos) |
| 555 | (not (memq (char-syntax (char-before spos)) '(?w ?_)))))))) | 566 | (not (memq (char-syntax (char-before spos)) '(?w ?_)))))))) |
| 556 | 567 | ||
| @@ -569,7 +580,7 @@ the {...} holes that appear within f-strings." | |||
| 569 | (while | 580 | (while |
| 570 | (progn | 581 | (progn |
| 571 | (while (and (not (python--f-string-p ppss)) | 582 | (while (and (not (python--f-string-p ppss)) |
| 572 | (re-search-forward "\\<f['\"]" limit 'move)) | 583 | (re-search-forward python--f-string-start-regexp limit 'move)) |
| 573 | (setq ppss (syntax-ppss))) | 584 | (setq ppss (syntax-ppss))) |
| 574 | (< (point) limit)) | 585 | (< (point) limit)) |
| 575 | (cl-assert (python--f-string-p ppss)) | 586 | (cl-assert (python--f-string-p ppss)) |