unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: kobarity <kobarity@gmail.com>
To: Dima Kogan <dima@secretsauce.net>
Cc: Lars Ingebrigtsen <larsi@gnus.org>, 56635@debbugs.gnu.org
Subject: bug#56635: 29.0.50; [PATCH] hide-show in python-mode supports ONLY function and class blocks
Date: Sun, 7 Aug 2022 23:37:59 +0900	[thread overview]
Message-ID: <CAMQkrSo26PvzyAr4Mrgx6kQ0yx_-onZXGFkogkd=Nqt+OL5BMQ@mail.gmail.com> (raw)
In-Reply-To: <87wnbt3vnq.fsf@secretsauce.net>

[-- Attachment #1: Type: text/plain, Size: 864 bytes --]

Hi,

Dima Kogan <dima@secretsauce.net> wrote:
> I do agree that the best thing to do is to use functions instead of pure
> regexes here. Can we hook that into hs-special-modes-alist instead of
> doing something specific in python-mode? I'm imagining extending the
> meaning of the fields in hs-special-modes-alist, where we'd do something
> different, depending on if some element is a string or not. Maybe that
> would make it easier to extend the hideshow support for other languages?

If we are to extend hs-special-modes-alist, I think it's better to add
functions to find/check blocks. Maybe this is the third option
mentioned in the first mail.  For that, I added three functions to
hs-special-modes-alist:

- hs-find-block-beginning-func
- hs-find-next-block-func
- hs-looking-at-block-start-p-func

Attached is a prototype patch to achieve this.

Regards,

[-- Attachment #2: 56635-prototype.patch --]
[-- Type: application/octet-stream, Size: 9519 bytes --]

diff --git a/lisp/progmodes/hideshow.el b/lisp/progmodes/hideshow.el
index f574ec84fb..6eef2c3271 100644
--- a/lisp/progmodes/hideshow.el
+++ b/lisp/progmodes/hideshow.el
@@ -433,6 +433,15 @@ hs-adjust-block-beginning
 
 See `hs-c-like-adjust-block-beginning' for an example of using this.")
 
+(defvar-local hs-find-block-beginning-func #'hs-find-block-beginning
+  "Function used to do `hs-find-block-beginning'.")
+
+(defvar-local hs-find-next-block-func #'hs-find-next-block
+  "Function used to do `hs-find-next-block'.")
+
+(defvar-local hs-looking-at-block-start-p-func #'hs-looking-at-block-start-p
+  "Function used to do hs-looking-at-block-start-p.")
+
 (defvar hs-headline nil
   "Text of the line where a hidden block begins, set during isearch.
 You can display this in the mode line by adding the symbol `hs-headline'
@@ -565,7 +574,7 @@ hs-hide-block-at-point
 and then further adjusted to be at the end of the line."
   (if comment-reg
       (hs-hide-comment-region (car comment-reg) (cadr comment-reg) end)
-    (when (hs-looking-at-block-start-p)
+    (when (funcall hs-looking-at-block-start-p-func)
       (let ((mdata (match-data t))
             (header-end (match-end 0))
             p q ov)
@@ -672,7 +681,14 @@ hs-grok-mode-type
                                                      0 (1- (match-end 0)))
                                         c-start-regexp)))
               hs-forward-sexp-func (or (nth 4 lookup) #'forward-sexp)
-              hs-adjust-block-beginning (or (nth 5 lookup) #'identity)))
+              hs-adjust-block-beginning (or (nth 5 lookup) #'identity)
+              hs-find-block-beginning-func (or (nth 6 lookup)
+                                               #'hs-find-block-beginning)
+              hs-find-next-block-func (or (nth 7 lookup)
+                                          #'hs-find-next-block)
+              hs-looking-at-block-start-p-func
+              (or (nth 8 lookup)
+                  #'hs-looking-at-block-start-p)))
     (setq hs-minor-mode nil)
     (error "%s Mode doesn't support Hideshow Minor Mode"
            (format-mode-line mode-name))))
@@ -683,7 +699,7 @@ hs-find-block-beginning
   (let ((done nil)
         (here (point)))
     ;; look if current line is block start
-    (if (hs-looking-at-block-start-p)
+    (if (funcall hs-looking-at-block-start-p-func)
         (point)
       ;; look backward for the start of a block that contains the cursor
       (while (and (re-search-backward hs-block-start-regexp nil t)
@@ -698,19 +714,25 @@ hs-find-block-beginning
         (goto-char here)
         nil))))
 
+(defun hs-find-next-block (regexp maxp skip-comments)
+  "Reposition point at next block-start.
+Skip comments if SKIP-COMMENTS is not nil, and search for REGEXP
+in region (point MAXP)."
+  (when skip-comments
+    (forward-comment (point-max)))
+  (and (< (point) maxp)
+       (re-search-forward regexp maxp t)))
+
 (defun hs-hide-level-recursive (arg minp maxp)
   "Recursively hide blocks ARG levels below point in region (MINP MAXP)."
-  (when (hs-find-block-beginning)
+  (when (funcall hs-find-block-beginning-func)
     (setq minp (1+ (point)))
     (funcall hs-forward-sexp-func 1)
     (setq maxp (1- (point))))
   (unless hs-allow-nesting
     (hs-discard-overlays minp maxp))
   (goto-char minp)
-  (while (progn
-           (forward-comment (buffer-size))
-           (and (< (point) maxp)
-                (re-search-forward hs-block-start-regexp maxp t)))
+  (while (funcall hs-find-next-block-func hs-block-start-regexp maxp t)
     (when (save-match-data
 	    (not (nth 8 (syntax-ppss)))) ; not inside comments or strings
       (if (> arg 1)
@@ -747,8 +769,8 @@ hs-already-hidden-p
           (goto-char (nth 0 c-reg))
         (end-of-line)
         (when (and (not c-reg)
-                   (hs-find-block-beginning)
-		   (hs-looking-at-block-start-p))
+                   (funcall hs-find-block-beginning-func)
+		   (funcall hs-looking-at-block-start-p-func))
           ;; point is inside a block
           (goto-char (match-end 0)))))
     (end-of-line)
@@ -790,10 +812,8 @@ hs-hide-all
                                    hs-c-start-regexp
                                    "\\)")
                          ""))))
-       (while (progn
-                (unless hs-hide-comments-when-hiding-all
-                  (forward-comment (point-max)))
-                (re-search-forward re (point-max) t))
+       (while (funcall hs-find-next-block-func re (point-max)
+                       (not hs-hide-comments-when-hiding-all))
          (if (match-beginning 1)
              ;; We have found a block beginning.
              (progn
@@ -838,8 +858,8 @@ hs-hide-block
                       (<= (count-lines (car c-reg) (nth 1 c-reg)) 1)))
        (message "(not enough comment lines to hide)"))
       ((or c-reg
-	   (hs-looking-at-block-start-p)
-           (hs-find-block-beginning))
+	   (funcall hs-looking-at-block-start-p-func)
+           (funcall hs-find-block-beginning-func))
        (hs-hide-block-at-point end c-reg)
        (run-hooks 'hs-hide-hook))))))
 
@@ -868,9 +888,9 @@ hs-show-block
              (when (car c-reg)
                (setq p (car c-reg)
                      q (cadr c-reg))))
-            ((and (hs-find-block-beginning)
+            ((and (funcall hs-find-block-beginning-func)
                   ;; ugh, fresh match-data
-                  (hs-looking-at-block-start-p))
+                  (funcall hs-looking-at-block-start-p-func))
              (setq p (point)
                    q (progn (hs-forward-sexp (match-data t) 1) (point)))))
       (when (and p q)
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 88b19c88cd..3bcadf1254 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1496,6 +1496,10 @@ python-nav-beginning-of-defun-regexp
 The name of the defun should be grouped so it can be retrieved
 via `match-string'.")
 
+(defvar python-nav-beginning-of-block-regexp
+  (python-rx line-start (* space) block-start)
+  "Regexp matching block start.")
+
 (defun python-nav--beginning-of-defun (&optional arg)
   "Internal implementation of `python-nav-beginning-of-defun'.
 With positive ARG search backwards, else search forwards."
@@ -4877,9 +4881,29 @@ 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)
-  (unless (python-info-current-line-empty-p)
-    (backward-char)))
+  (python-nav-end-of-block))
+
+(defun python-hideshow-find-block-beginning ()
+  "Python specific `hs-find-block-beginning' furnction for `hs-minor-mode'."
+  (let ((starting-pos (point))
+        (block-beginning))
+    (save-excursion
+      (when (and (python-nav-beginning-of-block)
+                 (<= starting-pos (save-excursion (python-nav-end-of-block))))
+        (setq block-beginning (point))))
+    (when block-beginning
+      (goto-char block-beginning))))
+
+(defun python-hideshow-find-next-block (regexp maxp _skip-comments)
+  "Python specific `hs-find-next-block' furnction for `hs-minor-mode'.
+Call `python-nav-forward-block' to find next block and check if point <= MAXP.
+REGEXP is passed to `looking-at' to set `match-data'."
+  (let ((next-block (save-excursion (python-nav-forward-block))))
+    (when (and next-block (<= next-block maxp))
+      (goto-char next-block)
+      (save-excursion
+        (beginning-of-line)
+        (looking-at regexp)))))
 
 \f
 ;;; Imenu
@@ -5376,6 +5400,18 @@ python-info-looking-at-beginning-of-defun
          (beginning-of-line 1)
          (looking-at python-nav-beginning-of-defun-regexp))))
 
+(defun python-info-looking-at-beginning-of-block ()
+  "Check if point is at beginning of block."
+  (let* ((line-beg-pos (line-beginning-position))
+         (line-content-start (+ line-beg-pos (current-indentation))))
+    (and (= (save-excursion
+              (python-nav-beginning-of-block))
+            line-content-start)
+         (<= (point) line-content-start)
+         (save-excursion
+           (beginning-of-line)
+           (looking-at python-nav-beginning-of-block-regexp)))))
+
 (defun python-info-current-line-comment-p ()
   "Return non-nil if current line is a comment line."
   (char-equal
@@ -5825,14 +5861,17 @@ python-mode
 
   (add-to-list
    'hs-special-modes-alist
-   '(python-mode
-     "\\s-*\\_<\\(?:def\\|class\\)\\_>"
+   `(python-mode
+     ,python-nav-beginning-of-block-regexp
      ;; 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))
+     nil
+     python-hideshow-find-block-beginning
+     python-hideshow-find-next-block
+     python-info-looking-at-beginning-of-block))
 
   (setq-local outline-regexp (python-rx (* space) block-start))
   (setq-local outline-heading-end-regexp ":[^\n]*\n")
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index eb57122690..e5348fdde4 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -5956,8 +5956,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 ()

  reply	other threads:[~2022-08-07 14:37 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAMQkrSo26PvzyAr4Mrgx6kQ0yx_-onZXGFkogkd=Nqt+OL5BMQ@mail.gmail.com' \
    --to=kobarity@gmail.com \
    --cc=56635@debbugs.gnu.org \
    --cc=dima@secretsauce.net \
    --cc=larsi@gnus.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).