reindenting a region does move whole region to correct level

Bug #1093026 reported by Benjamin Peterson
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
python-mode.el
Fix Released
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
Revision history for this message
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

Revision history for this message
Benjamin Peterson (benjaminp) wrote :

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

Revision history for this message
Benjamin Peterson (benjaminp) 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
Revision history for this message
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
Revision history for this message
Benjamin Peterson (benjaminp) 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.

Revision history for this message
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
Revision history for this message
Benjamin Peterson (benjaminp) 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.

Revision history for this message
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  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.