Best Eclipse Code Formatters?

92.6k views Asked by At

The default Eclipse formatter formats my Java code in a really funny way.

For example: hello.show().x().y() would oddly be formatted as x() and .y(), and be placed on a separate line.

Are there any other basic formatters that can do a better job?

Examples and links are very welcome.

4

There are 4 answers

2
nanda On BEST ANSWER

I always change the formatter in Eclipse to Java Convention AND change the tab policy to always use space instead of tab or mixing of tab and space. Sometime I change also the line width to be 100 or 120 (80 characters is just too narrow in my monitor).

Regarding your specific request: yes you can change this behavior. Go to the Formatter preference and create your own profile. Modify it on tab Line Wrapping and uncheck Prefer wrapping outer expressions (keep nested expression on one line).

More information on: https://bugs.eclipse.org/bugs/show_bug.cgi?id=313524

6
Anupam Saini On

I would highly recommend the google's style guide, it's a collection of coding style guide for commonly used programming languages.

The eclipse configurable Java guide can be found here. Installing the guide is quite simple, Save the style xml to a file and import it using the following dialogue box:

Window -> Preferences -> Java -> Code Style -> Formatter

Finally select the active profile as GoogleStyle.

1
Thorbjørn Ravn Andersen On

Have you considered looking at the preferences for the formatter? You can basically configure it to do anything you want.

I would, however, strongly consider the standard Eclipse formatting. It helps a lot to just use the standard, and then adjust your coding style to work well with that.

0
Vineet Reynolds On

In addition to the ability to customize the formatter, as indicated by Thorbjørn, it is also possible to import coding conventions from specific projects, especially the ones from frameworks and libraries that you are working on.

For instance, the Spring framework coding conventions have been made available, as is the case with Android. Eventually, you're better off starting with the Sun coding conventions (than a completely hand-crafted one), with a few tweaks done to suit your organization's coding style and readability requirements.