1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-23 22:20:24 -08:00

Fix python escape code fontification for multi-line literals

* lisp/progmodes/python.el (python--string-bytes-literal-matcher): Go
backward one char after a match so that consecutive escape codes are
highlighted
(python--not-raw-string-literal-start-regexp): Make regular expression
more comprehensive, so multi-line bytes literals are not caught
(python-rx): Accept one to three octal digits in octal escape codes
instead of always three
This commit is contained in:
Laurence Warne 2022-08-09 08:33:18 +01:00 committed by Lars Ingebrigtsen
parent 3ef18c7a21
commit b92e888758
2 changed files with 102 additions and 15 deletions

View file

@ -432,7 +432,7 @@ This variant of `rx' supports common Python named REGEXPS."
(seq (not "\\")
(group (or "\\\\" "\\'" "\\a" "\\b" "\\f"
"\\n" "\\r" "\\t" "\\v"
(seq "\\" (= 3 (in "0-7")))
(seq "\\" (** 1 3 (in "0-7")))
(seq "\\x" hex hex)))))
(string-escape-sequence
(or bytes-escape-sequence
@ -556,7 +556,14 @@ the {...} holes that appear within f-strings."
"A regular expression matching the start of a not-raw bytes literal.")
(defconst python--not-raw-string-literal-start-regexp
(rx (or bos (not alnum)) (? (or "u" "U" "F" "f")) (or "\"" "\"\"\"" "'" "'''") eos)
(rx bos (or
;; Multi-line string literals
(seq (? (? (not alnum)) (or "u" "U" "F" "f")) (or "\"\"\"" "'''"))
(seq (? anychar) (not alnum) (or "\"\"\"" "'''"))
;; Single line string literals
(seq (? (** 0 2 anychar) (not alnum)) (or "u" "U" "F" "f") (or "'" "\""))
(seq (? (** 0 3 anychar) (not (any "'\"" alnum))) (or "'" "\"")))
eos)
"A regular expression matching the start of a not-raw string literal.")
(defun python--string-bytes-literal-matcher (regexp start-regexp)
@ -565,11 +572,12 @@ the {...} holes that appear within f-strings."
(cl-loop for result = (re-search-forward regexp limit t)
for result-valid = (and
result
(let* ((pos (nth 8 (syntax-ppss)))
(before-quote
(buffer-substring-no-properties
(max (- pos 5) (point-min))
(min (+ pos 1) (point-max)))))
(when-let* ((pos (nth 8 (syntax-ppss)))
(before-quote
(buffer-substring-no-properties
(max (- pos 4) (point-min))
(min (+ pos 1) (point-max)))))
(backward-char)
(string-match-p start-regexp before-quote)))
until (or (not result) result-valid)
finally return (and result-valid result))))