Author Topic: Syntax tab indent for python broken in SE 12?  (Read 13830 times)

mrmouse

  • New Community Member
  • Posts: 1
  • Hero Points: 0
Syntax tab indent for python broken in SE 12?
« on: March 16, 2007, 02:37:40 AM »
I'm trying out SE 12, with what I think are the same indentation settings I'm using in SE 11 using syntax indentation with tabs, and not getting even close to the same results let alone valid levels of indentation.  I've tried with Java and indentation works fine, but with Python it goes crazy if you try syntax indent.  Here's what happens when I try to create a simple class and let the SE indentation take control, and the settings I'm using, and the same class when using the same settings in SE 11.



If I had hit return after that print "true" statement it would have indented way way even farther over to the right.  The settings...



Now in SE 11.



You can checkout the SE 11 settings attached at

http://community.slickedit.com/index.php?action=dlattach;topic=1269.0;attach=522;image
« Last Edit: March 16, 2007, 02:51:32 AM by mrmouse »

Ryan

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 986
  • Hero Points: 77
Re: Syntax tab indent for python broken in SE 12?
« Reply #1 on: March 16, 2007, 01:55:21 PM »
Indeed...backup your python.e in your macros directory, save the attached version and load it via Macro > Load Module...should be all set.

- Ryan

tea333

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Syntax tab indent for python broken in SE 12?
« Reply #2 on: May 14, 2007, 10:14:03 PM »
Does this apply to SE 10.0.3 as well?

I'm seeing some weird stuff where an IF statement like this:

if something == True:
     print "do this"

turns into this:

if something == True:
print "do this"

I'm still trying to track down when the indent got outdented. I'm using tools on Linux as well, but I think I used only SE on Friday but today I'm seeing the above when loading a file. It doesn't exist throughout the whole file so I'm not sure why some stuff is like that. It may be that I changed tab settings and either new changes or old changes are being affected. Either way, I want to straighten out my python settings with SE.

I was getting some python indentation errors on Linux that were not occurring on Windows. I thought I'd switch to using/saving actual tabs. Anyway, anything anyone can offer to make my SE python settings optimum would be helpful. Thanks.

Regards,
tom
 

tea333

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Syntax tab indent for python broken in SE 12?
« Reply #3 on: May 14, 2007, 11:00:37 PM »
Hi All,

I think I see what's going on. A coworker told me that it is agreed upon to use spaces here. That makes sense given the problems that can tabs can bring to cross platform Python development. I work mostly in Windows but am currently in a heavy Linux environment. I was using VI to add some changes and got indent errors and felt Linux and VI must be right and SE and Windows must be wrong. ;) Turns out that VI (my particular installation of it) uses real tabs. When combined with my Python which was done mostly with SE on Windows, it created problems.

The solution is to either modify VI not to use tab characters or just use SlickEdit or the like, configured correctly, for all changes. My only problem was assuming that I had to go "tabs." Turns out - it seems - a lot of Python folks, especially those involved in cross platform Python dev, perfer spaces because they're the same everywhere.

Sorry for replying to my own message. But I figured someone else may benefit from the info.

**** I'm still interested in any tips for using SE as effectively as possible to edit Python.

Thanks again!
Tom

Kohei

  • Senior Community Member
  • Posts: 192
  • Hero Points: 25
Re: Syntax tab indent for python broken in SE 12?
« Reply #4 on: May 15, 2007, 07:13:05 PM »
The Python interpreter itself treats each tab character as 8-whitespace equivalent, so if your code has both whitespace and tab characters, then you need to keep that in mind and change the editor setting to match it.  Of course, using all whitespace makes it less confusing. :)

Quote
**** I'm still interested in any tips for using SE as effectively as possible to edit Python.

I'd really encourage you to upgrade to SE2007 (v12) if that's an option for you.  A lot has changed in Python support between v10 and the latest, so you're missing out a lot if you stick with v10.

Kohei

tea333

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Syntax tab indent for python broken in SE 12?
« Reply #5 on: May 16, 2007, 01:16:12 AM »
Thanks for the reply, info, and SE2007 tip!   :)

garion911

  • Senior Community Member
  • Posts: 201
  • Hero Points: 13
Re: Syntax tab indent for python broken in SE 12?
« Reply #6 on: May 16, 2007, 05:57:46 AM »
Hi, just a few comments on python and se2007,

I use SE2007 mostly for python work, in my day job.. So I do use it a bunch. I've found a few odd things about SE2007's python support..

One odd thing is comments.. I usually use # as my comment char.. Sometimes, I'll have one line of comments, I hit enter, expecting it to insert another #, and it doesn't, and the cursor is directly under the previous lines #. Then sometimes, it aagain doesn't insert the #, but the cursor is tabbed over (in my case 4 spaces.).. And then sometimes, it actaully does put the # in.

The other odd thing is that sometimes, inserting text sometimes doesn't get re-indented properly (just maintains the previous indenting..).. Most of the time it works, but on occasion, it won't..

I've never been able to constantly reproduce these (then again, I haven't tried that hard.), but they haven't overly bothered me. I have not contacted SE support about it yet.. For all I know, its something odd with my config (has happend before..).

Anyone else seeing symptoms like this?

tea333

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Syntax tab indent for python broken in SE 12?
« Reply #7 on: May 16, 2007, 07:58:58 AM »
Thanks for your response!

I haven't been doing tons of Python, but it seems to me that insert would not be able to always be intelligent, and indent at the desired position automatically. The reason I say this is because Python uses an indentation as a BEGIN block, and an end block only exists by virtue of an outdent. In many cases I've experienced, an insert could be indented at a number of places.

For example, say this is on the clipboard:

Code: [Select]
if blah==None:
    print "blah is none"

And you want to insert at the comment in the code below

Code: [Select]
print "abc"
if something=None:
    print "123"
    if thisThing==None:
        print "abc"
# insert code on clipboard here. Question is, at what indent?

The clipboard code could exist at several indent locations. Maybe I'm missing something but I can't see how it can automatically determine the desired level. If what I'm saying is correct, it would be nice to have a feature while editing python, where after pasting/inserting, the text stays highlighted and allows you to use keys to move back/forth at all possible indentation levels. Perhaps the user could configure the default chosen by SE. For example, the user could choose to default the insert indent position closest to cursor, or they could choose the inner most indent. I probably would go with inner most and, after insert, when SE keeps it highlighted, I would move it out until satisfied, or just hit enter to leave it as is.

Perhaps there's better stuff already. But that's what I've noticed so far with SE 10.x and Python. I've noticed that it doesn't always get it, and I wish I didn't have to go highlight the whole block just to fix it. It is not ergonomically optimum.

Anyway, thanks for your post and if you have any other tips, I hope you'll post em.

Regards,
Tom

garion911

  • Senior Community Member
  • Posts: 201
  • Hero Points: 13
Re: Syntax tab indent for python broken in SE 12?
« Reply #8 on: May 16, 2007, 06:43:06 PM »
My issue has been that it seems that SE has been picking randomly which level to indent to...

Say I have something like this:

Code: [Select]
somefunc()

if x == 7:
    pass

And then I try to move the somefunc() line into the if.. Sometimes, it does what I would expect, indent to the 'current' level:

Code: [Select]
if x == 7:
    pass
    somefunc()

But for some reason, sometimes it does this:

Code: [Select]
if x == 7:
    pass
somefunc()

And sometimes when I'm moving things around, I'll be changing several levels:

Code: [Select]
somefunc()

if x == 7:
    while x == 7:
        pass

Will come out like:

Code: [Select]
if x == 7:
    while x == 7:
        pass
somefunc()

I know Python's indentation can be difficult, but SE could be more consistent with it.

As I said, it hasn't been that big a deal for me. Just have to keep an eye on where things get pasted.

--Garion

tea333

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Syntax tab indent for python broken in SE 12?
« Reply #9 on: May 16, 2007, 07:08:04 PM »
Yeah, I just tried a test with 10.x and it just inserted right where it copied it from. Not much of a test though. I need to upgrade!


Thanks for your input!

Kohei

  • Senior Community Member
  • Posts: 192
  • Hero Points: 25
Re: Syntax tab indent for python broken in SE 12?
« Reply #10 on: May 16, 2007, 07:43:26 PM »
Garion,

SE treats the keyword pass as an end-of-scope indicator, and decreases the indentation of the next statement line by 1 syntax indent value, which is 4 by default.

Because the pass statement[1] is used as a space holder to retain the scope when no other statements are present, it's typically not used unless that's the only statement in that scope.  With that in mind, it does make sense that SE de-indents the next statement automatically.

So, by your examples:
Quote
if x == 7:
    pass
somefunc()
This is expected, because of the presence of the pass statement.  Try pasting the same line without the pass statement or before it.  It should go
Quote
if x == 7:
    somefunc()
and

Quote
if x == 7:
    while x == 7:
        pass
somefunc()
Are you sure this is what SE does?  Because it's supposed to do the following:
Quote
if x == 7:
    while x == 7:
        pass
    somefunc()
and my test confirms this to be the case.  Anyway, again, try pasting the same line without the pass statement or before it and see if that makes any difference.

There are several other statements that are used in a similar manner, such as continue, break, and return.  These statements are all used as an end-of-scope indicator.

So, SE is in fact consistent with regard to this.

If you don't like this behavior, and want to hack at this, :) you can remove or add keywords in python_indent_col function.  It's a Slick-C function, so you can get there by
Code: [Select]
fp python_indent_colScroll down a bit, and you'll see those keywords I listed above used inside a if statement.

Hope this sheds some light on this issue.

Kohei

[1] http://docs.python.org/tut/node6.html#SECTION006500000000000000000
« Last Edit: May 16, 2007, 08:05:01 PM by Kohei »

garion911

  • Senior Community Member
  • Posts: 201
  • Hero Points: 13
Re: Syntax tab indent for python broken in SE 12?
« Reply #11 on: May 16, 2007, 09:20:27 PM »
I had used 'pass' only as an example.. But that may explain some of the behavior I've been seeing.. I'll see when this happens again, and see if I can dup it..

tea333

  • Community Member
  • Posts: 9
  • Hero Points: 0
Re: Syntax tab indent for python broken in SE 12?
« Reply #12 on: May 16, 2007, 11:06:11 PM »
All educational for me as well, and to know what to expect with the newer version. Thanks everyone!

garion911

  • Senior Community Member
  • Posts: 201
  • Hero Points: 13
Re: Syntax tab indent for python broken in SE 12?
« Reply #13 on: May 17, 2007, 06:00:32 PM »
Ok, here's one instance where SE doesn't do quite what I would expect:

Code: [Select]
def a():
    return 1

class A:
    def __init__(self):
        self.x = 1

Now move the a() func into the class, as if you were going to add it to the class A.. I would except it to indent the insert so that it be a part of the class.. But instead, it indents at the same original level:

Code: [Select]
class A:
    def __init__(self):
        self.x = 1

def a():
    return 1

I would expect it to indent it:

Code: [Select]
class A:
    def __init__(self):
        self.x = 1

    def a():
        return 1


Kohei

  • Senior Community Member
  • Posts: 192
  • Hero Points: 25
Re: Syntax tab indent for python broken in SE 12?
« Reply #14 on: May 17, 2007, 06:40:38 PM »
Hi Garion,

Let me guess.  The place you are pasting into is the last position in the buffer, correct?  Put it another way, there is no more characters (blank or otherwise) following the point of insertion.  Am I right? :)

If yes, then this is a known limitation.  This happens because SE closes the module scope (or context) at the last character position in the current buffer (in Python, each file is a module and has its own module namespace).  And because in Python there is no explicit end-of-scope token, SE closes all the contexts at the last character position.  So, when you paste something into that, and because you're already outside the module scope, the indent position defaults to 1 since there is no context information available outside the module scope.

The workaround is to insert a few blank lines at the end of the buffer, which extends the module scope by a few lines.  Then SE should do a better job, although...

Quote
I would expect it to indent it:

Code: [Select]
class A:
    def __init__(self):
        self.x = 1

    def a():
        return 1

Well, SE does a little different.  This
Code: [Select]
class A:
    def __init__(self):
        self.x = 1
        def a():
            return 1
is what you'll get. :)  Keep in mind that Python allows nested functions, so it's hard to determine whether you are pasting it as a member function of class A, or nested function of A.__init__(self) at paste time.  Obviously this is not what you'd expect, but I don't think there is an easy way around it.

Kohei

« Last Edit: May 17, 2007, 07:19:30 PM by Kohei »