From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Lele Gaifax Newsgroups: gmane.emacs.bugs Subject: bug#55572: Properly indent Python PEP634 match/case blocks Date: Sun, 22 May 2022 11:03:22 +0200 Message-ID: <87k0aeueg5.fsf@metapensiero.it> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6584"; mail-complaints-to="usenet@ciao.gmane.io" To: 55572@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun May 22 11:04:14 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nshVW-0001RJ-OT for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 22 May 2022 11:04:14 +0200 Original-Received: from localhost ([::1]:56560 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nshVV-0006Wi-JR for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 22 May 2022 05:04:13 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36030) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nshVL-0006WS-1q for bug-gnu-emacs@gnu.org; Sun, 22 May 2022 05:04:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:49739) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nshVK-0006tz-PZ for bug-gnu-emacs@gnu.org; Sun, 22 May 2022 05:04:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nshVK-0003VO-Ic for bug-gnu-emacs@gnu.org; Sun, 22 May 2022 05:04:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Lele Gaifax Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 22 May 2022 09:04:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 55572 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.165321021313432 (code B ref -1); Sun, 22 May 2022 09:04:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 22 May 2022 09:03:33 +0000 Original-Received: from localhost ([127.0.0.1]:43636 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nshUr-0003Ua-Ia for submit@debbugs.gnu.org; Sun, 22 May 2022 05:03:33 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:56556) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nshUp-0003US-NS for submit@debbugs.gnu.org; Sun, 22 May 2022 05:03:32 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:36022) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nshUp-0006Pf-Bz for bug-gnu-emacs@gnu.org; Sun, 22 May 2022 05:03:31 -0400 Original-Received: from orphu.arstecnica.it ([178.63.97.204]:59502) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nshUn-0006ri-0M for bug-gnu-emacs@gnu.org; Sun, 22 May 2022 05:03:30 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=metapensiero.it; s=mail; t=1653210204; bh=led0vUbmfReyAmhe0Vru2RWLCNKL0whGE0DU7gNhlxo=; h=From:To:Subject:Date; b=iWgPAZfHkvbYRXKBwuJQEuwkTKlAxz7bNJMufIcDJh1IA5oW3BgpRXgorr73zPZFU FwY5TdUCDtZwRJWnqPPBBvk8VnBKa5BWry5VQfbGIBO16xqFOxkuSob0uDDhJ8lgIT S9kFGTKCL9lk/yjQBS2K5fWzJJ6UCO18g4467lBI= Received-SPF: pass client-ip=178.63.97.204; envelope-from=lele@metapensiero.it; helo=orphu.arstecnica.it X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:232881 Archived-At: --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Properly-indent-Python-PEP634-match-case-blocks.patch >From 49730d01dbf3a72c29d5c881e5b5ae1fc55c71ec Mon Sep 17 00:00:00 2001 From: Lele Gaifax 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)))) + ;;; Filling -- 2.36.1 --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable --=20 nickname: Lele Gaifax | Quando vivr=C3=B2 di quello che ho pensato ieri real: Emanuele Gaifas | comincer=C3=B2 ad aver paura di chi mi copia. lele@metapensiero.it | -- Fortunato Depero, 1929. --=-=-=--