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