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
Related
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?
Due to a bug in Eclipse, every time I try to change the Code Formatting preferences, Eclipse crashes. Thus, I cannot edit code formatting preferences via Eclipse's built in window editor. These are workspace specific preferences. Where can I find the file to edit the preferences manually?
It looks like the code format profiles are stored in the workspace .metadata/.plugins/org.eclipse.core.runtime/.settings/org.eclipse.jdt.ui.prefs file
This is a Java properties format file. The code formatter settings are in the org.eclipse.jdt.ui.formatterprofiles property. The value is an XML object. This is going to be hard to edit!
The Eclipse content assist for PHP (and I'm assuming for other types as well) isn't using the currently active working set - it's searching ALL files in the project. How do I change it to only use the active working set?
When doing a Ctrl+Shift+R search, it was also looking for all files in the project (not what I wanted), until I clicked the small black triangle in that window and selected the working set I wanted it to use. Now it successfully only shows the files that match my search string in the working set. However, this doesn't change anything in the autocomplete content assist feature (i.e. doing a Ctrl+Space to complete the name of a function or variable). Is there any way to accomplish this?
I'm using Eclipse Indigo release, so it's very recent.
I'm afraid it's not possible the way you put it. Eclipse Working Sets just give you a way to organize your projects.
However you may want to take a look at Mylyn, which is fully integrated with Eclipse platform. Here and here is a nice overview of what you can do with Mylyn. But briefly, what it allows you to do is to create a task and maintain a context associated with it. This way, only the relevant files will be displayed in Project Explorer and autocomplete would suggest you only task-focused options.
Can some one suggest me a plugin to format HTML and JAVA code with proper indents etc ? Is there any built in option there in Eclipse ?
Use Source -> Format or CTRL+SHIFT+F
Eclipse does have built in Java formatting:
Preferences > Java > Code Style > Formatter
For HTML, you might try something like HTML Tidy. The Web Tools Platform project also includes formatting.
It's a combination of all the answers above, which suits me-
Go to Preferences > Java > Code Style > Formatter and set all the options you want by creating your own profile or using a default one. Personally I find the line-wrap limited to 80 a little too low. I set it to 200.
Save the 'custom' preferences by giving it a new profile name (export it for future restoration)
Select the code in your editor - then hit CTRL+SHIFT+F. That should format your code.
Worked wonders for me everytime.
Java code formatting is built in. Go to Window->Preferences->Java->Code Style->Formatter. There's a built in profile set up and you can make your own.
http://sourceforge.net/projects/jalopy/
Note that Jalopy has its settings under Window> Jalopy Preferences.
(This menu option appears only when all editor tabs are closed)
Eclipse doesn't have proper built-in code formatting. You might want to open your project in Idea IntelliJ or Netbeans and use their formatting to do a thorough job on it.
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.