Eclipse Autoformat For Mac

Eclipse has a wonderful feature that formats according to your coding standards. It handles things like indentation, where curly braces are placed, if blank lines should occur between field declaration and hundreds of other options.

However, to invoke this formatting, you have to tell Eclipse to do this every time you’ve edited the code. You can do this using Ctrl+Shift+F but (1) you have to do that every time and (2) you have to remember to do it in the first place (and we all know how good developers are at remembering things).

Introducing Eclipse 3: a lean, mean, 100 percent PowerPC native screen saving machine that works great on 68K Macs as well. To eliminate the problem of.

Eclipse sports a feature called Save Actions that makes formatting code easy. With this feature enabled, every time you save the file, Eclipse formats the code to your formatting preferences and does some cleanup of the code (eg. remove unused imports). All done automatically and for free.

How to enable automatic formatting and cleanup

By default automatic formatting and cleanup is disabled. To enable it, do the following:

  1. Go to Window > Preferences > Java > Editor > Save Actions.
  2. Select Perform the selected actions on save.
  3. Select Format Source code. Make sure Format all lines is selected. Below I discuss when to use the other option.
  4. Make sure Organize imports is selected.
  5. Select Additional actions.
  6. Click Ok, edit some code, save it and watch Eclipse format it automatically.

Here’s what the settings should look like.

How does Eclipse “clean up” the code

With “Format source code” enabled, the code is formatted according to the formatting rules you’ve specified under Java > Code Style > Formatter (or project-specific formatting if enabled).

With “Organize imports” enabled, Eclipse will automatically reorder imports, prompt for imports to unresolved classes and remove unused imports. It basically does a Ctrl+Shift+O whenever you save.

When you enabled the option “Additional actions”, you told Eclipse to do more cleanups. You can see what they are and what you want done by clicking Configure on the same preference page. Make sure that Additional actions is ticked.

Eclipse can clean up a lot of things, but here’s a list of some of the more useful ones I have enabled:

  • Add @Override and @Deprecated tags: Nice for compile-time checking. Under the Missing Code tab.
  • Use final modifier where possible: A nice thing if you want final variables to be marked as final automatically. Can get annoying sometimes, but mostly worth the trouble. Under the Code Style tab.
  • Convert for loops to enhanced: Replaces an index-based loop with an enhanced for loop. It just reads better. Under the Code Style tab.
  • Use blocks in if/while/for/do statements: Forget religious wars, I’ve programmed long enough to know that you always use braces even for single line blocks. Eclipse can do this automatically. Under the Code Style tab.
  • Remove trailing whitespace: Nice one if you’re obsessive about those blanks at the end of lines. Under the Code Organizing tab.

Why should I enable Save Actions?

Many reasons. You don’t have to remember to ask Eclipse to organise imports or format code, ie. more chance of standardised code in your team. You save time by not having to invoke these actions as well, ie. individual developer time saved.

Your version history comparisons become a lot easier as well because any developer working on the code would have done the same formatting and cleanup.

But it could mess up SCM/VCS diffs with earlier versions of the class

Yes, you could lose diff-ability with earlier versions of the class because Eclipse might reformat the whole file, especially if it’s not in your current coding standards. But I would rather take the hit the first time and reformat a class to the team standards rather than have it in an inconsistent format.

However, to address this complaint, there is also an option to only format/clean up only the parts of the code that you’ve recently worked on. On the same page you enabled the Save Actions, select Format edited lines instead of Format all lines. This will ensure that the new code conforms to the standards but the older code still stays relatively comparable from earlier versions.

Related Tips

Share This Tip

Documentation Survey Submit Documentation FeedbackView Latest Version and Get PDF Online

PDT can auto-format your code according to set standards in order to make it easily navigable and readable.

Your code will be automatically formatted according to the settings defined in the PHP Formatter Preferences page, accessed from Window Preferences PHP Formatter.

This procedure demonstrates how to format your scripts.

To format your whole script:

  1. Open the required file.

  2. Go to Source Format Document or press Ctrl+Shift+F.

Your code will be automatically formatted.

Eclipse

Example:

To format only selected lines within the script:

  1. Select the relevant lines.

  2. Go to Source Format Active Elements -or- press Ctrl+I.

Only the selected lines will be formatted.

Note:

Mac

Code Formatting will also be available in JavaScript editors.
JavaScript Formatting preferences can be configured from Window Preferences Web JavaScript Code Style Formatter.

Related Links: