From 43422e59d83e1ae4c842d51ebd065e1315c53cfc Mon Sep 17 00:00:00 2001 From: Denis Zubarev Date: Sat, 11 Nov 2023 04:55:44 +0300 Subject: [PATCH] Improve syntax highlighting for python-ts-mode * lisp/progmodes/python.el (python--treesit-keywords): Fontify compound keyword "is not". (python--treesit-fontify-string): Fix fontification of strings inside of f-strings interpolation, e.g. for f"beg {'nested'}" - 'nested' was not fontified as string. (python--treesit-fontify-string-interpolation): Remove function. (python--treesit-settings): Do not override the face of builtin functions (all, bytes etc.) with the function call face. Add missing assignment expressions. Fontify built-ins (dict,list,etc.) as types when they are used in type hints. * test/lisp/progmodes/python-tests.el (python-ts-tests-with-temp-buffer): (python-ts-mode-compound-keywords-face): (python-ts-mode-named-assignement-face-1): (python-ts-mode-named-assignement-face-2): (python-ts-mode-nested-types-face-1): (python-ts-mode-nested-types-face-2): (python-ts-mode-builtin-call-face): (python-ts-mode-interpolation-nested-string): (python-ts-mode-interpolation-doc-string): Add tests. --- lisp/progmodes/python.el | 94 +++++++++++++---------- test/lisp/progmodes/python-tests.el | 114 ++++++++++++++++++++++++++++ 2 files changed, 166 insertions(+), 42 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index ab3bf1b4ec..8a178d9c7e 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -979,7 +979,7 @@ python--treesit-keywords "raise" "return" "try" "while" "with" "yield" ;; These are technically operators, but we fontify them as ;; keywords. - "and" "in" "is" "not" "or" "not in")) + "and" "in" "is" "not" "or" "not in" "is not")) (defvar python--treesit-builtins '("abs" "all" "any" "ascii" "bin" "bool" "breakpoint" "bytearray" @@ -1042,9 +1042,7 @@ python--treesit-fontify-string f-strings. OVERRIDE is the override flag described in `treesit-font-lock-rules'. START and END mark the region to be fontified." - (let* ((string-beg (treesit-node-start node)) - (string-end (treesit-node-end node)) - (maybe-expression (treesit-node-parent node)) + (let* ((maybe-expression (treesit-node-parent node)) (grandparent (treesit-node-parent (treesit-node-parent maybe-expression))) @@ -1072,28 +1070,29 @@ python--treesit-fontify-string (equal (treesit-node-type maybe-expression) "expression_statement")) 'font-lock-doc-face - 'font-lock-string-face))) - ;; Don't highlight string prefixes like f/r/b. - (save-excursion - (goto-char string-beg) - (when (re-search-forward "[\"']" string-end t) - (setq string-beg (match-beginning 0)))) - (treesit-fontify-with-override - string-beg string-end face override start end))) + 'font-lock-string-face)) + ;; We will highlight only string_start/string_content/string_end children + ;; Do not touch interpolation node that can occur inside of the string + (string-nodes (treesit-filter-child + node + (lambda (ch) (member (treesit-node-type ch) + '("string_start" + "string_content" + "string_end"))) + t))) + + (dolist (string-node string-nodes) + (let ((string-beg (treesit-node-start string-node)) + (string-end (treesit-node-end string-node))) + (when (equal (treesit-node-type string-node) "string_start") + ;; Don't highlight string prefixes like f/r/b. + (save-excursion + (goto-char string-beg) + (when (re-search-forward "[\"']" string-end t) + (setq string-beg (match-beginning 0))))) -(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)))) + (treesit-fontify-with-override + string-beg string-end face override start end))))) (defvar python--treesit-settings (treesit-font-lock-rules @@ -1103,14 +1102,9 @@ python--treesit-settings :feature 'string :language 'python - '((string) @python--treesit-fontify-string) + '((string) @python--treesit-fontify-string + (interpolation ["{" "}"] @font-lock-misc-punctuation-face)) - ;; 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 - '((interpolation) @python--treesit-fontify-string-interpolation) :feature 'keyword :language 'python @@ -1126,12 +1120,6 @@ python--treesit-settings name: (identifier) @font-lock-type-face) (parameters (identifier) @font-lock-variable-name-face)) - :feature 'function - :language 'python - '((call function: (identifier) @font-lock-function-call-face) - (call function: (attribute - attribute: (identifier) @font-lock-function-call-face))) - :feature 'builtin :language 'python `(((identifier) @font-lock-builtin-face @@ -1142,6 +1130,12 @@ python--treesit-settings eol)) @font-lock-builtin-face))) + :feature 'function + :language 'python + '((call function: (identifier) @font-lock-function-call-face) + (call function: (attribute + attribute: (identifier) @font-lock-function-call-face))) + :feature 'constant :language 'python '([(true) (false) (none)] @font-lock-constant-face) @@ -1151,9 +1145,20 @@ python--treesit-settings `(;; Variable names and LHS. (assignment left: (identifier) @font-lock-variable-name-face) + (assignment left: (subscript value: (identifier) + @font-lock-variable-name-face)) (assignment left: (attribute attribute: (identifier) - @font-lock-property-use-face)) + @font-lock-variable-name-face)) + (assignment left: (subscript (attribute + attribute: (identifier) + @font-lock-variable-name-face))) + (augmented_assignment left: (identifier) + @font-lock-variable-name-face) + (named_expression name: (identifier) + @font-lock-variable-name-face) + (for_statement left: (identifier) + @font-lock-variable-name-face) (pattern_list (identifier) @font-lock-variable-name-face) (tuple_pattern (identifier) @@ -1168,15 +1173,20 @@ python--treesit-settings '((decorator "@" @font-lock-type-face) (decorator (call function: (identifier) @font-lock-type-face)) (decorator (identifier) @font-lock-type-face)) - :feature 'type :language 'python + ;; override built-in faces when dict/list are used for type hints + :override t `(((identifier) @font-lock-type-face (:match ,(rx-to-string `(seq bol (or ,@python--treesit-exceptions) eol)) @font-lock-type-face)) - (type (identifier) @font-lock-type-face)) + (type [(identifier) (none)] @font-lock-type-face) + (type + (_ [(identifier) (none)] @font-lock-type-face + (_ [(identifier) (none)] @font-lock-type-face + (_ [(identifier) (none)] @font-lock-type-face) :?) :?))) :feature 'escape-sequence :language 'python @@ -6842,7 +6852,7 @@ python-ts-mode '(( comment definition) ( keyword string type) ( assignment builtin constant decorator - escape-sequence number string-interpolation ) + escape-sequence number ) ( bracket delimiter function operator variable property))) (setq-local treesit-font-lock-settings python--treesit-settings) (setq-local imenu-create-index-function diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index a44a11896f..f8e1d8d07a 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -7299,6 +7299,120 @@ python-tests--flymake-command-output-pattern "Unused import a.b.c (unused-import)" "W0611: Unused import a.b.c (unused-import)")))))) +;;; python-ts-mode font-lock tests + +(defmacro python-ts-tests-with-temp-buffer (contents &rest body) + "Create a `python-ts-mode' enabled temp buffer with CONTENTS. +BODY is code to be executed within the temp buffer. Point is +always located at the beginning of buffer." + (declare (indent 1) (debug t)) + `(with-temp-buffer + (skip-unless (treesit-ready-p 'python)) + (require 'python) + (let ((python-indent-guess-indent-offset nil)) + (python-ts-mode) + (insert ,contents) + (goto-char (point-min)) + ,@body))) + +(ert-deftest python-ts-mode-compound-keywords-face () + (dolist (test '("is not" "not in")) + (python-ts-tests-with-temp-buffer (concat "t " test " t") + (font-lock-ensure) + (forward-to-word) + (should (eq (face-at-point) font-lock-keyword-face)) + (forward-to-word) + (should (eq (face-at-point) font-lock-keyword-face))))) + +(ert-deftest python-ts-mode-named-assignement-face-1 () + (python-ts-tests-with-temp-buffer "var := 3" + (font-lock-ensure) + (should (eq (face-at-point) font-lock-variable-name-face)))) + +(ert-deftest python-ts-mode-named-assignement-face-2 () + (python-ts-tests-with-temp-buffer "var1[ii] = 1; t.var2[jj] = 2" + (setopt treesit-font-lock-level 3) + (font-lock-ensure) + (dolist (test '("var1" "var2")) + (search-forward test) + (goto-char (match-beginning 0)) + (should (eq (face-at-point) font-lock-variable-name-face))) + (goto-char (point-min)) + (dolist (test '("ii" "jj")) + (search-forward test) + (goto-char (match-beginning 0)) + (should (eq (face-at-point) nil))))) + +(ert-deftest python-ts-mode-nested-types-face-1 () + (python-ts-tests-with-temp-buffer "def func(v:dict[ list[ tuple[str] ], int | None] | None):" + (font-lock-ensure) + (dolist (test '("dict" "list" "tuple" "str" "int" "None" "None")) + (search-forward test) + (goto-char (match-beginning 0)) + (should (eq (face-at-point) font-lock-type-face))))) + +(ert-deftest python-ts-mode-nested-types-face-2 () + (python-ts-tests-with-temp-buffer "def f(val: tuple[tuple, list[Lvl1 | Lvl2[Lvl3[Lvl3], Lvl2]]]):" + (font-lock-ensure) + (dolist (test '("tuple" "tuple" "list" "Lvl1" "Lvl2" "Lvl3" "Lvl3" "Lvl2")) + (search-forward test) + (goto-char (match-beginning 0)) + (should (eq (face-at-point) font-lock-type-face))))) + +(ert-deftest python-ts-mode-builtin-call-face () + (python-ts-tests-with-temp-buffer "all()" + ;; enable 'function' feature from 4th level + (setopt treesit-font-lock-level 4) + (font-lock-ensure) + (should (eq (face-at-point) font-lock-builtin-face)))) + +(ert-deftest python-ts-mode-interpolation-nested-string () + (python-ts-tests-with-temp-buffer "t = f\"beg {True + 'string'}\"" + (font-lock-ensure) + + (search-forward "True") + (goto-char (match-beginning 0)) + (should (eq (face-at-point) font-lock-constant-face)) + + (goto-char (point-min)) + (dolist (test '("f" "{" "+" "}")) + (search-forward test) + (goto-char (match-beginning 0)) + (should (not (eq (face-at-point) font-lock-string-face)))) + + + (goto-char (point-min)) + (dolist (test '("beg" "'string'" "\"")) + (search-forward test) + (goto-char (match-beginning 0)) + (should (eq (face-at-point) font-lock-string-face))))) + +(ert-deftest python-ts-mode-interpolation-doc-string () + (python-ts-tests-with-temp-buffer "f\"\"\"beg {'s1' + True + 's2'} end\"\"\"" + (font-lock-ensure) + + (search-forward "True") + (goto-char (match-beginning 0)) + (should (eq (face-at-point) font-lock-constant-face)) + + (goto-char (point-min)) + (dolist (test '("f" "{" "+" "}")) + (search-forward test) + (goto-char (match-beginning 0)) + (should (not (eq (face-at-point) font-lock-string-face)))) + + (goto-char (point-min)) + (dolist (test '("\"\"\"" "beg" "end" "\"\"\"")) + (search-forward test) + (goto-char (match-beginning 0)) + (should (eq (face-at-point) font-lock-doc-face))) + + (goto-char (point-min)) + (dolist (test '("'s1'" "'s2'")) + (search-forward test) + (goto-char (match-beginning 0)) + (should (eq (face-at-point) font-lock-string-face))))) + (provide 'python-tests) ;;; python-tests.el ends here -- 2.34.1