unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#55572: Properly indent Python PEP634 match/case blocks
@ 2022-05-22  9:03 Lele Gaifax
  2022-05-22 11:23 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 2+ messages in thread
From: Lele Gaifax @ 2022-05-22  9:03 UTC (permalink / raw)
  To: 55572

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

Hi,

PEP634, introduced in Python 3.10, added two new keywords, "match"
and "case". Commit 139042eb8629e6fd49b2c3002a8fc4d1aabd174d added them to the
font-lock machinery, but missed the opportunity to configure them as
block-starter as well.

The attached patch does that, adding also two simple indentation tests.

All the best,
ciao, lele.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Properly-indent-Python-PEP634-match-case-blocks.patch --]
[-- Type: text/x-diff, Size: 2802 bytes --]

From 49730d01dbf3a72c29d5c881e5b5ae1fc55c71ec Mon Sep 17 00:00:00 2001
From: Lele Gaifax <lele@metapensiero.it>
Date: Sun, 22 May 2022 10:44:31 +0200
Subject: [PATCH] Properly indent Python PEP634 match/case blocks

Python 3.10 introduced the "structural pattern matching" syntax, and
commit 139042eb8629e6fd49b2c3002a8fc4d1aabd174d told font-lock about the
new keywords. This adds them also as block-start statements, to enable
proper indentation of such blocks.

* lisp/progmodes/python.el (python-rx): Add "match" and "case" as
block-start keywords.
* test/lisp/progmodes/python-tests.el (python-indent-after-match-block,
python-indent-after-case-block): New tests to verify indentation of
"match" and "case" blocks.
---
 lisp/progmodes/python.el            |  2 ++
 test/lisp/progmodes/python-tests.el | 25 +++++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 0761aaebdc..94297d4ea5 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -362,6 +362,8 @@ python-rx
   `(rx-let ((block-start       (seq symbol-start
                                     (or "def" "class" "if" "elif" "else" "try"
                                         "except" "finally" "for" "while" "with"
+                                        ;; Python 3.10+ PEP634
+                                        "match" "case"
                                         ;; Python 3.5+ PEP492
                                         (and "async" (+ space)
                                              (or "def" "for" "with")))
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el
index ee7b66610a..a3f778bbbe 100644
--- a/test/lisp/progmodes/python-tests.el
+++ b/test/lisp/progmodes/python-tests.el
@@ -1516,6 +1516,31 @@ python-virt-bin
      (should (string= (buffer-substring-no-properties (point-min) (point-max))
                       expected)))))
 
+(ert-deftest python-indent-after-match-block ()
+  "Test PEP634 match."
+  (python-tests-with-temp-buffer
+   "
+match foo:
+"
+   (should (eq (car (python-indent-context)) :no-indent))
+   (should (= (python-indent-calculate-indentation) 0))
+   (goto-char (point-max))
+   (should (eq (car (python-indent-context)) :after-block-start))
+   (should (= (python-indent-calculate-indentation) 4))))
+
+(ert-deftest python-indent-after-case-block ()
+  "Test PEP634 case."
+  (python-tests-with-temp-buffer
+   "
+match foo:
+    case 1:
+"
+   (should (eq (car (python-indent-context)) :no-indent))
+   (should (= (python-indent-calculate-indentation) 0))
+   (goto-char (point-max))
+   (should (eq (car (python-indent-context)) :after-block-start))
+   (should (= (python-indent-calculate-indentation) 8))))
+
 \f
 ;;; Filling
 
-- 
2.36.1


[-- Attachment #3: Type: text/plain, Size: 211 bytes --]


-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
lele@metapensiero.it  |                 -- Fortunato Depero, 1929.

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

* bug#55572: Properly indent Python PEP634 match/case blocks
  2022-05-22  9:03 bug#55572: Properly indent Python PEP634 match/case blocks Lele Gaifax
@ 2022-05-22 11:23 ` Lars Ingebrigtsen
  0 siblings, 0 replies; 2+ messages in thread
From: Lars Ingebrigtsen @ 2022-05-22 11:23 UTC (permalink / raw)
  To: Lele Gaifax; +Cc: 55572

Lele Gaifax <lele@metapensiero.it> writes:

> PEP634, introduced in Python 3.10, added two new keywords, "match"
> and "case". Commit 139042eb8629e6fd49b2c3002a8fc4d1aabd174d added them to the
> font-lock machinery, but missed the opportunity to configure them as
> block-starter as well.
>
> The attached patch does that, adding also two simple indentation tests.

Thanks; pushed to Emacs 29.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





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

end of thread, other threads:[~2022-05-22 11:23 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-22  9:03 bug#55572: Properly indent Python PEP634 match/case blocks Lele Gaifax
2022-05-22 11:23 ` Lars Ingebrigtsen

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).