Community
Participate
Working Groups
A good idea from the eclipse.tools newsgroup: "I've just started working in a project that requires all files to have spaces rather than any tabs. I personally agree that this is a good thing. With my previous editor (a plain Java-aware text editor) I didn't need to do anything fancy for this, as I could set it to do everything using spaces anyway, and pressing "tab" just put the cursor to the "right place" on the line rather than inserting a normal tab character. I'm okay with tabbing to the right place on the line using Eclipse, but I'd love it if (either at save time or key entry time) it could convert the tabs into spaces. Is there any way to do this? I know that the code formatter is capable of using spaces rather than tabs for indentation, but it doesn't seem to apply that to "hand-entered" tabs. Jon Skeet - <skeet@pobox.com> " We should support entering "tabs as spaces" and allow the user to specify the number of spaces to use as well. NOTES:
PRODUCT VERSION: 136
As well as entering spaces instead of tabs, the auto-indenter should insert spaces instead of tabs. Currently, the only easy way to not have tabs in your code is to run it through the formatter afterwards.
Does the underlying SWT widget support the requested behavior?
We do not directly support this; however, one could do it via the ExtendedModifyEvent. My initial thought was that one could set the key binding for tab and insert spaces, but currently TAB is not defined as a key that can be customized. So we could provide a new key binding for tab. This would be more straightforward for the application to handle (vs. ExtendedModifyEvent). Or we could introduce new StyledText API and handle it.
The easiest solution would be to insert spaces whenever tab is pressed by implementing a VerifyListener. I assume that the user also wants existing tabs replaced with spaces when the file is opened.
Knut is right, you can use the VerifyEvent to do this quite painlessly. Just replace the tab character with spaces in the VerifyEvent text field.
No action. Use KR's suggested workaround.
As a plain user (rather than anyone developing a plug-in) this sounds like it's not actually a resolution - if it can be done in straight Eclipse without me adding/changing any code, could you point to exactly where it needs to be done? If it *does* involve adding code, would it be possible for me to write a plug- in module that allows this within the JDT (or general editors?) easily, and which could be distributed quite simply? Obviously I'd rather just use an existing configuration option if it's there, but I'm willing to get my hands dirty a *bit* :)
I agree completely with Jon Skeet. This doesn't sound simple to do. Frankly, I don't know how to do it. If it is so simple to workaround, why not fix it in the code so regular people can benefit from it once and for all? The Preferences/Java/Editor should the contain a check box saying something like "Use spaces instead of tabs". The tab width field already present in the 2.0 stream would then tell how many spaces should be inserted instead of each tab. There is regularly people in the newsgroup (approximately twice a week) complaining about this issue so I think it is worth fixing.
This PR should go back to Kevin or to JDT (Kai). The original problem was that, when pressing tab, a tab character is inserted instead of spaces. SWT supports this but JDT has to do it, although I believe this is a general editor problem/feature and not Java specific. Bug 6552 is related (http://bugs.eclipse.org/bugs/show_bug.cgi?id=6552). Basically the Java editor doesn't use the settings of the Java formatter. The Java formatter will convert tabs but the editor shouldn't put them in the code in the first place. Can we add a general editor preference for this that the Java editor honors as well (for auto-indent)?
The CC list seems to be long enough to have this issue resolved for 2.0
Orthogonal to this behaviour for Tab, making <Delete>/<Backspace> behave as they do when a literal Tab is inserted would be optimal: delete <tabsize> spaces when current column is a multiple of <tabsize>. The 3plusfour plugin for Eclipse already implements both <Tab> and <Delete>/<Backspace> behaviour: http://www.3plus4software.de/eclipse/.
Note that the 3plus4 patch is fantastic in all but one respect - it still inserts tabs when you're in a JavaDoc comment. (Must mail the authors about that some time...) Jon
Part of 2.0
This feature should be implemented for ALL text editors, not just Java Editor.