From: kobarity <kobarity@gmail.com>
To: Gustaf Waldemarson <gustaf.waldemarson@gmail.com>
Cc: 62696@debbugs.gnu.org, Eli Zaretskii <eliz@gnu.org>,
npostavs@users.sourceforge.net, gastove@gmail.com,
dgutov@yandex.ru
Subject: bug#62696: python.el: Extra indentation for conditionals
Date: Thu, 20 Apr 2023 23:19:51 +0900 [thread overview]
Message-ID: <eke7cz3yk54o.wl-kobarity@gmail.com> (raw)
In-Reply-To: <CABehr5fVqRiwPycAsXbyh3+zLdD7aCXmGru9A56oWy=ENO72Lw@mail.gmail.com>
[-- Attachment #1: Type: text/plain, Size: 1086 bytes --]
Gustaf Waldemarson wrote:
> It mostly looks good to me, but perhaps it would make sense to still output
> the closing paren on the same line as the first example? I suppose it might
> not make sense from a style-perspective, but I guess someone reading through
> this mostly care about comparing indentation depths
Do you mean
def do_something(
arg1,
arg2):
print('hello')
is better than the following?
def do_something(
arg1,
arg2
):
print('hello')
If so, I think it is still good. I updated the patches.
> > Do these two patches replace every other patch posted in this
> > discussion?
> > Are these two patches ready to be installed, or are you still
> > discussing the issue?
>
> I believe that's the case, but I would wait for a response from Kobarity
> following this.
Yes, I'm attaching the patches which replace every other patches in
old mails. I was just waiting to see if anyone had any comments or
feedback. So, unless there is an objection, the patches are ready to
be installed on the master branch.
[-- Attachment #2: 0001-Add-a-new-user-option-in-Python-mode-to-improve-the-.patch --]
[-- Type: application/octet-stream, Size: 11493 bytes --]
From 71471c48aced70e52568feebb2bc40ebf02d6e7e Mon Sep 17 00:00:00 2001
From: kobarity <kobarity@gmail.com>
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()
+
\f
* 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
[-- Attachment #3: 0002-Improve-docstring-of-python-indent-def-block-scale.patch --]
[-- Type: application/octet-stream, Size: 1653 bytes --]
From 0c235d8a82db8881fad3f5c4bc78ab0d9ccf4fc2 Mon Sep 17 00:00:00 2001
From: kobarity <kobarity@gmail.com>
Date: Thu, 20 Apr 2023 22:55:29 +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 | 25 ++++++++++++++++++++++++-
1 file changed, 24 insertions(+), 1 deletion(-)
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 9ae95ecfc1c..5bb15c60956 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1265,7 +1265,30 @@ 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
next prev parent reply other threads:[~2023-04-20 14:19 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-06 12:47 bug#62696: python.el: Extra indentation for conditionals Gustaf Waldemarson
2023-04-09 12:11 ` kobarity
2023-04-10 11:11 ` Gustaf Waldemarson
2023-04-10 15:21 ` kobarity
2023-04-11 11:11 ` Gustaf Waldemarson
2023-04-12 15:26 ` kobarity
2023-04-16 13:24 ` kobarity
2023-04-16 15:49 ` Gustaf Waldemarson
2023-04-18 14:23 ` kobarity
2023-04-20 8:22 ` Eli Zaretskii
2023-04-20 13:40 ` Gustaf Waldemarson
2023-04-20 14:19 ` kobarity [this message]
2023-04-20 17:44 ` Gustaf Waldemarson
2023-04-22 9:33 ` Eli Zaretskii
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=eke7cz3yk54o.wl-kobarity@gmail.com \
--to=kobarity@gmail.com \
--cc=62696@debbugs.gnu.org \
--cc=dgutov@yandex.ru \
--cc=eliz@gnu.org \
--cc=gastove@gmail.com \
--cc=gustaf.waldemarson@gmail.com \
--cc=npostavs@users.sourceforge.net \
/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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.