unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#62696: python.el: Extra indentation for conditionals
@ 2023-04-06 12:47 Gustaf Waldemarson
  2023-04-09 12:11 ` kobarity
  0 siblings, 1 reply; 14+ messages in thread
From: Gustaf Waldemarson @ 2023-04-06 12:47 UTC (permalink / raw)
  To: 62696


[-- Attachment #1.1: Type: text/plain, Size: 15629 bytes --]

Hello,

The follow can be considered either a feature request or a bug and was
originally asked as question on the Emacs StackOverflow page:

<
https://emacs.stackexchange.com/questions/76257/python-mode-multi-line-conditional-indentation
>

But I'll reproduce it in full here anyways:

Given the following somewhat silly Python code with a multi-line condition:

    if ("VALUE" in my_uncessarily_long_dictionary and
        some_other_long_condition_case):
        do_something()
    else:
        pass

This is written as Emacs typically indents each line, which I'm normally
fine
with. However, quite a few linters prefer the condition to be indented
visually
different from the next (non-conditional) line, e.g., something like this:

    if ("VALUE" in my_uncessarily_long_dictionary and
            some_other_long_condition_case):
        do_something():
    else:
        pass

As was helpfully provided by Tobias in the answer to this question on
StackOverflow, this was solved on Emacs-28.1. I went ahead and refactored
and
tested it on Emacs-master and it appears to be working fine there as well.
The
code for this is attached to this email.

One noticeable caveat is that **any** parenthesis can now be additionally
indented, e.g., the follow is now also possible:

    this_is_a_tuple = (long_variable_name_here,
                                      also_a_long_variable_name)

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.

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?

Best regards,
Gustaf


In GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.33, cairo version 1.16.0) of 2023-03-26 built on ShadowX
Repository revision: 248aabd9514c9eb21f4aa2e9062f8ce927d9fe54
Repository branch: master
System Description: Ubuntu 22.04.2 LTS

Configured using:
 'configure --prefix=/home/xaldew/.local
 '--program-transform-name=s/^ctags$/ctags.emacs/' --without-makeinfo
 --with-xpm=ifavailable --with-jpeg=ifavailable --with-gif=ifavailable
 --with-tiff=ifavailable'

Configured features:
CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG LCMS2
LIBSELINUX LIBXML2 MODULES NOTIFY INOTIFY PDUMPER PNG RSVG SECCOMP SOUND
THREADS TIFF TOOLKIT_SCROLL_BARS WEBP X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB

Important settings:
  value of $LANG: sv_SE.UTF-8
  value of $XMODIFIERS: @im=ibus
  locale-coding-system: utf-8-unix

Major mode: Magit

Minor modes in effect:
  pyvenv-mode: t
  dap-tooltip-mode: t
  dap-ui-many-windows-mode: t
  dap-ui-controls-mode: t
  dap-ui-mode: t
  gdb-many-windows: t
  treemacs-filewatch-mode: t
  treemacs-follow-mode: t
  treemacs-git-mode: t
  treemacs-fringe-indicator-mode: t
  dap-auto-configure-mode: t
  dap-mode: t
  global-git-commit-mode: t
  magit-auto-revert-mode: t
  shell-dirtrack-mode: t
  beacon-mode: t
  server-mode: t
  hes-mode: t
  projectile-mode: t
  global-display-fill-column-indicator-mode: t
  global-display-line-numbers-mode: t
  global-so-long-mode: t
  yas-global-mode: t
  yas-minor-mode: t
  global-company-mode: t
  company-mode: t
  global-undo-tree-mode: t
  undo-tree-mode: t
  global-anzu-mode: t
  anzu-mode: t
  global-atomic-chrome-edit-mode: t
  windmove-mode: t
  which-key-mode: t
  anyclip-mode: t
  override-global-mode: t
  electric-pair-mode: t
  save-place-mode: t
  global-subword-mode: t
  subword-mode: t
  winner-mode: t
  global-auto-revert-mode: t
  xterm-mouse-mode: t
  savehist-mode: t
  ido-everywhere: t
  tooltip-mode: t
  global-eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  buffer-read-only: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
/home/xaldew/.config/emacs/elpa/transient-20230315.1520/transient hides
/home/xaldew/.local/share/emacs/30.0.50/lisp/transient

Features:
(shadow sort bbdb-message mail-extr warnings emacsbug cdlatex reftex
reftex-loaddefs reftex-vars org-element org-persist org-id org-refile
avl-tree oc-basic ol-eww eww xdg url-queue mm-url ol-rmail ol-mhe ol-irc
ol-info ol-gnus nnselect gnus-art mm-uu mml2015 mm-view mml-smime smime
dig gnus-sum shr pixel-fill kinsoku url-file svg gnus-group gnus-undo
gnus-start gnus-dbus dbus gnus-cloud nnimap nnmail mail-source utf7 nnoo
gnus-spec gnus-int gnus-range gnus-win gnus nnheader range ol-docview
doc-view jka-compr ol-bibtex bibtex ol-bbdb ol-w3m org-tempo tempo
cus-start ob-latex ob-plantuml ob-org ob-shell ob-gnuplot ob-C ob-python
ob-ditaa ob-dot org ob ob-tangle ob-ref ob-lob ob-table ob-exp org-macro
org-src ob-comint org-pcomplete org-list org-footnote org-faces
org-entities ob-emacs-lisp ob-core ob-eval org-cycle org-table ol
org-fold org-fold-core org-keys oc org-loaddefs cal-menu calendar
cal-loaddefs org-version org-compat org-macs shortdoc help-fns
radix-tree magit-annex magit-patch magit-subtree magit-gitignore
magit-ediff ediff ediff-merg ediff-mult ediff-wind ediff-diff ediff-help
ediff-init ediff-util image-mode exif guess-language pyvenv eshell
esh-cmd esh-ext esh-opt esh-proc esh-io esh-arg esh-module esh-groups
esh-util highlight-indentation anaconda-mode pythonic tramp
tramp-loaddefs trampver tramp-integration files-x tramp-compat
parse-time iso8601 ls-lisp python-mode/.yas-setup.el dap-python python
treesit c++-genostream cmake-mode rst ace-window all-the-icons
all-the-icons-faces data-material data-weathericons data-octicons
data-fileicons data-faicons data-alltheicons vc-hg vc-bzr vc-src vc-sccs
vc-svn vc-cvs vc-rcs log-view vc bug-reference misearch multi-isearch
lsp-diagnostics lsp-headerline lsp-icons lsp-modeline dap-mouse dap-ui
gdb-mi bui bui-list bui-info bui-entry bui-core bui-history bui-button
bui-utils lsp-lens vc-git vc-dispatcher modern-cpp-font-lock
.yas-setup.el cc-mode/.yas-setup.el view lsp-zig lsp-tilt lsp-steep
lsp-svelte lsp-sqls lsp-ruby-syntax-tree lsp-ruby-lsp lsp-yaml lsp-xml
lsp-vimscript lsp-vhdl lsp-volar lsp-vetur lsp-html lsp-verilog lsp-vala
lsp-v lsp-typeprof lsp-ttcn3 lsp-toml lsp-terraform lsp-tex lsp-sorbet
lsp-solargraph lsp-rust lsp-rf lsp-ruff-lsp lsp-remark lsp-racket lsp-r
lsp-purescript lsp-pylsp lsp-pyls lsp-pwsh lsp-php lsp-pls
lsp-perlnavigator lsp-perl lsp-openscad lsp-ocaml lsp-magik lsp-nix
lsp-nim lsp-nginx lsp-mint lsp-marksman lsp-markdown lsp-lua lsp-ltex
lsp-kotlin lsp-json lsp-javascript lsp-idris lsp-haxe lsp-groovy
lsp-hack lsp-graphql lsp-gleam lsp-go lsp-completion lsp-gdscript
lsp-fsharp lsp-fortran lsp-eslint lsp-erlang lsp-emmet lsp-elixir
lsp-elm lsp-dockerfile lsp-dhall lsp-d lsp-css lsp-csharp gnutls
lsp-crystal lsp-cmake lsp-clojure lsp-treemacs lsp-treemacs-generic
lsp-treemacs-themes treemacs-treelib treemacs treemacs-header-line
treemacs-compatibility treemacs-mode treemacs-interface
treemacs-persistence treemacs-filewatch-mode treemacs-follow-mode
treemacs-rendering treemacs-annotations treemacs-async
treemacs-workspaces treemacs-dom treemacs-visuals
treemacs-fringe-indicator treemacs-scope pulse treemacs-faces
treemacs-icons treemacs-themes treemacs-core-utils pfuture hl-line
treemacs-logging treemacs-customization treemacs-macros
lsp-semantic-tokens lsp-clangd lsp-beancount lsp-bash lsp-astro
lsp-ansible lsp-angular lsp-ada lsp-actionscript dap-gdb-lldb dap-utils
dom xml dap-mode dap-tasks dap-launch lsp-docker yaml posframe
dap-overlays magit-extras magit-submodule magit-blame magit-stash
magit-reflog magit-bisect magit-push magit-pull magit-fetch magit-clone
magit-remote magit-commit magit-sequence magit-notes magit-worktree
magit-tag magit-merge magit-branch magit-reset magit-files magit-refs
magit-status magit magit-repos magit-apply magit-wip magit-log
which-func magit-diff smerge-mode diff-mode git-commit log-edit message
sendmail yank-media rfc822 mml mml-sec epa derived gnus-util time-date
mm-decode mm-bodies mm-encode mail-parse rfc2231 rfc2047 rfc2045 mm-util
ietf-drums mail-prsvr mailabbrev mail-utils gmm-utils mailheader
pcvs-util add-log magit-core magit-autorevert magit-margin
magit-transient magit-process with-editor shell pcomplete magit-mode
transient magit-git magit-base magit-section format-spec crm compat
dired-aux dired dired-loaddefs term/tmux term/xterm xterm pinentry
beacon server form-feed paredit nameless flyspell ispell whitespace
rainbow-delimiters highlight-escape-sequences projectile lisp-mnt grep
ibuf-ext ibuffer ibuffer-loaddefs lsp-ui lsp-ui-flycheck lsp-ui-doc
goto-addr lsp-ui-imenu lsp-ui-peek lsp-ui-sideline flycheck lsp-mode
tree-widget spinner network-stream puny nsm markdown-mode noutline
outline inline imenu f f-shortdoc ewoc epg rfc6068 epg-config compile
text-property-search lsp-ui-util face-remap find-func lsp-protocol s ht
dash display-fill-column-indicator display-line-numbers so-long
py-snippets yasnippet-radical-snippets yasnippet-snippets yasnippet
company-oddmuse company-keywords company-etags etags fileloop generator
xref project company-gtags company-dabbrev-code company-dabbrev
company-files company-clang company-capf company-cmake company-semantic
company-template company-bbdb company undo-tree diff queue anzu
thingatpt atomic-chrome websocket bindat let-alist
color-theme-approximate advice color delim-col hydra-examples windmove
rect hydra lv bbdb bbdb-site timezone cus-edit pp cus-load icons
wid-edit ace-link avy which-key anyclip-mode cl-extra help-mode edmacro
kmacro diminish use-package use-package-ensure use-package-delight
use-package-diminish use-package-bind-key bind-key easy-mmode
use-package-core finder-inf local-autoloads cwarn cc-mode cc-fonts
cc-guess cc-menus cc-cmds cc-styles cc-align cc-engine cc-vars cc-defs
elec-pair saveplace cap-words superword subword winner autorevert
filenotify xt-mouse tango-dark-theme savehist ido gud comint ansi-osc
ansi-color ring keybinds terminals ivy-autoloads
spacemacs-theme-autoloads ace-link-autoloads kotlin-mode-autoloads
company-auctex-autoloads gl-conf-mode-autoloads css-eldoc-autoloads
beacon-autoloads lsp-ltex-autoloads paredit-autoloads
glsl-mode-autoloads highlight-escape-sequences-autoloads
powershell-autoloads gnuplot-mode-autoloads cuda-mode-autoloads
ein-autoloads request-autoloads ssh-config-mode-autoloads
git-modes-autoloads mmm-mode-autoloads ebdb-autoloads rmsbolt-autoloads
debbugs-autoloads dart-mode-autoloads sublime-themes-autoloads
coffee-mode-autoloads graphviz-dot-mode-autoloads magit-annex-autoloads
opencl-mode-autoloads json-mode-autoloads rx company-anaconda-autoloads
pinentry-autoloads rainbow-mode-autoloads htmlize-autoloads
projectile-autoloads flycheck-rust-autoloads bbdb-vcard-autoloads
jira-markup-mode-autoloads browse-kill-ring-autoloads
helm-dash-autoloads sx-autoloads jenkins-autoloads smart-jump-autoloads
bbdb-autoloads which-key-autoloads anzu-autoloads x86-lookup-autoloads
cmake-mode-autoloads flycheck-kotlin-autoloads undo-tree-autoloads
rust-mode-autoloads plantuml-mode-autoloads magit-gerrit-autoloads
lua-mode-autoloads yaml-mode-autoloads evil-autoloads deferred-autoloads
iedit-autoloads unfill-autoloads srefactor-autoloads abc-mode-autoloads
autoinsert solarized-theme-autoloads alert-autoloads gntp-autoloads
emms-autoloads cider-autoloads sesman-autoloads queue-autoloads
parseedn-autoloads parseclj-autoloads clojure-mode-autoloads
flycheck-package-autoloads package-lint-autoloads
atomic-chrome-autoloads websocket-autoloads powerthesaurus-autoloads
jeison-autoloads dash-docs-autoloads rainbow-delimiters-autoloads
form-feed-autoloads yasnippet-radical-snippets-autoloads
pyimport-autoloads shut-up-autoloads json-snatcher-autoloads
helm-lsp-autoloads helm-autoloads clang-format-autoloads
poly-markdown-autoloads polymode-autoloads toml-mode-autoloads
diminish-autoloads cdlatex-autoloads eclim-autoloads anaphora-autoloads
py-snippets-autoloads yasnippet-snippets-autoloads
zenburn-theme-autoloads dap-mode-autoloads lsp-docker-autoloads
lsp-treemacs-autoloads treemacs-autoloads cfrs-autoloads
posframe-autoloads pfuture-autoloads ace-window-autoloads avy-autoloads
bui-autoloads magit-svn-autoloads magit-autoloads pcase
git-commit-autoloads with-editor-autoloads transient-autoloads
go-mode-autoloads goto-chg-autoloads log4e-autoloads
color-theme-approximate-autoloads cargo-autoloads calfw-autoloads
company-quickhelp-autoloads helm-core-autoloads async-autoloads
ecb-autoloads modern-cpp-font-lock-autoloads dts-mode-autoloads
nov-autoloads esxml-autoloads kv-autoloads company-math-autoloads
math-symbol-lists-autoloads elpy-autoloads pyvenv-autoloads
highlight-indentation-autoloads ob-ipython-autoloads
dash-functional-autoloads hydra-autoloads auto-complete-auctex-autoloads
auto-complete-autoloads yasnippet-autoloads auctex-latexmk-autoloads
auctex-autoloads tex-site yaml-autoloads ahk-mode-autoloads
magit-section-autoloads compat-autoloads cask-mode-autoloads
company-c-headers-autoloads company-autoloads csv-mode-autoloads
gnuplot-autoloads anaconda-mode-autoloads pythonic-autoloads
pos-tip-autoloads zerodark-theme-autoloads all-the-icons-autoloads
image+-autoloads expand-region-autoloads gnus-desktop-notify-autoloads
flycheck-autoloads pkg-info-autoloads epl-autoloads ggtags-autoloads
web-mode-autoloads lsp-ui-autoloads lsp-mode-autoloads lv-autoloads
markdown-mode-autoloads spinner-autoloads ht-autoloads f-autoloads
uimage-autoloads guess-language-autoloads dumb-jump-autoloads
popup-autoloads dash-autoloads s-autoloads google-c-style-autoloads
nameless-autoloads info slime-autoloads macrostep-autoloads package
browse-url url url-proxy url-privacy url-expand url-methods url-history
url-cookie generate-lisp-file url-domsuf url-util mailcap url-handlers
url-parse auth-source cl-seq eieio eieio-core cl-macs password-cache
json subr-x map byte-opt gv bytecomp byte-compile url-vars cl-loaddefs
cl-lib rmc iso-transl tooltip cconv eldoc paren electric uniquify
ediff-hook vc-hooks lisp-float-type elisp-mode mwheel term/x-win x-win
term/common-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode lisp-mode prog-mode register
page tab-bar menu-bar rfn-eshadow isearch easymenu timer select
scroll-bar mouse jit-lock font-lock syntax font-core term/tty-colors
frame minibuffer nadvice seq simple cl-generic indonesian philippine
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite emoji-zwj charscript
charprop case-table epa-hook jka-cmpr-hook help abbrev obarray oclosure
cl-preloaded button loaddefs theme-loaddefs faces cus-face macroexp
files window text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget keymap hashtable-print-readable backquote
threads dbusbind inotify lcms2 dynamic-setting system-font-setting
font-render-setting cairo move-toolbar gtk x-toolkit xinput2 x multi-tty
make-network-process emacs)

Memory information:
((conses 16 817895 212638)
 (symbols 48 62999 0)
 (strings 32 249530 19612)
 (string-bytes 1 8101995)
 (vectors 16 160320)
 (vector-slots 8 3855694 143961)
 (floats 8 1381 10031)
 (intervals 56 21064 2868)
 (buffers 976 49))

[-- Attachment #1.2: Type: text/html, Size: 16950 bytes --]

[-- Attachment #2: 0001-Allow-indent-in-long-python-mode-conditionals.patch --]
[-- Type: text/x-patch, Size: 1538 bytes --]

From 8dc2e7275d68d2749822b59e4748794ab27621c5 Mon Sep 17 00:00:00 2001
From: Gustaf Waldemarson <gustaf.waldemarson@gmail.com>
Date: Tue, 4 Apr 2023 14:04:44 +0200
Subject: [PATCH] Allow indent in long python-mode conditionals.

* lisp/progmodes/python.el: Allow additional indentation in
parentheses to more easily satisfy various Python linters.
---
 lisp/progmodes/python.el | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index bbabce80b4d..d7c6a9218c7 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1248,6 +1248,11 @@ python-indent-offset
   :type 'integer
   :safe 'integerp)
 
+(defcustom python-indent-add-after-paren python-indent-offset
+  "Additional indent after paren."
+  :type 'number
+  :group 'python)
+
 (defcustom python-indent-guess-indent-offset t
   "Non-nil tells Python mode to guess `python-indent-offset' value."
   :type 'boolean
@@ -1580,7 +1585,10 @@ python-indent--calculate-indentation
                ;; Add one level to avoid same indent as next logical line.
                (+ column python-indent-offset)
              column)))
-        (`(,(or :inside-paren
+        (`(:inside-paren . ,start)
+         (goto-char start)
+         (+ (current-column) python-indent-add-after-paren))
+        (`(,(or :after-backslash-block-continuation
                 :after-backslash-dotted-continuation) . ,start)
          ;; Use the column given by the context.
          (goto-char start)
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-04-22  9:33 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2023-04-20 17:44                       ` Gustaf Waldemarson
2023-04-22  9:33                       ` Eli Zaretskii

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).