From f58452e3ae7ed595566028010128c99e3afb572b Mon Sep 17 00:00:00 2001 From: kobarity Date: Sun, 1 Jan 2023 21:09:10 +0900 Subject: Fix 'python-shell-buffer-substring' when START is in middle of 1st line * lisp/progmodes/python.el (python-shell-buffer-substring): Instead of checking whether START is point-min, check whether START is in the first line. (Bug#60466) * test/lisp/progmodes/python-tests.el (python-shell-buffer-substring-18): New test. --- lisp/progmodes/python.el | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'lisp/progmodes/python.el') diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 59164d7d50c..c399bbc64f8 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -3766,15 +3766,16 @@ the python shell: (line-beginning-position) start)))) (substring (buffer-substring-no-properties start end)) - (starts-at-point-min-p (save-restriction - (widen) - (= (point-min) start))) + (starts-at-first-line-p (save-restriction + (widen) + (goto-char start) + (= (line-number-at-pos) 1))) (encoding (python-info-encoding)) (toplevel-p (zerop (save-excursion (goto-char start) (python-util-forward-comment 1) (current-indentation)))) - (fillstr (cond (starts-at-point-min-p + (fillstr (cond (starts-at-first-line-p nil) ((not no-cookie) (concat -- cgit v1.2.1 From 800e15e3be0569efdaa5e42c82937b1c87b7ec58 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Sat, 7 Jan 2023 18:38:24 -0800 Subject: Fix string-interpolation feature of python-ts-mode (bug#60599) * lisp/progmodes/python.el: (python--treesit-fontify-string-interpolation): New function. (python--treesit-settings): Use the new function for string-interpolation. --- lisp/progmodes/python.el | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) (limited to 'lisp/progmodes/python.el') diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index c399bbc64f8..e6ded7a0646 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1072,6 +1072,20 @@ fontified." (treesit-fontify-with-override string-beg string-end face override start end))) +(defun python--treesit-fontify-string-interpolation + (node _ start end &rest _) + "Fontify string interpolation. +NODE is the string node. Do not fontify the initial f for +f-strings. START and END mark the region to be +fontified." + ;; This is kind of a hack, it basically removes the face applied by + ;; the string feature, so that following features can apply their + ;; face. + (let ((n-start (treesit-node-start node)) + (n-end (treesit-node-end node))) + (remove-text-properties + (max start n-start) (min end n-end) '(face)))) + (defvar python--treesit-settings (treesit-font-lock-rules :feature 'comment @@ -1082,10 +1096,12 @@ fontified." :language 'python '((string) @python--treesit-fontify-string) + ;; HACK: This feature must come after the string feature and before + ;; other features. Maybe we should make string-interpolation an + ;; option rather than a feature. :feature 'string-interpolation :language 'python - :override t - '((interpolation (identifier) @font-lock-variable-name-face)) + '((interpolation) @python--treesit-fontify-string-interpolation) :feature 'definition :language 'python -- cgit v1.2.1 From e3d806b4172f16c446bb3c5b31a160ed24fa5244 Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Sat, 7 Jan 2023 18:41:28 -0800 Subject: Fix string fontification on python-ts-mode (bug#60599) * lisp/progmodes/python.el: (python--treesit-fontify-string): Generalize and skip anything before the first quote character. --- lisp/progmodes/python.el | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'lisp/progmodes/python.el') diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index e6ded7a0646..21d16db287c 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1067,8 +1067,11 @@ fontified." "expression_statement")) 'font-lock-doc-face 'font-lock-string-face))) - (when (eq (char-after string-beg) ?f) - (cl-incf string-beg)) + ;; Don't highlight string prefixes like f/r/b. + (save-excursion + (goto-char string-beg) + (when (search-forward "\"" string-end t) + (setq string-beg (match-beginning 0)))) (treesit-fontify-with-override string-beg string-end face override start end))) -- cgit v1.2.1