An Emacs mode for editing Python code

reindenting a region does move whole region to correct level

Reported by Benjamin Peterson on 2012-12-22
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-mode.el
Wishlist
Andreas Roehler

Bug Description

If I have code like:

v = 4
if cond:
    v += 9
    invoke()

And I remove the "if":

v = 4
    v += 9
    invoke()

selecting the whole segment of code and hitting tab doesn't fix the indentation of the 2 last lines.

Changed in python-mode:
milestone: none → 6.1.1
assignee: nobody → Andreas Roehler (a-roehler)
importance: Undecided → Medium
Andreas Roehler (a-roehler) wrote :

Could implement a new function py-indent-line-or-region, which would work on an active region.
TAB then would call that instead of just py-indent-line.

OTOH indent-region already exists and is bound to C-M-\

It's not hard to write a py-indent-line-or-region, however note sure if we need this.
WDYT?
Would be nice if Barry or others comment too.

Andreas

Benjamin Peterson (gutworth) wrote :

Whatever was bound to TAB used to have this behavior, which is why I reported the bug.

Benjamin Peterson (gutworth) wrote :

Putting the whole region in the correct indentation is also what other modes like cc-mode do.

Changed in python-mode:
importance: Medium → Wishlist
Andreas Roehler (a-roehler) wrote :

customizing py-tab-indents-region-p to `t' should make that feature work
thanks for the suggestion BTW

Changed in python-mode:
status: New → Fix Committed
Benjamin Peterson (gutworth) wrote :

Thanks. I don't want it to always indent the region, though. I want it to fix the indentation of the region. For example, if the region is

    x = 4
        y *= 23
    return x + y

then tab should move the middle line to the correct level.

Andreas Roehler (a-roehler) wrote :

Presently TAB is bound to indent/cycle the current line.
No problem to make it call `(auto) indent-region' too.

However, I'm afraid some confusion will result from this.
auto-indent always takes the outmost reasonable indent.

It would work for your example, but in case an `if' is between,
it would indent the last line

x = 4
if True:
        y *= 23
        return x + y

IMHO binding auto-indent to TAB would just move the problem, also creating unexpected actions.
If you want to re-indent the region after a change, C-M-\ is the key.

Anyway, no a great thing to do.
Should you not be convinced yet, please drop a note again and I'll bind it as an option.

Changed in python-mode:
status: Fix Committed → New
Benjamin Peterson (gutworth) wrote :

I guess I can learn to use C-M-\. Tab in cc-mode does indent-region, so that's where my intuition comes from. I'd appreciate an option to change it.

Andreas Roehler (a-roehler) wrote :

`py-tab-indents-region-p' renamed `py-tab-shifts-region-p'

`py-tab-indents-region-p' re-introduced with different meaning:

In case the first line of region is indented correctly (outmost), first TAB will re-indent the other line

When `py-tab-shifts-region-p' is `t', the region is shiftet that way.

Changed in python-mode:
status: New → Fix Committed
Changed in python-mode:
status: Fix Committed → Fix Released
To post a comment you must log in.
This report contains Public information  Edit
Everyone can see this information.

Other bug subscribers