Subject: bug#56635: 29.0.50; [PATCH] hide-show in python-mode supports ONLY function and class blocks

From: kobarity <[email-masked]>
Date: Sun, 24 Jul 2022 09:57:13 +0900

Hi Dima,

Dima Kogan <[email-masked]> wrote:
> So (python-hideshow-forward-sexp-function) should call
> (python-nav-end-of-block) and do nothing else. I think the extra
> (backward-char) stuff was intended to handle the closing brace of the
> block, which doesn't exist in Python. If you leave the (backward-char)
> stuff there, the collapsed blocks still display the last character in
> the block, which in Python has no special syntactical meaning.

This is one point I was thinking of.  The first patch failed to pass
ERTs python-hideshow-hide-levels-1 and python-hideshow-hide-levels-2.
The second patch still fails with python-hideshow-hide-levels-1, but I
think it's OK to fix the ERT:

diff --git a/test/lisp/progmodes/python-tests.el
b/test/lisp/progmodes/python-tests.el
index 3b10bde23b..8cb2aa84d0 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -5828,8 +5828,11 @@ python-hideshow-hide-levels-1
 class SomeClass:

     def __init__(self, arg, kwarg=1):
+
     def filter(self, nums):
-    def __str__(self):"))))
+
+    def __str__(self):
+"))))
       (or enabled (hs-minor-mode -1)))))

 (ert-deftest python-hideshow-hide-levels-2 ()

It would be better to add some ERTs for blocks.

Another point I am thinking of is to improve the regex.  As
hs-special-modes-alist has a support of the form (COMPLEX-START
MDATA-SELECTOR), I tried with the code below:

  (add-to-list
   'hs-special-modes-alist
   `(python-mode
     (,(python-rx (seq (or (seq buffer-start (? ?\n)) (seq (not ?\\) ?\n))
                       (* space) (group block-start))) 1)
     ;; Use the empty string as end regexp so it doesn't default to
     ;; "\\s)".  This way parens at end of defun are properly hidden.
     ""
     "#"
     python-hideshow-forward-sexp-function
     nil))

This code solves many problems of `hs-hide-block' mentioned in the
first mail, but it breaks `hs-hide-level'.  It seems that current
MDATA-SELECTOR support is limited and needs some fixes if we want to
do something like above.  I will look into this a little bit more.

Regards,