From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: kobarity Newsgroups: gmane.emacs.bugs Subject: bug#62696: python.el: Extra indentation for conditionals Date: Tue, 18 Apr 2023 23:23:35 +0900 Message-ID: References: Mime-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: multipart/mixed; boundary="Multipart_Tue_Apr_18_23:23:34_2023-1" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22222"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Wanderlust/2.15.9 (Almost Unreal) SEMI-EPG/1.14.7 (Harue) FLIM-LB/1.14.9 (=?UTF-8?Q?Goj=C5=8D?=) APEL-LB/10.8 EasyPG/1.0.0 Emacs/30.0.50 (x86_64-pc-linux-gnu) MULE/6.0 (HANACHIRUSATO) Cc: 62696@debbugs.gnu.org, Dmitry Gutov , Ross Donaldson , Noam Postavsky To: Gustaf Waldemarson Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Apr 18 16:24:21 2023 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 1pomFp-0005ZO-5t for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 18 Apr 2023 16:24:21 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pomFY-0001TA-KC; Tue, 18 Apr 2023 10:24:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pomFW-0001So-Gh for bug-gnu-emacs@gnu.org; Tue, 18 Apr 2023 10:24:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pomFW-0003Wn-3v for bug-gnu-emacs@gnu.org; Tue, 18 Apr 2023 10:24:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pomFV-0005ej-NY for bug-gnu-emacs@gnu.org; Tue, 18 Apr 2023 10:24:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: kobarity Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 18 Apr 2023 14:24:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 62696 X-GNU-PR-Package: emacs Original-Received: via spool by 62696-submit@debbugs.gnu.org id=B62696.168182782821722 (code B ref 62696); Tue, 18 Apr 2023 14:24:01 +0000 Original-Received: (at 62696) by debbugs.gnu.org; 18 Apr 2023 14:23:48 +0000 Original-Received: from localhost ([127.0.0.1]:60136 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pomFI-0005eI-Gd for submit@debbugs.gnu.org; Tue, 18 Apr 2023 10:23:48 -0400 Original-Received: from mail-pl1-f182.google.com ([209.85.214.182]:46241) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pomFF-0005e4-JL for 62696@debbugs.gnu.org; Tue, 18 Apr 2023 10:23:47 -0400 Original-Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-1a66911f5faso16039505ad.0 for <62696@debbugs.gnu.org>; Tue, 18 Apr 2023 07:23:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1681827819; x=1684419819; h=mime-version:user-agent:references:in-reply-to:subject:cc:to:from :message-id:date:from:to:cc:subject:date:message-id:reply-to; bh=/p1Zxhv5r2UiqpCwHMQHASXxNkAcvBCbWSWvoo+glBQ=; b=MjsTjc6wfnJ9noMOePts07KtNF6i27X0pH5QjKau2mp1CRSHU7mTsvA6YFqRwi9u0b SZiFEQatp3X1sHTwQb+ayocg1opNMTiSu9GDjV81KbREVxxJQ6zZJk9MqH636u1L68gv YCGyV9m52HZ+DyxNAaCyWw/a1h++AjOSUNJtexL5Gpn6rfw6SMCTNyJz0QCkhUCqOHAR NfDTJUsVZyW1Nf1LHNKKYrH/E0hp4aFq6QgZqhuYZAHAjivG/1U7tV96v04+S7kiyAH7 BVkN9VkUnHG35EEMt8dAbef3lmDFmeENc+BhJcTTiGP4f2lq9FzwEvSGq7/H1rDyuV4k onTw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1681827819; x=1684419819; h=mime-version:user-agent:references:in-reply-to:subject:cc:to:from :message-id:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=/p1Zxhv5r2UiqpCwHMQHASXxNkAcvBCbWSWvoo+glBQ=; b=WxoyzBralm98u8uJW7RCKICZaqAQZ7TSealgAuymFIGnpO7KBXw0OM6OxILH3TDMTh iNaxdAQXyotC7UTucsettarXseS2SOLGPFzJhLubrKN/pjVDoXDZGI+A1iTPassSnl3A kcAdDPw/ISHtS0dZ+TgqzostuUaAluMhRJzqXsmCag1O/Uwb+duBZLPBAtcIRlqGiErS w0nBtvQst6dvXlPdto7t8v1lCoWWKNngLBZYujCoauiJChl7PGPiADThQDAa6LH392WH cFwxEQ3j4xhbjJrZjfrSgqyldtMVKOf3DdlKM6FfVHiYJMxvcrab0eAWgtElmhRckByP vAiQ== X-Gm-Message-State: AAQBX9cxiGT9GbfPHvnCtkOymSKNpQPjujYYOk77shu9WDafzydq6cdA qqqhtun8HNu+tDnun4PYniw= X-Google-Smtp-Source: AKy350bynUj/7heUoU4n8dJuU4wt6UPloBi/ZCIvoPG59AFSdhqSM1+9ATaD3jKOW21aOSwNJYJYVQ== X-Received: by 2002:a17:902:7285:b0:1a6:566b:dd73 with SMTP id d5-20020a170902728500b001a6566bdd73mr2017866pll.60.1681827819332; Tue, 18 Apr 2023 07:23:39 -0700 (PDT) Original-Received: from localhost (58x12x133x161.ap58.ftth.ucom.ne.jp. [58.12.133.161]) by smtp.gmail.com with ESMTPSA id 5-20020a170902e9c500b0019aa6bf4450sm9645605plk.188.2023.04.18.07.23.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Apr 2023 07:23:38 -0700 (PDT) In-Reply-To: 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-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:260225 Archived-At: --Multipart_Tue_Apr_18_23:23:34_2023-1 Content-Type: text/plain; charset=US-ASCII Gustaf Waldemarson wrote: > I think that's a really good idea actually. It might also be a good idea to add a > negative example (i.e., the black-indentation style you mentioned earlier), or > a reference to one (maybe to the tests?) I think the best way is to update the docstring of `python-indent-def-block-scale'. So, I am also CC'ing this mail to the members of Bug#28475, where `python-indent-def-block-scale' was introduced. I made a separate patch to update the docstring of `python-indent-def-block-scale'. I think the name `python-indent-def-block-scale' itself is a bit misleading, but I have left it as is for compatibility. The following is my proposal of the docstring. What do you think? "Multiplier applied to indentation inside multi-line blocks. The indentation in parens in the block header will be the current indentation plus `python-indent-offset' multiplied by this variable. For example, the arguments are indented as follows if this variable is 1: def do_something( arg1, arg2 ): print('hello') if this variable is 2 (default): def do_something( arg1, arg2): print('hello') This variable has an effect on all blocks, not just def block. This variable only works if the opening paren is not followed by non-whitespace characters on the same line. Modify `python-indent-block-paren-deeper' to customize the case where non-whitespace characters follow the opening paren on the same line." --Multipart_Tue_Apr_18_23:23:34_2023-1 Content-Type: application/octet-stream; type=patch; name="0001-Add-a-new-user-option-in-Python-mode-to-improve-the-.patch" Content-Disposition: attachment; filename="0001-Add-a-new-user-option-in-Python-mode-to-improve-the-.patch" Content-Transfer-Encoding: 7bit >From ee243dc685cb0cddeb66c53018633682791dbd0a Mon Sep 17 00:00:00 2001 From: kobarity Date: Sun, 16 Apr 2023 22:18:39 +0900 Subject: [PATCH 1/2] Add a new user option in Python mode to improve the indentation * lisp/progmodes/python.el (python-indent-block-paren-deeper): New user option. (python-indent-context): Add a new context :inside-paren-from-block. (python-indent--calculate-indentation): Modify according to `python-indent-block-paren-deeper' and :inside-paren-from-block. * test/lisp/progmodes/python-tests.el (python-indent-inside-paren-block-1) (python-indent-inside-paren-block-2) (python-indent-inside-paren-block-3) (python-indent-inside-paren-block-4): New tests. (python-indent-inside-paren-5, python-indent-dedenters-8): Modify according to the new context. * etc/NEWS: Document the new user option. (Bug#62696) --- etc/NEWS | 17 ++++ lisp/progmodes/python.el | 57 ++++++++++++-- test/lisp/progmodes/python-tests.el | 116 +++++++++++++++++++++++++++- 3 files changed, 181 insertions(+), 9 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index b121002b246..9e318812714 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -276,6 +276,23 @@ distracting and easily confused with actual code, or a significant early aid that relieves you from moving the buffer or reaching for the mouse to consult an error message. +** Python mode + +*** New user option 'python-indent-block-paren-deeper'. +If non-nil, increase the indentation of the lines inside parens in a +header of a block when they are indented to the same level as the body +of the block: + + if (some_expression + and another_expression): + do_something() + +instead of: + + if (some_expression + and another_expression): + do_something() + * New Modes and Packages in Emacs 30.1 diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index bbabce80b4d..9ae95ecfc1c 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1270,6 +1270,31 @@ python-indent-def-block-scale :type 'integer :safe 'natnump) +(defcustom python-indent-block-paren-deeper nil + "Increase indentation inside parens of a block. +If non-nil, increase the indentation of the lines inside parens +in a header of a block when they are indented to the same level +as the body of the block: + + if (some_expression + and another_expression): + do_something() + +instead of: + + if (some_expression + and another_expression): + do_something() + +This variable only works if the opening paren is followed by +non-whitespace characters on the same line. Modify +`python-indent-def-block-scale' to customize the case where +non-whitespace character does not follow the opening paren on the +same line." + :version "30.1" + :type 'boolean + :safe 'booleanp) + (defvar python-indent-current-level 0 "Deprecated var available for compatibility.") @@ -1367,6 +1392,10 @@ python-indent-context - Point is inside a paren with items starting in their own line from a block start. - START is the position of the open paren. +:inside-paren-from-block + - Point is inside a paren from a block start followed by some + items on the same line. + - START is the first non space char position *after* the open paren. :after-backslash - Fallback case when point is after backslash. @@ -1450,12 +1479,16 @@ python-indent-context (starts-in-newline (cons :inside-paren-newline-start start)) ;; General case. - (t (cons :inside-paren - (save-excursion - (goto-char (1+ start)) - (skip-syntax-forward "(" 1) - (skip-syntax-forward " ") - (point)))))))) + (t (let ((after-start (save-excursion + (goto-char (1+ start)) + (skip-syntax-forward "(" 1) + (skip-syntax-forward " ") + (point)))) + (if (save-excursion + (python-nav-beginning-of-statement) + (python-info-looking-at-beginning-of-block)) + (cons :inside-paren-from-block after-start) + (cons :inside-paren after-start)))))))) ;; After backslash. ((let ((start (when (not (python-syntax-comment-or-string-p ppss)) (python-info-line-ends-backslash-p @@ -1603,7 +1636,17 @@ python-indent--calculate-indentation (`(,(or :inside-paren-newline-start-from-block) . ,start) (goto-char start) (+ (current-indentation) - (* python-indent-offset python-indent-def-block-scale)))))) + (* python-indent-offset python-indent-def-block-scale))) + (`(,:inside-paren-from-block . ,start) + (goto-char start) + (let ((column (current-column))) + (if (and python-indent-block-paren-deeper + (= column (+ (save-excursion + (python-nav-beginning-of-statement) + (current-indentation)) + python-indent-offset))) + (+ column python-indent-offset) + column)))))) (defun python-indent--calculate-levels (indentation) "Calculate levels list given INDENTATION. diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index 50153e66da5..60b11d572cf 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el @@ -1139,7 +1139,7 @@ python-indent-inside-paren-5 (should (eq (car (python-indent-context)) :no-indent)) (should (= (python-indent-calculate-indentation) 0)) (forward-line 1) - (should (eq (car (python-indent-context)) :inside-paren)) + (should (eq (car (python-indent-context)) :inside-paren-from-block)) (should (= (python-indent-calculate-indentation) 7)) (forward-line 1) (should (eq (car (python-indent-context)) :after-block-start)) @@ -1174,6 +1174,118 @@ python-indent-inside-paren-7 ;; This signals an error if the test fails (should (eq (car (python-indent-context)) :inside-paren-newline-start)))) +(ert-deftest python-indent-inside-paren-block-1 () + "`python-indent-block-paren-deeper' set to nil (default). +See Bug#62696." + (python-tests-with-temp-buffer + " +if ('VALUE' in my_unnecessarily_long_dictionary and + some_other_long_condition_case): + do_something() +elif (some_case or + another_case): + do_another() +" + (python-tests-look-at "if") + (should (eq (car (python-indent-context)) :no-indent)) + (should (= (python-indent-calculate-indentation) 0)) + (forward-line 1) + (should (eq (car (python-indent-context)) :inside-paren-from-block)) + (should (= (python-indent-calculate-indentation) 4)) + (forward-line 1) + (should (eq (car (python-indent-context)) :after-block-start)) + (should (= (python-indent-calculate-indentation) 4)) + (forward-line 1) + (should (eq (car (python-indent-context)) :at-dedenter-block-start)) + (should (= (python-indent-calculate-indentation) 0)) + (forward-line 1) + (should (eq (car (python-indent-context)) :inside-paren-from-block)) + (should (= (python-indent-calculate-indentation) 6)) + (forward-line 1) + (should (eq (car (python-indent-context)) :after-block-start)) + (should (= (python-indent-calculate-indentation) 4)))) + +(ert-deftest python-indent-inside-paren-block-2 () + "`python-indent-block-paren-deeper' set to t. +See Bug#62696." + (python-tests-with-temp-buffer + " +if ('VALUE' in my_unnecessarily_long_dictionary and + some_other_long_condition_case): + do_something() +elif (some_case or + another_case): + do_another() +" + (let ((python-indent-block-paren-deeper t)) + (python-tests-look-at "if") + (should (eq (car (python-indent-context)) :no-indent)) + (should (= (python-indent-calculate-indentation) 0)) + (forward-line 1) + (should (eq (car (python-indent-context)) :inside-paren-from-block)) + (should (= (python-indent-calculate-indentation) 8)) + (forward-line 1) + (should (eq (car (python-indent-context)) :after-block-start)) + (should (= (python-indent-calculate-indentation) 4)) + (forward-line 1) + (should (eq (car (python-indent-context)) :at-dedenter-block-start)) + (should (= (python-indent-calculate-indentation) 0)) + (forward-line 1) + (should (eq (car (python-indent-context)) :inside-paren-from-block)) + (should (= (python-indent-calculate-indentation) 6)) + (forward-line 1) + (should (eq (car (python-indent-context)) :after-block-start)) + (should (= (python-indent-calculate-indentation) 4))))) + +(ert-deftest python-indent-inside-paren-block-3 () + "With backslash. `python-indent-block-paren-deeper' set to nil (default). +See Bug#62696." + (python-tests-with-temp-buffer + " +if 'VALUE' in my_uncessarily_long_dictionary and\\ + (some_other_long_condition_case or + another_case): + do_something() +" + (python-tests-look-at "if") + (should (eq (car (python-indent-context)) :no-indent)) + (should (= (python-indent-calculate-indentation) 0)) + (forward-line 1) + (should (eq (car (python-indent-context)) + :after-backslash-block-continuation)) + (should (= (python-indent-calculate-indentation) 3)) + (forward-line 1) + (should (eq (car (python-indent-context)) :inside-paren-from-block)) + (should (= (python-indent-calculate-indentation) 4)) + (forward-line 1) + (should (eq (car (python-indent-context)) :after-block-start)) + (should (= (python-indent-calculate-indentation) 4)))) + +(ert-deftest python-indent-inside-paren-block-4 () + "With backslash. `python-indent-block-paren-deeper' set to t. +See Bug#62696." + (python-tests-with-temp-buffer + " +if 'VALUE' in my_uncessarily_long_dictionary and\\ + (some_other_long_condition_case or + another_case): + do_something() +" + (let ((python-indent-block-paren-deeper t)) + (python-tests-look-at "if") + (should (eq (car (python-indent-context)) :no-indent)) + (should (= (python-indent-calculate-indentation) 0)) + (forward-line 1) + (should (eq (car (python-indent-context)) + :after-backslash-block-continuation)) + (should (= (python-indent-calculate-indentation) 3)) + (forward-line 1) + (should (eq (car (python-indent-context)) :inside-paren-from-block)) + (should (= (python-indent-calculate-indentation) 8)) + (forward-line 1) + (should (eq (car (python-indent-context)) :after-block-start)) + (should (= (python-indent-calculate-indentation) 4))))) + (ert-deftest python-indent-after-block-1 () "The most simple after-block case that shouldn't fail." (python-tests-with-temp-buffer @@ -1670,7 +1782,7 @@ python-indent-dedenters-8 (should (= (python-indent-calculate-indentation) 0)) (should (= (python-indent-calculate-indentation t) 0)) (python-tests-look-at "a == 4):\n") - (should (eq (car (python-indent-context)) :inside-paren)) + (should (eq (car (python-indent-context)) :inside-paren-from-block)) (should (= (python-indent-calculate-indentation) 6)) (python-indent-line) (should (= (python-indent-calculate-indentation t) 4)) -- 2.34.1 --Multipart_Tue_Apr_18_23:23:34_2023-1 Content-Type: application/octet-stream; type=patch; name="0002-Improve-docstring-of-python-indent-def-block-scale.patch" Content-Disposition: attachment; filename="0002-Improve-docstring-of-python-indent-def-block-scale.patch" Content-Transfer-Encoding: 7bit >From 3b3a0dd9394b7905ad47401e4ce2166d5cb79c10 Mon Sep 17 00:00:00 2001 From: kobarity Date: Tue, 18 Apr 2023 22:52:18 +0900 Subject: [PATCH 2/2] Improve docstring of python-indent-def-block-scale * lisp/progmodes/python.el (python-indent-def-block-scale): Improve docstring. (Bug#62696) --- lisp/progmodes/python.el | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 9ae95ecfc1c..3bec400006e 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1265,7 +1265,31 @@ python-indent-trigger-commands :type '(repeat symbol)) (defcustom python-indent-def-block-scale 2 - "Multiplier applied to indentation inside multi-line def blocks." + "Multiplier applied to indentation inside multi-line blocks. +The indentation in parens in the block header will be the current +indentation plus `python-indent-offset' multiplied by this +variable. For example, the arguments are indented as follows if +this variable is 1: + + def do_something( + arg1, + arg2 + ): + print('hello') + +if this variable is 2 (default): + + def do_something( + arg1, + arg2): + print('hello') + +This variable has an effect on all blocks, not just def block. +This variable only works if the opening paren is not followed by +non-whitespace characters on the same line. Modify +`python-indent-block-paren-deeper' to customize the case where +non-whitespace characters follow the opening paren on the same +line." :version "26.1" :type 'integer :safe 'natnump) -- 2.34.1 --Multipart_Tue_Apr_18_23:23:34_2023-1--