Subject: Re: bug#60856: 29.0.60; C-M-f in python-ts-mode does not work properly with docstrings

From: Theodor Thornhill <[email-masked]>
Date: Sun, 22 Jan 2023 11:30:17 +0100

Apostolis Bessas <[email-masked]> writes:

> In python-ts-mode executing C-M-f (forward-sexp) when at the beginning
> of a docstring does not go to the end of the docstring (as happens in
> python-mode), but at the end of the file.
>
> However, if I switch to python-mode and then back to python-ts-mode,
> C-M-f works correctly. See the following example:
>
> - Start emacs -Q
> - Execute:
>
> (add-to-list 'auto-mode-alist '("\\.py\\'" . python-ts-mode))
>
> - Create the file test.py with the contents:
>
> """Some docstring.
>
> The docstring has multiple lines.
> """
>
> x = 1
>

Can you test this patch?  More nodes are needed, but this is a start, I
think.  Must be applied to the master branch :)

Theo

>From bec1839da51e1fe6c399e636d1cf476fe76682c9 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <[email-masked]>
Date: Sun, 22 Jan 2023 11:27:37 +0100
Subject: [PATCH] Add sentence and sexp movement to python-ts-mode

* lisp/progmodes/python.el (python-ts-mode): Add relevant node types.
---
 lisp/progmodes/python.el | 14 ++++++++++++++
 1 file changed, 14 insertions(+)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 21d16db287..2a8fb5fcf5 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -6710,6 +6710,20 @@ python-ts-mode
                                               "_definition"))
     (setq-local treesit-defun-name-function
                 #'python--treesit-defun-name)
+
+    (setq-local treesit-sentence-type-regexp
+                (regexp-opt '("statement"
+                              "clause")))
+
+    (setq-local treesit-sexp-type-regexp
+                (regexp-opt '("expression"
+                              "string"
+                              "call"
+                              "operator"
+                              "identifier"
+                              "integer"
+                              "float")))
+
     (treesit-major-mode-setup)
 
     (when python-indent-guess-indent-offset
-- 
2.34.1