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: Sun, 09 Apr 2023 21:11:26 +0900 Message-ID: References: Mime-Version: 1.0 (generated by SEMI-EPG 1.14.7 - "Harue") Content-Type: multipart/mixed; boundary="Multipart_Sun_Apr__9_21:11:26_2023-1" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34593"; 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 To: Gustaf Waldemarson Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Apr 09 14:12:30 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 1plTuI-0008nW-24 for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 09 Apr 2023 14:12:30 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1plTtt-00034t-Cx; Sun, 09 Apr 2023 08:12:05 -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 1plTtr-00034c-HK for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2023 08:12:03 -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 1plTtr-00037P-6H for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2023 08:12:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1plTtq-0006Ln-CT for bug-gnu-emacs@gnu.org; Sun, 09 Apr 2023 08:12:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: kobarity Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 09 Apr 2023 12:12:02 +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.168104231324383 (code B ref 62696); Sun, 09 Apr 2023 12:12:02 +0000 Original-Received: (at 62696) by debbugs.gnu.org; 9 Apr 2023 12:11:53 +0000 Original-Received: from localhost ([127.0.0.1]:60334 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1plTtg-0006LC-KD for submit@debbugs.gnu.org; Sun, 09 Apr 2023 08:11:53 -0400 Original-Received: from mail-pl1-f169.google.com ([209.85.214.169]:39777) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1plTte-0006Kq-5t for 62696@debbugs.gnu.org; Sun, 09 Apr 2023 08:11:50 -0400 Original-Received: by mail-pl1-f169.google.com with SMTP id ke16so2422233plb.6 for <62696@debbugs.gnu.org>; Sun, 09 Apr 2023 05:11:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1681042304; x=1683634304; 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=2yAyRbf7dImHgixdxgC8YHVVE21cwjz7Xqkz7HtnnwY=; b=i2HUQ1GrqSof1Au61wNEJeWwwAfyOpNw5KEHYCLyOCy4pkGwgTR23zimnRgYbnZe3f ZRdWClWbUpzda/CdJDysEbcX7/q1NC6tgdxTPJa8kOrp/mKi35K57Ok3kD+9yWs65QjG WGQIA9g0K+e4DxGuMw6d6x7/cebn+ULXbd/hjv7R3Wq8fKMrdyDwlNmF7zLZYTl8gK2B OEm/3nUs+ddQvIxhbb65BzgBeygDZT74DMHlEdsxr0+y2aE4jHfdryg3zUchlsaMHdCY SG0NRrDdF85Rw/omPrN4jh5t4CxeayMSmqM3nuo3FNsCIQPad2m4uSRI5LcTH3g3h/ae KJOg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1681042304; x=1683634304; 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=2yAyRbf7dImHgixdxgC8YHVVE21cwjz7Xqkz7HtnnwY=; b=J9VgLF31CW8xsyrHFRkOzU6piwH0P5Mtvx/G9eAQHaQ6dUUdUf2/inqmNqKJA6xbr9 mr7nLdmGhbhlHJsoWi71B8rReolYkOcQrW8G6IXwdUgwpYqunFd+7w8ztCBsNzqwiS8v 68PDeYwhZYTJsC6rdO2aIJXuL5JJZ+1tPoG9Myr7F0X1TEqMqw3Ss4PimO3mZUiJPQ0V NWZELqdRKqS3YeD6Tx0LfUD/zPZnzoz/GsN86fmiO3/b78hfhza/iHXP7bjk11awwcmm fFa9YPE55rtcVXPceOWSlFctZyGrgYw4sl2PYoCKx/VuwNOSpskU+E857WTUJQK9huBe E/TQ== X-Gm-Message-State: AAQBX9eqYY9l7A1lA9Zk1nI9Tr5vXZVQhWYAnCk3RmEKv1OlVkQqzZwZ iLqohSSeEjahpGch5Sfw0mM= X-Google-Smtp-Source: AKy350bdfca6/ch66Oho9WN4FhUacg/JLqrFgfbMvEw9SxGioH2H9ffmihPPx7fiosfW9HM7PAPZ5g== X-Received: by 2002:a05:6a20:be1e:b0:db:e054:826a with SMTP id ge30-20020a056a20be1e00b000dbe054826amr9519019pzb.5.1681042304007; Sun, 09 Apr 2023 05:11:44 -0700 (PDT) Original-Received: from localhost (58x12x133x161.ap58.ftth.ucom.ne.jp. [58.12.133.161]) by smtp.gmail.com with ESMTPSA id h10-20020a056a001a4a00b00594235980e4sm5940272pfv.181.2023.04.09.05.11.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 09 Apr 2023 05:11:43 -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:259517 Archived-At: --Multipart_Sun_Apr__9_21:11:26_2023-1 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Gustaf Waldemarson wrote: > One noticeable caveat is that **any** parenthesis can now be additionally > indented, e.g., the follow is now also possible: >=20 > this_is_a_tuple =3D (long_variable_name_here, > also_a_long_variable_name) >=20 > Although, given that this can be cycled at will by the user, I'm not sure= if it > is a bad additional feature or not. >=20 > Ideally, I suppose that `python-indent-context` could be modified to add a > `:inside-cond-paren` symbol that signals that the parenthesis is for a > conditional expression and thus the extra indentation should be applied, = and not > in any other case. That does seem a bit harder for me to fix at a cursory= glance > however, so maybe this fix is enough? Hi Gustaf, I agree with you in that it's better to have a new indent context, and I tried to implement it. At first, I thought that it would be enough to add a counterpart of the user option `python-indent-def-block-scale' and corresponding `:inside-paren-newline-start-from-block' context. `python-indent-def-block-scale' can be used to customize the following code #+begin_src python if ( "VALUE" in my_unnecessarily_long_dictionary and some_other_long_condition_case ): do_something() #+end_src to be indented as follows (with a TAB at "):" line): #+begin_src python if ( "VALUE" in my_unnecessarily_long_dictionary and some_other_long_condition_case ): do_something() #+end_src This is the style used by the popular formatter "black". =46rom the name `python-indent-def-block-scale' and its docstring, it is easy to assume that it only works for def block, but in fact it works for every blocks. As `python-indent-def-block-scale' works only when there is no item on the same line following the opening paren, I tried to add a similar user option and an indent context for the opening paren followed by some items on the same line. It could indent as follows: #+begin_src python if ("VALUE" in my_unnecessarily_long_dictionary and some_other_long_condition_case): do_something() #+end_src However, it could not handle correctly the following example: #+begin_src python elif (some_case or another_case): do_another() #+end_src The extra indentation is not needed here. So I think it is best to increase the indentation only if the calculated indentation equals to the indentation of the contents of the block ("do_something()" in the above example). This is similar to the way I fixed Bug#57262. Unlike Bug#57262, the current indentation shown below is not a violation of the latest PEP8: #+begin_src python if ("VALUE" in my_unnecessarily_long_dictionary and some_other_long_condition_case): do_something() #+end_src Although pycodestyle reports E129 "visually indented line with same indent as next logical line," PEP8 was changed to allow this. This is explained in the following issue, for example: https://github.com/PyCQA/pycodestyle/issues/474 So changing this indentation should be a user option. Attached is my implementation of this. The user option `python-indent-block-paren-deeper' is added to customize this indentation. I would be glad if you could try it. --Multipart_Sun_Apr__9_21:11:26_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 acec03331413b621843fb1489e9aeb6db927614a Mon Sep 17 00:00:00 2001 From: kobarity Date: Sun, 9 Apr 2023 20:48:00 +0900 Subject: [PATCH] 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 | 8 ++ lisp/progmodes/python.el | 43 +++++++++-- test/lisp/progmodes/python-tests.el | 116 +++++++++++++++++++++++++++- 3 files changed, 158 insertions(+), 9 deletions(-) diff --git a/etc/NEWS b/etc/NEWS index d20d9f65ac9..61736e8edf4 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -261,6 +261,14 @@ When non-nil, it will automatically register every package as a project, that you can quickly select using 'project-switch-project' ('C-x p p'). +** Python mode + +*** New user option 'python-indent-block-paren-deeper'. +If this variable is set to non-nil, and some items follow the opening +paren on the same line in a block statement, and the calculated +indentation of a line inside the parens equals to the indentation of +the block contents, the indentation level of the line is increased. + * New Modes and Packages in Emacs 30.1 diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index bbabce80b4d..dbc48038554 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -1270,6 +1270,17 @@ python-indent-def-block-scale :type 'integer :safe 'natnump) +(defcustom python-indent-block-paren-deeper nil + "Non-nil means to increase indentation inside parens of a block. +If this variable is set to non-nil, and some items follow the +opening paren on the same line in a block statement, and the +calculated indentation of a line inside the parens equals to the +indentation of the block contents, the indentation level of the +line is increased." + :version "30.1" + :type 'boolean + :safe 'booleanp) + (defvar python-indent-current-level 0 "Deprecated var available for compatibility.") @@ -1367,6 +1378,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 +1465,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 +1622,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_Sun_Apr__9_21:11:26_2023-1--