Autoindent not working properly in python?


#1

Hi,

I am putting the code below in a python script and then I do auto indent. The results are as you can see below. Is this a bug? I am selecting all the code before indenting, without selecting anything nothing happens (as I would expect). I am using Atom 1.0.7-0a6da38 in a MacBook Air from early 2014 with Scientific Linux 7.

There is a similar topic in Auto indent doesn't work properly but for C.

BEFORE INDENTING

def printinfo( name, age = 35 ):
"This prints a passed info into this function"
print "Name: ", name
print "Age ", age
return 5;

def printsomething( name, age = 35 ):
"This prints a passed info into this function"
print "Name: ", name
print "Age ", age
return 3;

returncode = printinfo("somename",110)
returncod  = printsomething("something",11)

print returncode
print returncod

AFTER INDENTING

def printinfo( name, age = 35 ):
    "This prints a passed info into this function"
    print "Name: ", name
    print "Age ", age
    return 5;
    
    def printsomething( name, age = 35 ):
        "This prints a passed info into this function"
        print "Name: ", name
        print "Age ", age
        return 3;
        
        returncode = printinfo("somename",110)
        returncod  = printsomething("something",11)
        
        print returncode
        print returncod

Better API for indent
#2

I’m not sure any system would know which indentation you intended to use here.

The indentation that was used automatically makes perfect sense and is valid syntax, and there is no logical reason that it should be otherwise, other than your personal intention or preference which the auto-indent feature wouldn’t be able to guess.

Of course one could reason that the usage of the functions later on is a good indicator. However, nothing prevents you from using the code the way the auto-indent suggests here.

It’s ambiguous and you should probably do the indentation on such code yourself.


#3

There is a topic here that talks about the challenges of auto-indenting indentation-significant languages like Python, CoffeeScript, HAML, et al:


#4

Hi Leedohm,

I read the linked thread. However in the example I posted “return 3/5” definitely ends the function and could be used to set the end of the indentation.

In case of a if, the else would do it. However for a for loop this would be a problem but a comment like #end would tell the machine where to stop the current indentation and this comment could be chosen in the options, a package could do this type of thing. Is there a reason why Atom does not do that?

By the way https://atom.io/packages/auto-indent seems to be just the autoindent command but without needing the code to be highlighted.


#5

actually as https://www.python.org/dev/peps/pep-0008/#blank-lines states
autoindent could use single and double blank lines for this. i tried if it already works with double blank lines as top level separator but that isn’t the case.

hope this helps.


#6

Off-topic comment: the semicolons in the code serve no purpose – they’re neither required nor recommended. They’re only for separating two statements on the same line – which you usually shouldn’t do.


#7

Generally, whenever that question crosses anybody’s mind, the most likely answer is that the Atom core dev team hasn’t yet been forced to work 22 hours a day on every commentator’s favorite Atom enhancements.

The application proper is little more than a year old really, tremendous efforts have been made by core and community developers to make it what it is today. But there is no Microsoft or Apple level of resources behind this project, so a lot of features people would like Atom to have… will have to wait until someone from the community picks them up and creates them.

Having said that, I think a more sophisticated auto-indent would definitely be a good thing, and you seem to have a few ideas what might work well, so a package or a PR from you would probably be quite welcome.