44

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.

msrd0
  • 7,816
  • 9
  • 47
  • 82
Sheehan Alam
  • 60,111
  • 124
  • 355
  • 556

4 Answers4

78

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.

Community
  • 1
  • 1
Anupam Saini
  • 2,431
  • 2
  • 23
  • 30
  • 6
    This throws an error on import now with Eclipse Mars. Specific details: `Problems reading profiles from XML Attribute name "data-pjax-transient" associated with an element type "meta" must be followed by the ' = ' character.` – fIwJlxSzApHEZIl Sep 28 '15 at 08:40
  • 2
    https://github.com/google/google-java-format try this solution instead – vikramvi Jul 21 '16 at 13:49
  • 2
    @anon58192932 The "data-pjax-transient" error occurs if you download the file from the github webpage (e.g. save as). The content is then HTML and **NOT** the XML file! If you do a real download of the raw file it works... – Lonzak Jan 17 '17 at 14:36
  • Don't use this it doesn't even format HTML – Philip Rego Dec 16 '17 at 07:26
  • Very good formatter for java and eclipse – JRichardsz Oct 13 '19 at 18:08
  • Version 1.6 of the google-java-format Eclipse plugin can be downloaded from the releases page. It can be drop into the Eclipse drop-ins folder to activate the plugin. { Ref.: https://github.com/google/google-java-format } – bitfox Jul 29 '20 at 08:10
30

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

nanda
  • 24,458
  • 13
  • 71
  • 90
  • Only problem with wide lines is when you ask Eclipse to print them... – Thorbjørn Ravn Andersen Aug 02 '11 at 20:22
  • Old thread but will still reply. Printing is a problem. I agree. I am finalizing a formatter here for the team. I am in favour of Java Convention with spaces only, block and line comment formatting turned off, and 120 lines. We as programmers need to move away from the 80 line convention. In these days where wide LCDs are very common in software development firms, we must start moving to 120 line convention and lessen the pain on our eyes. – RuntimeException Mar 25 '14 at 14:52
9

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.

Dave Jarvis
  • 30,436
  • 41
  • 178
  • 315
Vineet Reynolds
  • 76,006
  • 17
  • 150
  • 174
6

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.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347