Subject: [PATCH] Add font-locking for typed parameters in python-ts-mode

From: Noah Peart <[email-masked]>
Date: Tue, 30 Apr 2024 03:09:07 -0700

Tags: patch

This patch adds a rule to python--treesit-settings to apply
font-lock-variable-name-face to typed parameters in python-ts-mode.
Currently, function parameters with type annotations are missing
font-lock-variable-name-face.

For example, this patch adds font-locking in the following case:

    def foo(val: str) -> str
    #        ^ font-lock-variable-name-face

In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
3.24.33, cairo version 1.16.0) of 2024-04-29 built on noah-X580VD
Repository revision: 3000edc6179dfe0b5f24ae2e472826530809dfd1
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101004
System Description: Ubuntu 22.04.4 LTS

Configured using:
 'configure --prefix=/usr/local --with-modules --with-tree-sitter
--with-threads --with-x-toolkit=gtk3 --with-xwidgets --with-gnutls
--with-json --with-mailutils --with-jpeg --with-png --with-rsvg
--with-tiff --with-xml2 --with-xpm --with-imagemagick CC=gcc-12
CXX=gcc-12'
From 77dd550a17426bf59de880a8808df0b1aa2c0bba Mon Sep 17 00:00:00 2001
From: Noah Peart <[email-masked]>
Date: Tue, 30 Apr 2024 02:55:18 -0700
Subject: [PATCH] Add font-locking for typed parameters in python-ts-mode

* lisp/progmodes/python.el (python--treesit-settings): Add
tree-sitter font-locking rule for typed parameters.
* test/lisp/progmodes/python-tests.el
(python-ts-mode-types-face-1): Test for
font-lock-variable-name-face in typed parameter.
---
 .last-build-hash                    | 1 +
 lisp/progmodes/python.el            | 1 +
 test/lisp/progmodes/python-tests.el | 3 +++
 3 files changed, 5 insertions(+)
 create mode 100644 .last-build-hash

diff --git a/.last-build-hash b/.last-build-hash
new file mode 100644
index 00000000000..f64a905ac96
--- /dev/null
+++ b/.last-build-hash
@@ -0,0 +1 @@
+3000edc6179dfe0b5f24ae2e472826530809dfd1
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index d6c29e5ffc6..4ff5e9d4878 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1202,6 +1202,7 @@ python--treesit-settings
      (class_definition
       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)))
 
    :feature 'builtin
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index e11440cdb5b..878f60f1614 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -7545,6 +7545,9 @@ python-ts-mode-union-types-face-2
 (ert-deftest python-ts-mode-types-face-1 ()
   (python-ts-tests-with-temp-buffer
    "def f(val: Callable[[Type0], (Type1, Type2)]):"
+   (search-forward "val")
+   (goto-char (match-beginning 0))
+   (should (eq (face-at-point) font-lock-variable-name-face))
    (dolist (test '("Callable" "Type0" "Type1" "Type2"))
      (search-forward test)
      (goto-char (match-beginning 0))
-- 
2.34.1