Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format Java source files automatically #46745

Merged
merged 22 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
f6176bf
Format Java using google-java-format
pugnascotia Sep 16, 2019
e833d42
Add notes on handling file that do not format
pugnascotia Sep 16, 2019
72274ca
Apply format plugin in the right place
pugnascotia Sep 16, 2019
a5eec34
Some big examples of reformatted code
pugnascotia Sep 16, 2019
1194956
Switch to Spotless for code formatting
pugnascotia Sep 17, 2019
b5d2b34
Changes to make Spotless format consistently
pugnascotia Sep 18, 2019
d85a1d4
Only format projects on an accept list.
pugnascotia Sep 18, 2019
5ef0dc4
Revert formatted outside of buildSrc
pugnascotia Sep 18, 2019
06ab706
Format buildSrc directory
pugnascotia Sep 18, 2019
5c1ee6a
Merge remote-tracking branch 'upstream/master' into auto-format-java-…
pugnascotia Sep 18, 2019
6a32487
Reformat after merge
pugnascotia Sep 18, 2019
4423991
Update checkstyle suppressions
pugnascotia Sep 18, 2019
0c432a5
Add an option to enable paddedCell() from the gradle CLI
pugnascotia Sep 21, 2019
e1d27cd
Format java with with Eclipse JDT instead
pugnascotia Sep 21, 2019
b0d0d6d
Reformat buildSrc/
pugnascotia Sep 21, 2019
7d69c76
More formatter settings tweaks
pugnascotia Sep 23, 2019
8418c15
Revert formatted ahead of merge with master
pugnascotia Sep 25, 2019
82c44b7
Merge remote-tracking branch 'upstream/master' into auto-format-java-…
pugnascotia Sep 25, 2019
5b76280
Leave the formatting project list empty, so we can merge the Gradle c…
pugnascotia Sep 25, 2019
2a2856b
Update CONTRIBUTING.md
pugnascotia Sep 25, 2019
896cbe3
Update CONTRIBUTING.md
pugnascotia Sep 25, 2019
7314c3a
Remove spotless command that the formatter already handles
pugnascotia Sep 26, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 67 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,19 +155,73 @@ For Eclipse, go to `Preferences->Java->Installed JREs` and add `-ea` to

### Java Language Formatting Guidelines

Java files in the Elasticsearch codebase are formatted with
[google-java-format](https://github.com/google/google-java-format), which
enforces the [Google Java
Style](https://google.github.io/styleguide/javaguide.html). The formatting
check can be run explicitly with:

./gradlew spotlessJavaCheck

The code can be formatted with:

./gradlew spotlessApply

These tasks can also be run for specific subprojects, e.g.

./gradlew server:spotlessJavaCheck

Code is formatted with the `--aosp` option (Android Open Source Project),
which indents with 4 spaces instead of 2. There are no other formatting
options available by design.

Please follow these formatting guidelines:

* Java indent is 4 spaces
* Line width is 140 characters
* Lines of code surrounded by `// tag` and `// end` comments are included in the
documentation and should only be 76 characters wide not counting
leading indentation
* The rest is left to Java coding standards
* Disable “auto-format on save” to prevent unnecessary format changes. This makes reviews much harder as it generates unnecessary formatting changes. If your IDE supports formatting only modified chunks that is fine to do.
* Wildcard imports (`import foo.bar.baz.*`) are forbidden and will cause the build to fail. This can be done automatically by your IDE:
* Eclipse: `Preferences->Java->Code Style->Organize Imports`. There are two boxes labeled "`Number of (static )? imports needed for .*`". Set their values to 99999 or some other absurdly high value.
* IntelliJ: `Preferences/Settings->Editor->Code Style->Java->Imports`. There are two configuration options: `Class count to use import with '*'` and `Names count to use static import with '*'`. Set their values to 99999 or some other absurdly high value.
* Don't worry too much about import order. Try not to change it but don't worry about fighting your IDE to stop it from doing so.
* Line width is 100 characters
* Lines of code surrounded by `// tag` and `// end` comments are included
in the documentation and should only be 76 characters wide not counting
leading indentation
* Wildcard imports (`import foo.bar.baz.*`) are forbidden and will cause
the build to fail. This can be done automatically by your IDE:
* Eclipse: `Preferences->Java->Code Style->Organize Imports`. There are
two boxes labeled "`Number of (static )? imports needed for .*`". Set
their values to 99999 or some other absurdly high value.
* IntelliJ: `Preferences/Settings->Editor->Code Style->Java->Imports`.
There are two configuration options: `Class count to use import with
'*'` and `Names count to use static import with '*'`. Set their values
to 99999 or some other absurdly high value.

#### Editor / IDE Support

See the `google-java-format` repository for details on installing the
[plugins for Jetbrains IDEs] and [Eclipse plugins].

For editors that support formatting via a CLI command, see the
[google-java-format CLI instructions].

#### Formatting failures

Sometimes Spotless will report a "misbehaving rule which can't make up its
mind" and will recommend enabling the `paddedCell()` setting. If you
enabled this settings and run the format check again,
Spotless will write files to
`$PROJECT/build/spotless-diagnose-java/` to aid diagnosis. It writes
different copies of formatted files, so that you can see how they differ
and infer what is the problem. Ther are two common problems:

1. Single-line comments at the end of a line of code can be wrapped onto
multiple lines during one formatting interation, and then change
indentation in another iteration. The solution is to identify the
comment and move it so that it is consistently formatted.
Multiline-style comments are not affected.
2. Sometimes `<p>` tags are inserted within `<code>` tags in JavaDoc.
The solution is to wrap with `<pre>` tags. See:

https://github.com/google/google-java-format/issues/254

The `paddedCell() option is disabled for normal operation in order to
detect any misbehaviour.

### License Headers

Expand Down Expand Up @@ -391,3 +445,6 @@ repeating in this section because it has come up in this context.
[eclipse]: http://www.eclipse.org/community/eclipse_newsletter/2017/june/
[intellij]: https://blog.jetbrains.com/idea/2017/07/intellij-idea-2017-2-is-here-smart-sleek-and-snappy/
[shadow-plugin]: https://github.com/johnrengelman/shadow
[plugins for Jetbrains IDEs]: https://github.com/google/google-java-format#intellij-android-studio-and-other-jetbrains-ides
[Eclipse plugins]: https://github.com/google/google-java-format#eclipse
[google-java-format CLI instructions]: https://github.com/google/google-java-format#from-the-command-line
25 changes: 25 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ plugins {
id 'com.gradle.build-scan' version '2.4'
id 'lifecycle-base'
id 'elasticsearch.global-build-info'
id "com.diffplug.gradle.spotless" version "3.24.2" apply false
}

apply plugin: 'nebula.info-scm'
Expand Down Expand Up @@ -98,6 +99,30 @@ subprojects {
plugins.withType(BuildPlugin).whenPluginAdded {
project.licenseFile = project.rootProject.file('licenses/APACHE-LICENSE-2.0.txt')
project.noticeFile = project.rootProject.file('NOTICE.txt')

// Projects that should be formatted with Spotless are listed here, by
// project path. Once the number of formatted projects is greater than
// the number of unformatted projects, this can be switched to an
// exclude list, and eventualy removed completely.
def projectPathsToFormat = [
':build-tools'
]

if (projectPathsToFormat.contains(project.path)) {
project.apply plugin: "com.diffplug.gradle.spotless"

spotless {
java {
// Use Android Open Source Project settings (i.e. 4 space indents)
googleJavaFormat().aosp()

// See CONTRIBUTING.md for details of when to enabled this.
// paddedCell()
pugnascotia marked this conversation as resolved.
Show resolved Hide resolved
}
}

precommit.dependsOn 'spotlessJavaCheck'
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ dependencies {
compile 'com.netflix.nebula:gradle-extra-configurations-plugin:3.0.3'
compile 'com.netflix.nebula:nebula-publishing-plugin:4.4.4'
compile 'com.netflix.nebula:gradle-info-plugin:3.0.3'
compile 'org.eclipse.jgit:org.eclipse.jgit:3.2.0.201312181205-r'
compile 'org.eclipse.jgit:org.eclipse.jgit:5.5.0.201909110433-r'
compile 'com.perforce:p4java:2012.3.551082' // THIS IS SUPPOSED TO BE OPTIONAL IN THE FUTURE....
compile 'org.apache.rat:apache-rat:0.11'
compile "org.elasticsearch:jna:4.5.1"
Expand Down
Loading