-
Notifications
You must be signed in to change notification settings - Fork 24.9k
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
Format Java source files automatically #46745
Conversation
Fomatting can be checked or carried out through Gradle.
Also fix the interleaving of imports and comments in a few files, which the formatted rejected.
Pinging @elastic/es-core-infra |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great ! Thanks for working on it !
I added some comments, glad to help with any of them.
Does the tool re-format false == something
to ! something
?
We feel strongly for the former and most tool would re-write that.
IDEA constantly complains about that with no apparent way to turn it off too.
PS: a few other examples of the what the new format changes would be useful too. Maybe add some files that have most changes ? PS2: How long does it take for it to run ? Looking to see how much longer |
The formatter is quick, I can reformat the entire codebase in about 4-5 minutes on my laptop. The IDE plugin formats a file instantaneously. I've added somne choice reformatted examples. The formatting is actually quite conservative, and seems mostly concerned with whitespace, line length and ordering imports. From that perspective, it is nowhere near to replacing Checkstyle. |
How long does it take to check that the format is the one we expect ? |
CONTRIBUTING.md
Outdated
[google-java-format](https://github.com/google/google-java-format), which | ||
enforces the [Google Java | ||
Style](https://google.github.io/styleguide/javaguide.html). There are | ||
plugins available for Eclipse, JetBrains IDEs such as IntelliJ IDEA, and a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we link directly to the plugins ?
Alternatively @mark-vieira do you know a way to generate an IDEA configuration that would at least suggests users to install the plugin ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not aware of any. I suspect something like installing a plugin is not something most folks want to happen when importing a project. There are likely security concerns there.
The preview gradle integration for google-java-format did an inadequate job at reporting problems, so I've switched to using Spotless. I had to upgrade the JGit dependency in order to get error reporting to work. Also commit formatting changes to `TestingConventionsTasks.java`, since it demonstrates a file that had suffered from being formatted.
SpotlessI had another go at using Note that I had to upgrade a Groovy / GradleI also tried formatting the Groovy and Gradle files with spotless, which almost worked great, but it relies on an Eclipse formatter, and it did some very strange things with indentation, so I removed it again. One for the future perhaps. Formatting notesI also found a pathological formatting case in Now, to address the immediate issue of Checkstyle, we could disable that check for Java files, because the formatter (usually) doesn’t allow long lines. That aside, if we proceed with formatting the codebase then we may want to follow up with some refactoring to make such files more palatable (e.g. extract expressions to vars). The other options is to switch to 2-space indents, which would probably reduce the size of diff significantly (if you ignore whitespace changes). What do people think? On a similar note, the formatter will wrap comments to satisfy the line length constraint, which sometimes mangles the readability of comments somewhat. It’s not a big deal, and perhaps the team can fix up any particularly bad examples as they find them. Issues with JavaDocEntertainingly, the formatter tries much harder to do something pretty with JavaDoc comments, and will even format HTML (and remove some closing HTML tags in the process, which I don’t agree with). Less entertainingly, the formatter will sometimes break a line after a |
This looks good to me. The only problem I see is that making this happen all at once will be very difficult without generating lots of conflicts in all directions. E.x. we could specifically enable this for projects until we have them all and then enable for all by default ? Or enable for all and specifically disable for all existing project so new projects added will have it enabled by default and existing ones will migrate. |
@atorok I would have sworn I needed the I've added a list of projects to format, and included only I also investigating the @rjernst @jasontedor Feels like this PR is in good shape, and the outstanding questions are:
|
We will have to consider how this affects backporting. |
This is a good point. The assumption would be that if we introduce this, we would first reformat all existing code. Then all subsequent PRs should be on top of that. That said, we'd have to implement this back to all branches we intend to backport changes to, and also reformat those branches, or else as Alpar points out, backporting PRs from post-formatted branches is going to be a nightmare. |
It's for this reason that when we discussed this in the past we concluded the best time to make a transition like this is when we cut a major branch from master. If we did this here, we would hold this change until right before we cut 8.0 and 8.x from master. In this way, we would end up with 8.0, and 8.x, and master all formatted the same, and then backports from master to 8.x and 8.0, and 8.x to 8.0 would go through without pain since the branches can not have deviated between formatting master and cutting the branches. The only wrinkle is 7.last but I find that as time goes on (e.g., from master to 6.8) such backports become less and less clean anyway. |
So just so confirm, in that case we wouldn't gradually opt in Gradle projects over time, but instead format the whole repo just before cutting the branches. Correct? If so, I think I should send an email about this to the broader ES team sooner rather than later. (I'm also looking for further feedback, if you have any, on the formatting itself, given the switch to the Eclipse JDT formatter.) |
f677408
to
5b76280
Compare
I've trimmed this PR now so that it can be merged without doing anything 😁 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please change the title now that we are no longer evaluating google-java-format? I left a couple minor comments.
|
||
// See CONTRIBUTING.md for details of when to enabled this. | ||
if (System.getProperty('spotless.paddedcell') != null) { | ||
paddedCell() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we have everything about the formatting doable by the IDE plugins?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we'll have much need to enable this on a day-to-day basis, and it's outside the remit of the IDE plugins. This option lets Spotless cope when formatting isn't idempotent, which happened in a few places with GJF. It's a seperate thing from the configured formatting options.
@pugnascotia hope you don't mint that I back-ported this to |
This commit adds a Java source formatter and checker into the build process. This is not yet enabled for any sub-projects - to format and check a sub-project, add its Gradle path into `build.gradle` and run: ./gradlew spotlessApply to format, and: ./gradlew spotlessJavaCheck # or: ./gradlew precommit to verify formatting.
I don't mind at all, we may very well end up formatting |
Even if we don't, there's value in keeping the build code similar across branches to make it easy to pack-port improvements. |
This PR introduces support for formatting Java code using
google-java-formatEclipse JDT. Fomatting can be verified or executed out through Gradle. There is a CLI, and plugins for Eclipse and Jetbrains IDEs.Formatting is executed with:
Checking is executed with:
Checking is also carried out during the
precommit
task.I've updated
CONTRIBUTING.md
with some notes on formatting, IDE plugins, etc.Sub-project needs to opt-in before the formatter will look at them - currently the list of opted-in projects is empty. This PR just adds the mechanism.