X-1.0rc3: IDE: Indentation issues with scripts not conforming with current tab settings when editing

Asked by Parva Thakkar

**** problem description / workaround

-indentation tabs are not handled at the time a script is loaded into the IDE, only when editing a script in the IDE. Or more generally: when loading a script, indentation is not normalized according to the current settings in the IDE's preferences.

- So you might come up, with a situation, that a loaded script does not conform to the current tab settings and things get messed up, when you start editing.

- A workaround would be, to change all tabs to the required spaces in a capable editor directly in the .py files with search-and-replace before loading the script into the IDE

----------------------------------------------------------------------

I just upgraded to latest version of Sikuli to x-1.0rc3(r905) and have issue with all the scripts written with previous version, which was X-1.0rc2 IDE.

And I am sure that this has been asked and resolved previously as this new version is here for more than a month.

Now, here is the problem -

With previous IDE, if I write "IF statement" like this -
if (x == 1):
and I press enter then, cursor would go to the next line with indentation. like -
if (x == 1):
    |
and if you see at the lower right corner of IDE, you would read something like - Line: 2; Column: 2
which essentially tells you that you are at line number two and column is also 2. That means that indentation would count as one column and you start writing from second column.

Now, do the same thing with new IDE. Write the same if statement and press enter & you would see exactly same thing -
if (x == 1):
    |
But the only issue is when you see lower right corner of IDE. Now you see - Line: 2; Column: 5
Saw the difference in column number? Its because, this IDE considers one tab or one indentation as 4 spaces or 4 column to be specific. And you start writing from fifth column.

Now, this makes no difference if you are writing a new script in this new IDE. But when you open any script which was written in previous IDE, you encounter problem. Let me give you same example. You have script like below written in old IDE -
if (x == 1):
    print x

and you open this script in new IDE. Now, you realize that you want a new statement inside "if statement". so, you move your cursor to end of the "if statement", that is at "if (x == 1): <---". And then you hit enter. Cursor still comes at the correct place visually with indentation and you write a new statement there -
if (x == 1):
    popup something
    print x

and you expect the script to work because it has proper indentation and you click big right arrow on top of the IDE. BUT script doesn't run. WHY? Because first statement inside "IF" is at column 5 and second statement inside "IF" is at column 2!!

Is it a known bug? or a bug at all and it is how it should be working?

Thanks,
Parva Thakkar

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

goto Preferences

and change the automatic "tab to spaces" value to your needs.

The new value of 4 spaces is according to the Python coding guidelines PEP 808.
It is recommended to only use tab/shift-tab (besides auto indentation). Do not use spaces or act on indent spaces directly.

Revision history for this message
Parva Thakkar (parva-email) said :
#2

I apologize, I probably couldn't explain the problem thoroughly.

Yes, I understand it and 'tab to spaces' of 4 spaces is perfectly okay. But the issue is even with tab/shift-tab or preference set, your column number remains 5 as opposed to 2 in prior version. If you change 'tab to spaces' to 1, then visual indentation looks bad like -

if (x == 1):
 abc
    xyz

even though they look bad visually, they have the same column number and thus perfectly okay indented for Python.
line 'abc' is with newer IDE
line 'xyz' is with older IDE

So, any script written in prior IDE with condition is very difficult to edit. I understand this is something which cannot be solved. But, I recommend adding this in some FAQ page for the newer IDE.

Revision history for this message
RaiMan (raimund-hocke) said :
#3

No, your problem was explained well.

I think my answer was not good enough.

In the preferences, you should set the tab width to 2, when editing existing scripts, so it is compatible with your "old scripts".

As far as I know, in the next version (may be already earlier in the "latest builds") there will be a "reformatting" service, that makes all the indents in a script equal.

Revision history for this message
Parva Thakkar (parva-email) said :
#4

Well, actually I made a video out of my problem and before I upload onto youtube and post it here, you posted a second answer.

Yes, now if I set tab width to 2 as you said, the visual indentation looks completely wrong. I have explained it in the video I described.

It's here - http://www.youtube.com/watch?v=ZhBINSiw3rM

When tab width is set to 2, the script looks like -

if (x == 1):
 xyz
    abc

that looks completely odd but works fine.

I'll wait for you to one more reply after you watch video, and see what your thoughts are. And then I would close this thread immediately. We don't want some issue to be open if that already has been taken care in next version as you described.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

Another example of: pictures are telling more than 1000 words ;-)

Do you have an editor at hand, that can show invisible characters like space, tab, newline with special symbols?

If yes, open <script-name>.py file (contained in the folder <script-name>.sikuli) with this editor. make the invisible characters visible.

My guess: in your rc2-scripts you will see real tabs as indentation. This is the only explanation for these symptom. In the IDE the real tab counts as one character, but advances in the editor pane to four spaces. The conversion of a tab to 4 spaces is currently only done when typing in the IDE - not when the script is loaded.

If you do not have an editor at hand, just send me the zipped .sikuli folder from the video to my mail at https://launchpad.net/~raimund-hocke. I will check.

A workaround would be, to change all tabs to 4 spaces in such a capable editor directly in the .py files with search-and-replace.

If I am right, then this bug should stay alive and I would make it ready for the developers.

Revision history for this message
Parva Thakkar (parva-email) said :
#6

Okay, so in spirit of this Chinese proverb, here is the reply video -
http://www.youtube.com/watch?v=Zi1x6usllII

Is this what you were trying to tell me?

Your observation, "The conversion of a tab to 4 spaces is currently only done when typing in the IDE - not when the script is loaded" is absolutely correct.

Revision history for this message
Best RaiMan (raimund-hocke) said :
#7

Thanks for confirming.

--- So finally we have this:

-indentation tabs are not handled at the time a script is loaded, only when editing a script in the IDE. Or more generally: when loading a script, indentation is not normalized according to the current settings in the IDE's preferences.

- So you might come up, with a situation like yours, that a loaded script does not conform to the current tab settings and things get messed up, when you start editing.

- A workaround would be, to change all tabs to the required spaces in a capable editor directly in the .py files with search-and-replace.

--- comment on your second video
When using Python language in any editor, that handles tabs, to show some defined indentation in the editor window, you should always stick to one of these 2 method2:
- use tabs as is (real tabs)
- let the editor automatically change a tab to e.g. 4 spaces

The second version is the one to prefer, since you will never have any problems in any other editor.

For a Python interpreter is only relevant, what is really in a source file, not what you can see in the editor window. So take care, that you know, what will be in the source file, if you do something with indentation in the editor.

I will turn this question into a request bug accordingly.

Revision history for this message
Parva Thakkar (parva-email) said :
#8

Thanks RaiMan, that solved my question.