Function: python--treesit-fontify-union-types

python--treesit-fontify-union-types is a byte-compiled function defined in python.el.gz.

Signature

(python--treesit-fontify-union-types NODE OVERRIDE START END &optional TYPE-REGEX &rest _)

Documentation

Fontify nested union types in the type hints.

For example, Lvl1 | Lvl2[Lvl3[Lvl4[Lvl5 | None]], Lvl2]. This structure is represented via nesting binary_operator and subscript nodes. This function iterates over all levels and highlight identifier nodes. If TYPE-REGEX is not nil fontify type identifier only if it matches against TYPE-REGEX. NODE is the binary_operator node. OVERRIDE is the override flag described in treesit-font-lock-rules. START and END mark the region to be fontified.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
(defun python--treesit-fontify-union-types (node override start end &optional type-regex &rest _)
  "Fontify nested union types in the type hints.
For example, Lvl1 | Lvl2[Lvl3[Lvl4[Lvl5 | None]], Lvl2].  This
structure is represented via nesting binary_operator and
subscript nodes.  This function iterates over all levels and
highlight identifier nodes. If TYPE-REGEX is not nil fontify type
identifier only if it matches against TYPE-REGEX. NODE is the
binary_operator node.  OVERRIDE is the override flag described in
`treesit-font-lock-rules'.  START and END mark the region to be
fontified."
  (dolist (child (treesit-node-children node t))
    (let (font-node)
      (pcase (treesit-node-type child)
        ((or "identifier" "none")
         (setq font-node child))
        ("attribute"
         (when-let ((type-node (treesit-node-child-by-field-name child "attribute")))
           (setq font-node type-node)))
        ((or "binary_operator" "subscript")
         (python--treesit-fontify-union-types child override start end type-regex)))

      (when (and font-node
                 (or (null type-regex)
                     (let ((case-fold-search nil))
                       (string-match-p type-regex (treesit-node-text font-node)))))
        (treesit-fontify-with-override
         (treesit-node-start font-node) (treesit-node-end font-node)
         'font-lock-type-face override start end)))))