From 8c385f05120bdf905a027fe99eab23a59d0f837e Mon Sep 17 00:00:00 2001 From: Yuan Fu Date: Sat, 29 Oct 2022 19:59:50 -0700 Subject: [PATCH] Separate python tree-sitter fontification into features Level 1: comment string function-name class-name Level 2: keyword builtin constant type Level 3: assignment decorator escape-sequence string-interpolation * lisp/progmodes/python.el (python--treesit-settings): Separate. (python-mode): Set features. --- lisp/progmodes/python.el | 82 +++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 31 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 97839366350..558868efdf7 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1024,44 +1024,55 @@ Do not fontify the initial f for f-strings." (defvar python--treesit-settings (treesit-font-lock-rules + :feature 'comment :language 'python - :feature 'basic - '(;; Queries for def and class. - (function_definition - name: (identifier) @font-lock-function-name-face) + '((comment) @font-lock-comment-face) - (class_definition - name: (identifier) @font-lock-type-face) - - ;; Comment and string. - (comment) @font-lock-comment-face - - (string) @python--treesit-fontify-string + :feature 'string + :language 'python + :override t + '((string) @python--treesit-fontify-string ((string) @font-lock-doc-face - (:match "^\"\"\"" @font-lock-doc-face)) - (interpolation (identifier) @font-lock-variable-name-face)) + (:match "^\"\"\"" @font-lock-doc-face))) + + :feature 'string-interpolation :language 'python - :feature 'moderate :override t - `(;; Keywords, builtins, and constants. - [,@python--treesit-keywords] @font-lock-keyword-face + '((interpolation (identifier) @font-lock-variable-name-face)) + :feature 'function-name + :language 'python + '((function_definition + name: (identifier) @font-lock-function-name-face)) + + :feature 'class-name + :language 'python + '((class_definition + name: (identifier) @font-lock-type-face)) + + :feature 'keyword + :language 'python + `([,@python--treesit-keywords] @font-lock-keyword-face ((identifier) @font-lock-keyword-face - (:match "^self$" @font-lock-keyword-face)) + (:match "^self$" @font-lock-keyword-face))) - ((identifier) @font-lock-builtin-face + :feature 'builtin + :language 'python + `(((identifier) @font-lock-builtin-face (:match ,(rx-to-string `(seq bol (or ,@python--treesit-builtins ,@python--treesit-special-attributes) eol)) - @font-lock-builtin-face)) + @font-lock-builtin-face))) - [(true) (false) (none)] @font-lock-constant-face) + :feature 'constant :language 'python - :feature 'elaborate - :override t - `(;; Variable names. + '([(true) (false) (none)] @font-lock-constant-face) + + :feature 'assignment + :language 'python + `(;; Variable names and LHS. (assignment left: (identifier) @font-lock-variable-name-face) (assignment left: (attribute @@ -1074,19 +1085,25 @@ Do not fontify the initial f for f-strings." (list_pattern (identifier) @font-lock-variable-name-face) (list_splat_pattern (identifier) - @font-lock-variable-name-face) + @font-lock-variable-name-face)) - ;; Types and decorators. - (decorator) @font-lock-type-face - ((identifier) @font-lock-type-face + :feature 'decorator + :language 'python + '((decorator) @font-lock-type-face) + + :feature 'type + :language 'python + `(((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) @font-lock-type-face)) - ;; Escape sequences - (escape_sequence) @font-lock-constant-face)) + :feature 'escape-sequence + :language 'python + :override t + '((escape_sequence) @font-lock-constant-face)) "Tree-sitter font-lock settings.") @@ -6469,7 +6486,10 @@ Add import for undefined name `%s' (empty to skip): " ;; Tree-sitter. ((treesit-ready-p 'python-mode 'python) (setq-local treesit-font-lock-feature-list - '((basic) (moderate) (elaborate))) + '(( comment string function-name class-name) + ( keyword builtin constant type) + ( assignment decorator escape-sequence + string-interpolation))) (setq-local treesit-font-lock-settings python--treesit-settings) (setq-local imenu-create-index-function #'python-imenu-treesit-create-index) -- 2.39.2