]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve python-ts-mode fontification (bug#78129)
authorJostein Kjønigsen <jostein@kjonigsen.net>
Thu, 20 Mar 2025 09:39:31 +0000 (10:39 +0100)
committerEshel Yaron <me@eshelyaron.com>
Sat, 10 May 2025 06:53:01 +0000 (08:53 +0200)
Fontification based on selectors & code-conventions:

- Lambda parameters: variable declarations
- For in statements: variable declarations
- Import statements: variable declerations
- Constants: All upper case alphanumerical identifiers
- Types: CamelCase alphanumerical identifiers
- Enums: Enum-values as constants.

* lisp/progmodes/python.el:
(python--treesit-settings): Improve queries.

(cherry picked from commit 2964d51c9cd9b55e60901069c378d0e8277d60f9)

lisp/progmodes/python.el

index e9db1f6af0c86023857116f6c0d482b52498edd9..ee014549c7fd9ee22adae9817d5a3ad954a4eefd 100644 (file)
@@ -1229,7 +1229,15 @@ fontified."
       name: (identifier) @font-lock-type-face)
      (parameters (identifier) @font-lock-variable-name-face)
      (parameters (typed_parameter (identifier) @font-lock-variable-name-face))
-     (parameters (default_parameter name: (identifier) @font-lock-variable-name-face)))
+     (parameters (default_parameter name: (identifier) @font-lock-variable-name-face))
+     (lambda_parameters (identifier) @font-lock-variable-name-face)
+     (for_in_clause
+      left: (identifier) @font-lock-variable-name-face)
+     ((import_from_statement
+       name: ((dotted_name (identifier) @font-lock-type-face)))
+      (:match "\\`[A-Z][A-Za-z0-9]+\\'" @font-lock-type-face))
+     (import_from_statement
+      name: ((dotted_name (identifier) @font-lock-variable-name-face))))
 
    :feature 'builtin
    :language 'python
@@ -1258,7 +1266,12 @@ fontified."
 
    :feature 'constant
    :language 'python
-   '([(true) (false) (none)] @font-lock-constant-face)
+   '([(true) (false) (none)] @font-lock-constant-face
+     ((identifier) @font-lock-constant-face
+      (:match "\\`[A-Z][A-Z0-9_]+\\'" @font-lock-constant-face))
+     ((attribute
+       attribute: (identifier) @font-lock-constant-face)
+      (:match "\\`[A-Z][A-Z0-9_]+\\'" @font-lock-constant-face)))
 
    :feature 'assignment
    :language 'python
@@ -1286,8 +1299,6 @@ fontified."
 
    :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)
@@ -1332,7 +1343,9 @@ fontified."
      ((call function: (identifier) @func-name
             (argument_list :anchor (_)
                            (binary_operator) @python--treesit-fontify-union-types-strict))
-      (:match "^is\\(?:instance\\|subclass\\)$" @func-name)))
+      (:match "^is\\(?:instance\\|subclass\\)$" @func-name))
+     ((identifier) @font-lock-type-face
+      (:match "\\`[A-Z][A-Za-z0-9]+\\'" @font-lock-type-face)))
 
    :feature 'escape-sequence
    :language 'python