diff options
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 558868efdf7..a9aff167767 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -1015,12 +1015,24 @@ It makes underscores and dots word constituent chars.") | |||
| 1015 | "VMSError" "WindowsError" | 1015 | "VMSError" "WindowsError" |
| 1016 | )) | 1016 | )) |
| 1017 | 1017 | ||
| 1018 | (defun python--treesit-fontify-string (beg end _) | 1018 | (defun python--treesit-fontify-string (_beg _end node) |
| 1019 | "Fontify string between BEG and END. | 1019 | "Fontify string. |
| 1020 | Do not fontify the initial f for f-strings." | 1020 | NODE is the last quote in the string. Do not fontify the initial |
| 1021 | (let ((beg (if (eq (char-after beg) ?f) | 1021 | f for f-strings." |
| 1022 | (1+ beg) beg))) | 1022 | (let* ((string (treesit-node-parent node)) |
| 1023 | (put-text-property beg end 'face 'font-lock-string-face))) | 1023 | (string-beg (treesit-node-start string)) |
| 1024 | (string-end (treesit-node-end string)) | ||
| 1025 | (maybe-defun (treesit-node-parent | ||
| 1026 | (treesit-node-parent | ||
| 1027 | (treesit-node-parent string)))) | ||
| 1028 | (face (if (member (treesit-node-type maybe-defun) | ||
| 1029 | '("function_definition" | ||
| 1030 | "class_definition")) | ||
| 1031 | 'font-lock-doc-face | ||
| 1032 | 'font-lock-string-face))) | ||
| 1033 | (when (eq (char-after string-beg) ?f) | ||
| 1034 | (cl-incf string-beg)) | ||
| 1035 | (put-text-property string-beg string-end 'face face))) | ||
| 1024 | 1036 | ||
| 1025 | (defvar python--treesit-settings | 1037 | (defvar python--treesit-settings |
| 1026 | (treesit-font-lock-rules | 1038 | (treesit-font-lock-rules |
| @@ -1031,9 +1043,11 @@ Do not fontify the initial f for f-strings." | |||
| 1031 | :feature 'string | 1043 | :feature 'string |
| 1032 | :language 'python | 1044 | :language 'python |
| 1033 | :override t | 1045 | :override t |
| 1034 | '((string) @python--treesit-fontify-string | 1046 | ;; Capture the last quote node rather than the whole string. The |
| 1035 | ((string) @font-lock-doc-face | 1047 | ;; whole string might not be captured if it's not contained in the |
| 1036 | (:match "^\"\"\"" @font-lock-doc-face))) | 1048 | ;; region being fontified. E.g., the user inserts a quote, that |
| 1049 | ;; single quote is the whole region we are fontifying. | ||
| 1050 | '((string "\"" @python--treesit-fontify-string :anchor)) | ||
| 1037 | 1051 | ||
| 1038 | :feature 'string-interpolation | 1052 | :feature 'string-interpolation |
| 1039 | :language 'python | 1053 | :language 'python |