unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#56635: 29.0.50; [PATCH] hide-show in python-mode supports ONLY function and class blocks
@ 2022-07-18 22:20 Dima Kogan
  2022-07-23  7:58 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 21+ messages in thread
From: Dima Kogan @ 2022-07-18 22:20 UTC (permalink / raw)
  To: 56635

Hi. I'm using hide-show mode to visually collapse blocks while coding.
This is very helpful, and works great in C-like modes. hide-show mode
currently supports Python in a limited fashion: only def: and class:
blocks are hidden. Other kinds of blocks (if, else, for, while, ...) are
not supported. A simple patch that mostly adds support for all other
kinds of blocks is:

  diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
  index 1c99937c4b9..b63d2bc0690 100644
  --- a/lisp/progmodes/python.el
  +++ b/lisp/progmodes/python.el
  @@ -4819,7 +4819,7 @@ python-describe-at-point
   (defun python-hideshow-forward-sexp-function (_arg)
     "Python specific `forward-sexp' function for `hs-minor-mode'.
   Argument ARG is ignored."
  -  (python-nav-end-of-defun)
  +  (python-nav-end-of-block)
     (unless (python-info-current-line-empty-p)
       (backward-char)))

  @@ -5766,8 +5766,8 @@ python-mode

     (add-to-list
      'hs-special-modes-alist
  -   '(python-mode
  -     "\\s-*\\_<\\(?:def\\|class\\)\\_>"
  +   `(python-mode
  +     ,(python-rx block-start)
        ;; Use the empty string as end regexp so it doesn't default to
        ;; "\\s)".  This way parens at end of defun are properly hidden.
        ""

I say "mostly" because this isn't doable with regular expressions, as is
required by hide-show mode. So this patch can confuse the parser: some
bits of code will look like they begin a block, while in reality they do
not. hide-show would then collapse lines that it should ignore. Some
Python code that shows this failure:

  if 0:

      aaa
      l = [ i for i in range(5) \
            if i < 3 ]
      ccc
      abc = o.match(1,2,3)
      ddd

Here the "for" and "if" in the list comprehension both trigger the block
hiding. And the "match" function call triggers it too. python-mode has
functions to move between blocks (python-nav-...), but they're not
completely regex-based, so they can't be baked into hide-show.
Suggestions?

Obvious options:

  1. Leave the code as it is, don't support other blocks

  2. Take this patch, with the understanding that it'll collapse some
     stuff that it shouldn't

  3. Patch hide-show to potentially accept non-regex logic

I've been dogfooding the patch for a few days now, and it's behaving 99%
properly for me, so I'm inclined to suggest option 2. Making hide-show
smarter (option 3) doesn't feel worth the effort to fix THIS problem.

Thanks!





^ permalink raw reply	[flat|nested] 21+ messages in thread

end of thread, other threads:[~2022-08-25 13:39 UTC | newest]

Thread overview: 21+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-07-18 22:20 bug#56635: 29.0.50; [PATCH] hide-show in python-mode supports ONLY function and class blocks Dima Kogan
2022-07-23  7:58 ` Lars Ingebrigtsen
2022-07-23 15:51   ` kobarity
2022-07-23 20:41     ` Dima Kogan
2022-07-24  0:57       ` kobarity
2022-07-31 10:18         ` kobarity
2022-07-31 19:04           ` Dima Kogan
2022-08-07 14:37             ` kobarity
2022-08-08 15:16               ` Augusto Stoffel
2022-08-13 12:36                 ` kobarity
2022-08-15 15:00                   ` kobarity
2022-08-15 17:16                     ` Dima Kogan
2022-08-16  2:28                       ` kobarity
2022-08-17 10:45                     ` Lars Ingebrigtsen
2022-08-17 11:11                       ` Lars Ingebrigtsen
2022-08-17 11:32                         ` kobarity
2022-08-24  0:39                           ` Dima Kogan
2022-08-24 10:27                             ` Lars Ingebrigtsen
2022-08-24 14:15                               ` kobarity
2022-08-25 12:31                                 ` Lars Ingebrigtsen
2022-08-25 13:39                                   ` kobarity

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).