]> git.eshelyaron.com Git - emacs.git/commitdiff
Improve treesit settings for python-ts-mode (bug#73404).
authorJuri Linkov <juri@linkov.net>
Wed, 25 Jun 2025 16:57:05 +0000 (19:57 +0300)
committerEshel Yaron <me@eshelyaron.com>
Wed, 23 Jul 2025 19:18:55 +0000 (21:18 +0200)
* lisp/progmodes/python.el (python--thing-settings):
For the 'sexp' thing use all nodes except top-level
grouping nodes, comments and parens.  Add the 'list' thing.
(python-ts-mode): Use setq-local for treesit-thing-settings.
Modify 'forward-sexp-function' and 'treesit-sexp-thing'
after calling 'treesit-major-mode-setup'.

(cherry picked from commit f3c755129f7d77bd6e18f5005f3c4034c170ea25)

lisp/progmodes/python.el

index f51bb770c83d1a94bd3aa49b136ac256ba23fa0d..b9bb79ccab4c3567517f0d30a73cffc782414552 100644 (file)
@@ -6071,13 +6071,34 @@ tree-sitter."
 (defvar python--thing-settings
   `((python
      (defun ,(rx (or "function" "class") "_definition"))
-     (sexp ,(rx (or "expression"
-                    "string"
-                    "call"
-                    "operator"
-                    "identifier"
-                    "integer"
-                    "float")))
+     (sexp (not (or (and named
+                         ,(rx bos (or "module"
+                                      "block"
+                                      "comment")
+                              eos))
+                    (and anonymous
+                         ,(rx bos (or "(" ")" "[" "]" "{" "}" ",")
+                              eos)))))
+     (list ,(rx bos (or "parameters"
+                        "type_parameter"
+                        "parenthesized_list_splat"
+                        "argument_list"
+                        "_list_pattern"
+                        "_tuple_pattern"
+                        "dict_pattern"
+                        "tuple_pattern"
+                        "list_pattern"
+                        "list"
+                        "set"
+                        "tuple"
+                        "dictionary"
+                        "list_comprehension"
+                        "dictionary_comprehension"
+                        "set_comprehension"
+                        "generator_expression"
+                        "parenthesized_expression"
+                        "interpolation")
+                eos))
      (sentence ,(rx (or "statement"
                         "clause")))
      (text ,(rx (or "string" "comment")))))
@@ -7285,8 +7306,11 @@ implementations: `python-mode' and `python-ts-mode'."
     (setq-local treesit-defun-name-function
                 #'python--treesit-defun-name)
 
-    (setq treesit-thing-settings python--thing-settings)
+    (setq-local treesit-thing-settings python--thing-settings)
     (treesit-major-mode-setup)
+    ;; Enable the `sexp' navigation by default
+    (setq-local forward-sexp-function #'treesit-forward-sexp
+                treesit-sexp-thing 'sexp)
 
     (setq-local syntax-propertize-function #'python--treesit-syntax-propertize)