views:

4806

answers:

7

Hello,

The question has two parts, one of which I already have the answer for.

  1. How to auto-remove trailing whitespace from the entire file being edited? -> Answer: use the AnyEdit plugin, which can be set to do that on any save to the file.
  2. How to auto-remove trailing whitespace only from the lines I changed? -> This I don't know and would appreciate any help.

Thanks, Jan

+4  A: 

I would say AnyEdit too. It does not provide this specific functionalities. However, if you and your team use the AnyEdit features at each save actions, then when you open a file, it must not have any trailing whitespace.

So, if you modify this file, and if you add new trailing spaces, then during the save operation, AnyEdit will remove only these new spaces, as they are the only trailing spaces in this file.

If, for some reasons, you need to keep the trailing spaces on the lines that were not modified by you, then I have no answer for you, and I am not sure this kind of feature exists in any Eclipse plugin...

romaintaz
Yes, I'd like to keep the spaces on the lines not modified by me.. This is the default behavior of Intellij Idea (remove spaces on my lines, keep them on others), and I'd like to get that in Eclipse.Thanks for the answer.
Jan Zankowski
+1  A: 

I am not aware of any solution for the second part of your question. The reason is that it is not clear how to define I changed. Changed when? Just between 2 saves or between commits... Basically - forget it.

I assume you would like to stick to some guideline, but do not touch the rest of the code. But the guideline should be used overall, and not for bites and pieces. So my suggestion is - change all the code to the guideline: it is once-off operation, but make sure that all your developers have the same plugin (AnyEdit) with the same settings for the project.

van
Since it is a save action it would be the lines changed between saves. Eclipse already keeps track of these lines for marking in the side bar.
starblue
+22  A: 

I assume your questions is with regards to Java code. If that's the case, you don't actually need any extra plugins to accomplish 1). You can just go to Preferences -> Java -> Editor -> Save Actions and configure it to remove trailing whitespace.

By the sounds of it you also want to make this a team-wide setting, right? To make life easier and avoid having to remember setting it up every time you have a new workspace you can set the save action as a project specific preference that gets stored into your SCM along with the code.

In order to do that right-click on your project and go to Properties -> Java Editor -> Save Actions. From there you can enable project specific settings and configure it to remove trailing whitespace (among other useful things).

gcastro
Thanks, gcastro - it is easier than AnyEdit. Unfortunately, it's for the entire file being edited .. still don't know how to do it for only the lines I changed - looks like it's probably impossible right now.
Jan Zankowski
Good for Java; good for C/C++ too; but not for Python in PyDev.
Craig McQueen
Craig McQueen
+2  A: 

You can map a key in Eclipse to manually remove trailing whitespaces in the whole file, but only on request instead of automatically at save. (Preference/Keys and then map a set of keys to File/Remove Trailing Whitespace) This can be useful if you want to sanitize all new files, but keep legacy code untouched.

Another strategy is to activate visual display of whitespace, so at least you'll know when you're adding some trailing whitespace. As far as I know, there's no way to display only trailing whitespace though, but I'll be glad to be proved wrong.

Kena
Thank you very much for this answer! So far it's the only way that I have found for Aptana plugin.
Igor Zinov'yev
+3  A: 

In Eclipse 3.5+ you have the option of reformatting only the edited lines of code on save: Preferences -> Java -> Editor -> Save Actions -> Format edited lines.

You can define a loose format that only removes whitespace but it's probably not worth the bother.

Dan Berindei
What about other editors - PHP, Python, Javascript?
techtonik
+1  A: 

It is impossible to do it in Eclipse in generic way right now, but it can be changed given with basic Java knowledge and some free time to add basic support for this https://bugs.eclipse.org/bugs/show_bug.cgi?id=180349

The dependent issue: https://bugs.eclipse.org/bugs/show_bug.cgi?id=311173

techtonik
+2  A: 

PyDev can do it by either Ctrl+Shift+F if you have code formatter option set to do it, or by during saving:

Eclipse -> Window -> Preferences -> PyDev -> Editor -> Code Style -> Code Formatter:

I use at least these:

  • Auto format before saving
  • Right trim lines?
  • Add new line at end of file
Ciantic