Skip to content

Latest commit

 

History

History
49 lines (34 loc) · 6.48 KB

Eclipse.adoc

File metadata and controls

49 lines (34 loc) · 6.48 KB

Eclipse

Here is how to configure Eclipse so that it will format your code according to the Google Java Style Guide.

Format code using the Google Java Style Guide

  1. Configure the Eclipse formatter: use method 1 or method 2, below

  2. Apply the newly configured formatter globally or to a specific project: see Vogella’s tutorial

Method 1: Configure Eclipse formatter and tweak Eclipse manually

Download eclipse-java-google-style patched.xml. Go to Eclipse preferences, Java / Code Style / Formatter, and import that file.

Back to Eclipse preferences, at Java / Code Style / Organize Imports, remove all groups, so that Eclipse will not insert unwanted blank lines within your import blocks.

The Eclipse formatter configuration file provided here is a modified version of Google’s configuration file for Eclipse.

Explanation

I initially downloaded the configuration file provided in Google’s styleguide repository (this version, to be precise), imported it into Eclipse and exported it again so as to adapt its format to recent Eclipse versions, sorted the settings in it (to ease comparisons), then modified the file manually.

  • Google Java Style Guide, 3.3.3 Ordering and spacing, requires that static imports come together first in a single block, then a blank line, then non-static imports in a single block. But the related formatter settings (blank_lines_between_import_groups, corresponding to Blank Lines / Blank lines within compilation unit / Between import groups in Eclipse formatter configuration GUI) was incorrect in the original eclipse-java-google-style.xml file, so I changed it.

    • Also, the Checkstyle configuration file for Google Style Guide (original or mine) requires separator between static and non-static imports (rule CustomImportOrder, property separateLineBetweenGroups), so it is required to correct this in Eclipse’s formatter for compliance with Checkstyle’s requirement.

  • Similarly, it is necessary to modify blank_lines_between_type_declarations to comply with the style requirements.

  • I enlarged permission to the formatter to split lines (by modifying multiple alignment_for_ settings such as alignment_for_annotations_on_enum_constant, alignment_for_method_declaration, …): under the original configuration file, the formatter may leave lines longer than 100 characters where it is actually possible to reformat them; which Google style forbids. Here is an example of a difference this makes.

  • number_of_empty_lines_to_preserve (configured so as to suppress vertical spacing greater than one blank line) and blank_lines_after_last_class_body_declaration (configured so as to suppress blank lines before closing bracket) are personal preferences. This may be subjective, but these settings are compatible with Google style (the first one even encouraged), and I think they make sense generally.

  • There is no way (using configuration only) to force Eclipse formatter to satisfy the rule that <p> must be immediately followed by a word. Please vote for this bug.

  • Format Java code snippets inside 'pre' tags disabled because of Eclipse bug #552012. Please vote for this bug. (Related links: formatter bugs since 2013; open formatter bugs.)

I renamed the file and named the formatter configuration GoogleStyle patched to distinguish it from the original one.

Method 2: Configure Eclipse formatter and more

You may instead follow these instructions, which will add a “GoogleStyle patched” formatter style configured exactly as when following Method 1, and also tweak multiple other aspects of Eclipse.

Export your settings

Here is how to compare your formatter settings to these ones (or any other ones), assuming you modified them manually using Eclipse’s preference GUI.

  • From Eclipse preferences: Java / Code Style / Formatter / Edit / Export…, save it as Untitled.xml (for example)

  • Download this repository’s Sort profile settings jshell script

  • Run jshell -R-ea "-R-Dfile=Untitled.xml" --startup "Sort profile settings.jsh"

You obtain an Untitled.xml file with sorted settings elements, which you can then compare using any diff program to other similarly sorted Eclipse formatter configuration files. (It seems to me that Eclipse’s export ordering is not deterministic, hence the sorting step.)

To transform these (or your) settings to the EPF (Eclipse Preference File) format, run rm out.epf ; jshell -R-ea "-R-Dinput=eclipse-java-google-style patched.xml" --startup "Formatter to EPF.jsh".

References

Some relevant screenshots