Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Eclipse Code Formatters? [closed]

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.

like image 594
Sheehan Alam Avatar asked Sep 20 '10 18:09

Sheehan Alam


People also ask

How can I get beautify code in Eclipse?

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

How do I fix indentation in Eclipse?

You can use Ctrl + A to select the entire file, then use this shortcut to quickly fix all the indentation. You can also change how Eclipse handles indentation by going to Window > Preferences, then in the left panel, navigate to Java > Code Style > Formatter > Edit... > Indentation.


2 Answers

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.

like image 93
Anupam Saini Avatar answered Oct 14 '22 08:10

Anupam Saini


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

like image 40
nanda Avatar answered Oct 14 '22 08:10

nanda