Eclipse can automatically format a file upon save. One use for this is to insure everyone's code meets a common corporate standard. Let's say "hypothetically" this standard is dumb. Is it possible to have a second set of formatting rules that get automatically applied on file open, so you can work sanely?
The short answer is no, you cannot specify one set of formatting rules when opening a file, and another set of formatting rules when saving a file because Eclipse does not provide the functionality to reformat a file using different formatting rules on open. But even if that were true, you would still have problems:
You open your file and Eclipse automatically uses your "good" formatter.
You work on the file and save it because you are going to lunch (or Eclipse autosaves while you are on the phone), and the "dumb" formatter is automatically used.
You would now have to close the file and reopen it to resume using the "good" formatter.
However, an alternative approach that would achieve what you want with very little effort is:
Create your own profile with your "good" formatting rules (e.g. For Java use Window -> Preferences -> Java -> Code Style -> Formatter -> New...).
Make that the active profile for the project you are working on (e.g. For Java use Window -> Preferences -> Java -> Code Style -> Formatter -> Configure Project Specific Settings...).
Make your changes and test locally using the "good" formatter, but immediately prior to committing any changes to source control switch to using the "dumb" formatting rules.
Of course that approach is potentially disastrous if you fail to revert a modified file to use the "dumb" formatter; a one line change in the source could result in thousands of differences within source control due to reformatting.
Perhaps I'm being unfair, but your question sets off flashing red warning lights in my head. Is it simply that you don't like the formatting rules that have been laid down? If not, what would be a scenario where the hypothetical approach you describe would make good sense?
Related
I am using Eclipse 2019. It is very slow on Save Action. It is necessary for me to enable the Save Action to do code format and indent check. But it is really a pain to watch Eclipse busy and slow at that point.
Is there a way to improve this?
I am trying Intellij. It is much much better than Eclipse on the Save Action.
I really hope Eclipse has the same or similar performance.
EDIT:
I am using Eclipse IDE for Enterprise Java Developers, Version 2019-12.
I only installed two editor plugins: One for resource bundle file and one for bash file.
EDIT 2:
Sorry, I should say that the Save Action on big Java file is slow. We have many files are more than 2 thousand lines. Some of them are double, triple that size, or even more. We often see the dialog telling us that we can turn off the Save Action to shorten waiting time. I tested with Intellij. The same file, the same formatter and Save Action, there is no noticeable delay in Intellij but Eclipse needs more time (noticeable even that dialog does not show up) to finish the action. There is nothing to do with the change itself, change many places, or change one line is the same. My guess is that Intelliji may be tracing the changes during the process and do format and Save Action only on where is changed. But Eclipse may not tracing the changes during the process and do format and Save Action on the entire file at the end.
I have no source control and other things attached.
Were you saving with Ctrl-S?
I was having a similar (or the same problem) whenever I was Ctrl-C/V to copy/paste. It seems the underlying problem may have been that my Hyperlink key was "Ctrl", so that whenever I was about to Copy/Paste, it was trying to work out Hyperlink options - stalling the editor.
Changing the Hyperlink modifier key (General > Editors > Text Editors > Hyperlinking) to Alt and the slow copy/paste is no more.
Even though I should have the same setup as the rest of my team, Eclipse's formatter has one small anomaly in my setup.
You can see the difference in the attached picture.
On the left is my formatting.
On the right is the rest of the team's formatting.
I want to have the same formatting as the rest of the team.
The difference arises when there is an empty line after a chain of method calls.
Question: How do I achieve the formatting on the right?
I expect you are using the same formatter definition and have already tried to export the formatter on one of your team members machine and import it on yours.
Another thing you should check are the Save Actions in the preferences (Java -> Editor -> Save Actions). Maybe the settings for removing whitespaces differs here.
Some context first: I'm in charge of purifying the (abominable) source code for an old, huge Java application. Eclipse's code style cleanup tool does a good bit of work, upon which I can then rewrite the extant source code into something less awful, but I keep running into an annoyance: I have the "organize imports" save action turned ON (see Preferences → Java → Editor → Save Actions → Organize imports), but I would rather be able to turn it OFF while I am working on a particular file, and turn it back ON when I'm done with it, to ensure the file gets fully cleaned up.
Routinely turning the mentioned preference on or off on command through the standard dialog windows is less than straightforward (and one can get lost as to its current state as well). It would be much more comfortable to define a custom button in the IDE that I could simply click to turn this particular preference on and off, and know its current state just with a quick glance... but I'm at a loss on how to program such a button.
I love the Eclipse feature that allows the Formatter Profile to be applied on save, but what if my preferred bracing style differs from the project "standard"? Is there a way to apply one Formatter Profile when I open a file, and another when I save it?
I suppose there would be complications when debugging, but maybe there's some brilliant Eclipse Plugin developer that has solved this problem. I can hope, right?
Not to my knowledge for Java sources.
Especially since, at the first saved modification, the bracing style would then revert to the second Formatter profile, hence modifying the reading experience.
Plus that would required a mandatory recompilation on project/source opening, for the debug informations to be correctly stored.
The only kind of formatting I know about are for non-compiled files, like xml ones.
OxygenXml has such a setting in place, with PrettyPrint.
Format and indent the document on open
When checked, the Format and indent the document on open operation will format and indent the document before opening it in the editor panel.
In Eclipse, under Windows -> Preference -> Java -> Code Style, you can define code templates for comments and code, and you can setup a code formatter.
I'm wondering if it is possible in Eclipse to have these setting take affect every time I save a source file. Basically, instead of me highlighting everything and pressing Ctrl+Shift+F, I want Eclipse to be responsible for making sure my code is formatted properly.
Is this possible and how do you set it up?
Note On Using the Auto-Format: It's probably best to choose "Format Edited Lines" as merging changes becomes very difficult when you reformat a whole file that is in source control already that was not formatted properly. Already did this to a co-worker.
Under Preferences, choose Java --> Editor --> Save Actions. Check the Perform the selected actions on save, and check the Format source code box.
This may or may not be available in previous versions of Eclipse. I know it works in:
Version: 3.3.3.r33x_r20080129-_19UEl7Ezk_gXF1kouft<br>
Build id: M20080221-1800
I have a snapshot of the setting.
Please follow the path:
In the Preferences window, choose Java --> Editor --> Save Actions.
Check the Perform the selected actions on save, and check the Format source code box.
I strongly recommend checking your eclipse format xml descriptor into source control. That way all members of the team can use it and you don't get to and fro reformatting battles.
If you find that you do not have a Save Actions preference under Java--> Editor, it may be because you are using an older version of Eclipse. In that case you can install the Format on save plugin from here.
Then, under Preferences, choose Java --> Format on save. Select the Run Format option under Select a code formatting action