#3200 Colon causes unnecessary indent in python

closed-fixed
5
2012-05-30
2008-11-23
Altonator
No

In python if you type

if whatever:
print 'something'
print 'something with a colon:

It assumes that the second print statement should be indented as well, so when you type the : (electric key) it indents the line. This is really annoying!! I've had a look at the python.xml file in the 'modes' directory and played around with the regexes, but either I'm shit at regular expressions or the solution isn't that simple. If you remove the 'electrickeys' tag then it removes the problem but it ruins all other indentation cleverness. Nothing else seems to fix it. There's no 'indentThisLine' tag, so why does it indent it?

Weak!

Discussion

1 2 > >> (Page 1 of 2)
  • Altonator
    Altonator
    2008-11-23

    • summary: Colon causes unnecessary indent --> Colon causes unnecessary indent in python
     
  • Hi, what version of jEdit do you use ? I just tried with 4.3pre16 and I cannot reproduce (or maybe I did not understand exactly what you mean ?)

     
    • status: open --> pending-works-for-me
     
  • Altonator
    Altonator
    2008-11-23

    • status: pending-works-for-me --> open-works-for-me
     
  • Altonator
    Altonator
    2008-11-23

    Hi, sorry, should have put that in. I was using 4.3pre15, but I've just downloaded 4.3pre16 and the issue still occurs. Ah, what I've written wont make sense because Sourceforge has helpfully removed all of the spaces so you can't see which bits are indented!
    When you type out the example indent the second line (so it's part of the if clause), but don't indent the last one. When you type that last colon jEdit indents that line as well. Lame. (This actually makes it part of the if clause as python uses the indent rather than {} to determine the bounds of if/else clauses, so it not only looks wrong, but it breaks your code if you don't correct it!)

     
  • Alan Ezust
    Alan Ezust
    2008-11-25

    I am unable to reproduce your problem also.
    It seems to work fine for me. it indents the NEXT line if the line ends on an unquoted colon, which is the only time it is supposed to.
    I think (let's hope it works) you can put a
    [code]
    if something:
    print 'something with a : in quotes'
    next line:
    indented as expected;
    print 'something ending with a quoted colon:
    next line is not indented
    [/code]

    let's see if it works.

     
  • Altonator
    Altonator
    2008-11-25

    I've just tried your example code below... when I type out line 5, as soon as I put that : on the end it indents the line, so you end up with:
    [code]
    if something:
    print 'something with a : in quotes'
    next line:
    indented as expected;
    print 'something ending with a quoted colon:
    [/code]

    jEdit 4.3pre16 using Java 1.5.0_16 on Mac OS X 10.4.11.

    ezust, what version of jEdit are you using?

     
  • Altonator
    Altonator
    2008-11-25

    Have tested using the generic Java-based installer as well. Behaviour is the same.

     
  • Altonator
    Altonator
    2008-11-26

    A much simpler, and more likely way to trigger this bug is this...
    [code]
    if something:
    print 'pie'
    if bananas:
    [/code]

    When you type that last colon it indends the line. But it doesn't do it with...
    [code]
    if something:
    print 'pie'
    else:
    [/code]

     
  • Hi, automatic indentation in python is very difficult, we added a convention for that : if you add an empty line after the "if" block the next one will not be indented :

    [code]
    if something:
    print 'pie'
    if bananas
    [/code]

    When you type ":" will become

    [code]
    if something:
    print 'pie'
    if bananas:
    [/code]

    but if you have this

    [code]
    if something:
    print 'pie'

    if bananas
    [/code]

    typing the ":" will not indent your if bananas.
    It is the best compromise we found, if you have better ideas, let us know

     
    • status: open-works-for-me --> pending-works-for-me
     
  • Alan Ezust
    Alan Ezust
    2008-11-27

    I can only reproduce this if the previous line ends with :, *AND* I did a shift indent left
    on the line (outdenting) before I type :. In that case, I see it re-indents my line in one level from the previous line.

    Perhaps that rule kicks in under other circumstances, but I don't know enough about mode files to say more about it at this time. I will play around some more. But your test case is not sufficient to reproduce it. It seems to depend on

    1. what is on the previous line
    2. whether you did a shift+indent on the line you are editing at the moment.

     
  • This Tracker item was closed automatically by the system. It was
    previously set to a Pending status, and the original submitter
    did not respond within 14 days (the time period specified by
    the administrator of this Tracker).

     
    • status: pending-works-for-me --> closed-works-for-me
     
  • Altonator
    Altonator
    2008-12-27

    Thanks kpouer, that's really useful to know. I do think there's a better way though. Whether the modes xml system will allow it to be implemented I'm not sure, but I'll try to explain it...

    Currently, when you type:
    [code]
    if something:
    print 'pie'
    if bananas
    [/code]
    And then you type ":" on the end, it will become

    [code]
    if something:
    print 'pie'
    if bananas:
    [/code]

    So it's indenting the line when you type the colon(:). Is there any situation in python where you would want it to indent the current line when you type a colon? Ever?

    And so (assuming you answer 'no' to the above question), why not just remove the indentThisLine thing, or whatever it is that tells it to indent the line when you type a colon?

    When you type
    [code]
    if bananas:
    [/code]
    it doesn't indent the current line (that would be silly), no, it just indents the next line (useful). And if you type
    [code]
    if bananas:
    print 'pie'
    if cheese
    [/code]
    then it automatically indents those next two lines WHEN YOU HIT RETURN. There's no indenting of the current line going on. It's all done as you press return. So if you UN-indent the 'if cheese' line, there's no reason for it to re-indent it when you type a colon. No need! What use would that ever be? All the indenting should be done when you press return, once return is pressed the indenting should be left alone (in the case of if: type stuff), surely?

    I had a play with the python.xml mode, but didn't get far with it. Maybe you would be able to sort the dog out?
    Altonator

     
  • Altonator
    Altonator
    2008-12-27

    • status: closed-works-for-me --> open
     
  • Alan Ezust
    Alan Ezust
    2011-12-03

    • status: open --> pending-works-for-me
     
  • Alan Ezust
    Alan Ezust
    2011-12-03

    I can not reproduce any of your issues with jEdit 5.0pre1.
    Is this still an issue for you?

     
  • Rick R.
    Rick R.
    2012-01-07

    I can definitely reproduce it with jEdit 4.5pre1. In fact, it's not just a colon that breaks the indentation. jEdit reindents the second line when I type an open brace '{', a close brace '}', an open bracket '[', or a close bracket ']', in addition to a colon.

    I do a *lot* of python coding, and while I like jEdit for most things, I avoid it for python because of the auto-indentation issues.

     
  • Rick R.
    Rick R.
    2012-01-07

    Well, I found that if I comment out the following lines in <jEdit install dir>/modes/python.xml

    <PROPERTY NAME="indentOpenBrackets" VALUE="{[" />
    <PROPERTY NAME="indentCloseBrackets" VALUE="}]" />

    and

    <PROPERTY NAME="electricKeys" VALUE=":" />

    that my indentation issues appear to go away. Of course, I haven't spent much time with it, yet...

     
  • Seems like the original issue is fixed. I'm testing on jedit 4.5. If you wish to report a problem with braces indentation, please make it a new bug entry.

    To test this issue make sure you use the original python mode file.

    If however this is not fixed please make a new example, but use dots or underlines instead of spaces, because those are eaten by bug tracker software.

     
    • status: pending-works-for-me --> closed-fixed
     
  • Rick R.
    Rick R.
    2012-04-18

    I don't see that this is fixed at all. If I type

    if cond1:
    ....stmt

    and press Enter, my cursor ends up under the 's' in stmt, as expected. But if I unindent so that my cursor is under the i in 'if', as in

    if cond1:
    ....stmt
    | (the '|' character represents the cursor)

    then type

    if cond2:

    as soon as I type the colon, I end up with

    if cond1:
    ....stmt
    ....if cond2:

    which, in this case, is not what I intended. In fact, the exact same thing happens with braces and brackets, which I mentioned earlier. I can't think of a case where I'd ever want any character to change the indentation of the line on which I'm currently typing. Subsequent lines, sure, but not the current line. That's essentially the same thing that Alternator said below on 2008-12-27 10:22:34 PST.

    I tested on jEdit 4.5.1 with the distributed python mode file.

     
  • Alan Ezust
    Alan Ezust
    2012-04-18

    • status: closed-fixed --> open
     
  • electricKeys are responsible for the unwanted behaviour. Sorry for the mistake I made in previous tests, because I prefer to work on hacked jedit with electricKeys off. See
    https://sourceforge.net/tracker/?func=detail&atid=350588&aid=3514076&group_id=588

    However there is a case in which you want them to work. It's for unindentThisLine section in python.xml. Thanks to the electric keys you may type:

    if asdfas:
    ....something
    else:
    ....uiwuerw

    And this gets automatically indented. So it's not obvious in python's case to remove electricKeys absolutely. What do you suggest, Rick?

     
1 2 > >> (Page 1 of 2)