From: Lele Gaifax <lele@metapensiero.it>
To: 55572@debbugs.gnu.org
Subject: bug#55572: Properly indent Python PEP634 match/case blocks
Date: Sun, 22 May 2022 11:03:22 +0200 [thread overview]
Message-ID: <87k0aeueg5.fsf@metapensiero.it> (raw)
[-- 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.
next reply other threads:[~2022-05-22 9:03 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-05-22 9:03 Lele Gaifax [this message]
2022-05-22 11:23 ` bug#55572: Properly indent Python PEP634 match/case blocks Lars Ingebrigtsen
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=87k0aeueg5.fsf@metapensiero.it \
--to=lele@metapensiero.it \
--cc=55572@debbugs.gnu.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).