Search code examples
eclipsewhitespacepydevspaces

How to keep spaces from becoming tabs in Eclipse (w/ PyDev)


I have an unfortunate problem with Eclipse that only happens in existing Python files with tabs somewhere in them. It doesn't happen in empty files or non .py files.

Basically, if I try to copy and then past more than 3 spaces I end up with tabs for every X spaces, X being the number of characters per space setup for the editor. In my case this is 4, so I end up with the following:

Copy and paste 1 space:       " "         (one space)
Copy and paste 3 spaces:      "   "       (three spaces)
Copy and paste 6 spaces:      "\t  "      (one tab, two spaces)
Copy and paste 9 spaces:      "\t\t "     (two tabs, one space)
Copy and paste 12 spaces:     "\t\t\t"    (three tabs)

For the life of me I can't figure out why this is happening or how to turn it off. It's really frustrating to have to keep manually editing each line I paste instead of getting exactly what I copied in the first place.

Does anyone know what setting this is, or if it's just a bug?

EDIT: To be clear I do not want spaces when I hit the tab key, I want the tab characters when I ask for the tab characters. However when I want to paste a space I do not want the characters changed, especially if it's clear I'm between single or double quotes and just trying to write text as is.


Solution

  • This happens because at:

    org.python.pydev.editor.autoedit.AbstractIndentPrefs.convertToStd(IDocument, DocumentCommand) it'll do it both ways (either make spaces -> tabs or tabs -> spaces), so, there isn't really an option to let it as it was.

    i.e.: https://github.com/fabioz/Pydev/blob/development/plugins/org.python.pydev/src/org/python/pydev/editor/autoedit/AbstractIndentPrefs.java

    code:

    private String convertSpacesToTabs(IDocument document, String text, int offset, String indentString)
            throws BadLocationException {
        String spaceStr = StringUtils.createSpaceString(getTabWidth());
        while (text.startsWith(spaceStr)) {
            text = text.replaceAll(spaceStr, "\t");
        }
        return text;
    }
    

    There are 2 possible fixes here:

    1. Create an option so that no conversion is ever done.
    2. Improve the convertSpacesToTabs so that it guesses the indent better instead of using the tab-width (that way the conversion would do things better).

    As PyDev is open source, ideally someone could provide a patch for that (see http://pydev.org/developers.html for getting the code).

    Otherwise, you can create a ticket at https://sw-brainwy.rhcloud.com/tracker/PyDev ;)