unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: kobarity <kobarity@gmail.com>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: Lele Gaifax <lele@metapensiero.it>, 62092@debbugs.gnu.org
Subject: bug#62092: 30.0.50; "case" does not work as dedenter in Python
Date: Sat, 25 Mar 2023 23:13:06 +0900	[thread overview]
Message-ID: <eke74jq8nclp.wl-kobarity@gmail.com> (raw)
In-Reply-To: <bbac1272-08ea-566b-b959-13ca1d56436a@yandex.ru>

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


Dmitry Gutov wrote:
> Pushed your change to emacs-29, and closing. Thanks!

Thank you for applying the patch.  However, I overlooked one problem.

#+begin_src python
match a:
    case 1:
        match b:
            case 2
#+end_src

When I type ":" at the last line after "case 2", the message "Closes
case 1:" is shown.  This is wrong because it cannot be the "case"
block of the outer "match" block (match a:).  I'm sorry if you were
mentioning this case.

Similar message can be shown with "elif":

#+begin_src python
for c in (1, 2):
    if a == 1:
        for d in (3, 4):
            elif b == 1:
#+end_src

However, this is not a correct Python code because "elif" cannot be
the first sentence in a block.

Dedenters other than "case" can never be the first sentence of a
block, whereas "case" can be the first sentence of a block.

So I think it is appropriate that the first "case" in the block should
not be considered a dedenter.  Attached is a patch to add a condition
in `python-info-dedenter-statement-p'.

[-- Attachment #2: 0001-Improve-indenting-case-in-Python.patch --]
[-- Type: application/octet-stream, Size: 3517 bytes --]

From f0dafe85cb78b0510e70fe7b1d093d013aea9008 Mon Sep 17 00:00:00 2001
From: kobarity <kobarity@gmail.com>
Date: Sat, 25 Mar 2023 22:59:05 +0900
Subject: [PATCH] Improve indenting "case" in Python

* lisp/progmodes/python.el (python-info-dedenter-statement-p): Do not
consider the first "case" in the block as dedenter.
* test/lisp/progmodes/python-tests.el
(python-info-dedenter-opening-block-positions-7)
(python-info-dedenter-statement-p-6): New tests. (Bug#62092)
---
 lisp/progmodes/python.el            |  9 ++++++-
 test/lisp/progmodes/python-tests.el | 38 +++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 2fe88323c35..bbabce80b4d 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -5854,7 +5854,14 @@ python-info-dedenter-statement-p
   (save-excursion
     (python-nav-beginning-of-statement)
     (when (and (not (python-syntax-context-type))
-               (looking-at (python-rx dedenter)))
+               (looking-at (python-rx dedenter))
+               ;; Exclude the first "case" in the block.
+               (not (and (string= (match-string-no-properties 0)
+                                  "case")
+                         (save-excursion
+                           (back-to-indentation)
+                           (python-util-forward-comment -1)
+                           (equal (char-before) ?:)))))
       (point))))
 
 (defun python-info-line-ends-backslash-p (&optional line-number)
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index ed4a08da6ab..50153e66da5 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -5940,6 +5940,26 @@ python-info-dedenter-opening-block-positions-6
      (equal (list (python-tests-look-at "if (" -1 t))
             (python-info-dedenter-opening-block-positions)))))
 
+(ert-deftest python-info-dedenter-opening-block-positions-7 ()
+  "Test case blocks."
+  (python-tests-with-temp-buffer
+   "
+match a:
+    case 1:
+        match b:
+            case 2:
+                something()
+            case 3:
+"
+   (python-tests-look-at "case 1:")
+   (should-not (python-info-dedenter-opening-block-positions))
+   (python-tests-look-at "case 2:")
+   (should-not (python-info-dedenter-opening-block-positions))
+   (python-tests-look-at "case 3:")
+   (equal (list (python-tests-look-at "case 2:" -1)
+                (python-tests-look-at "case 1:" -1 t))
+            (python-info-dedenter-opening-block-positions))))
+
 (ert-deftest python-info-dedenter-opening-block-message-1 ()
   "Test dedenters inside strings are ignored."
   (python-tests-with-temp-buffer
@@ -6125,6 +6145,24 @@ python-info-dedenter-statement-p-5
                  (point))
                (python-info-dedenter-statement-p)))))
 
+(ert-deftest python-info-dedenter-statement-p-6 ()
+  "Test case keyword."
+  (python-tests-with-temp-buffer
+      "
+match a:  # Comment
+    case 1:
+        match b:
+            case 2:
+                something()
+            case 3:
+"
+    (python-tests-look-at "case 1:")
+    (should-not (python-info-dedenter-statement-p))
+    (python-tests-look-at "case 2:")
+    (should-not (python-info-dedenter-statement-p))
+    (python-tests-look-at "case 3:")
+    (should (= (point) (python-info-dedenter-statement-p)))))
+
 (ert-deftest python-info-line-ends-backslash-p-1 ()
   (python-tests-with-temp-buffer
    "
-- 
2.34.1


  reply	other threads:[~2023-03-25 14:13 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-10  5:33 bug#62092: 30.0.50; "case" does not work as dedenter in Python kobarity
2023-03-10  7:36 ` Eli Zaretskii
2023-03-10  8:27   ` kobarity
2023-03-11  0:27 ` Dmitry Gutov
2023-03-11  2:08   ` kobarity
2023-03-11 13:18     ` Dmitry Gutov
2023-03-25 14:13       ` kobarity [this message]
2023-03-26  2:04         ` Dmitry Gutov
2023-08-14 15:12 ` Mattias Engdegård
2023-08-15 13:43   ` kobarity
2023-08-15 15:09     ` Mattias Engdegård

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=eke74jq8nclp.wl-kobarity@gmail.com \
    --to=kobarity@gmail.com \
    --cc=62092@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    --cc=lele@metapensiero.it \
    /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).