Author Topic: Python autoindent if / for mistake  (Read 798 times)

jporkkahtc

  • Senior Community Member
  • Posts: 1828
  • Hero Points: 177
  • Text
Python autoindent if / for mistake
« on: September 11, 2017, 09:40:26 pm »
When typing in this code:
Code: [Select]
def fn(a,b,c,d):
    if a:
        for z in b:
            print(z)
        else:


Slick undents 1 tabstop when I enter "else:", but it should unindent 2 tabstops to match the if.

patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1125
  • Hero Points: 105
Re: Python autoindent if / for mistake
« Reply #1 on: September 12, 2017, 01:49:27 pm »
Reproduced, will take a look.

patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1125
  • Hero Points: 105
Re: Python autoindent if / for mistake
« Reply #2 on: September 12, 2017, 02:42:44 pm »
Oh, I forgot about this.  It does that because it starts with the innermost block, and the 'while' and 'for' statements can have an else clause. 

What I'll probably do is have it so if an 'else' matches with a 'while' or 'for' loop, we'll check to see if there's an outer 'if' or 'try', and if so we'll prefer the outer indent.  On the assumption that the 'else' clauses on loops are rarer than on 'if' and 'try'.

It also tempts me to put in a command that works like 'Tab cycles indent on empty line', but that works on the previous line, so you can just bang a key to get the correct indent when we choose a wrong initial indent.  I wanted something like that when playing with another indent sensitive language a while back, but apparently forgot to implement it.

jporkkahtc

  • Senior Community Member
  • Posts: 1828
  • Hero Points: 177
  • Text
Re: Python autoindent if / for mistake
« Reply #3 on: September 12, 2017, 03:09:57 pm »
Wow -- I didn't realize that while/for had an else clause!
Learn something new every day.

I've never seen else used on a loop - so certainly around here it is pretty rare.
Of course, now that I know it, maybe I will use it!

Hm...Looking at the Python docs, else on while doesn't seem so useful. It is only not executed if the loop terminates with a break.
Otherwise it is no different than simply having another statement after the loop.

So what Slick does now is technically correct, I think more people would be happy having it ignore the possibility of having else: on a while loop.


patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1125
  • Hero Points: 105
Re: Python autoindent if / for mistake
« Reply #4 on: September 12, 2017, 03:22:19 pm »
Yes, in the next drop it will prefer the indent of the outer 'if' over the inner 'for'.  It will still align with a for or while if there's no other likely choice it can make.   Thanks for the report.