Spotless: google-java-format doesn't respect formatter:off

Created on 27 Aug 2019  路  5Comments  路  Source: diffplug/spotless

  • [Gradle ] gradle or maven version
  • [3.24.1] spotless version
  • [Linux Manjaro ] operating system and version

In my team we like builders and Fluent API's keep in several lines. Apparently, Google Java Format inline these lines, but we use to apply comments to don't format that specific lines. For example:

    // @formatter:off
    throw Problem.builder()
            .withStatus(Status.NOT_IMPLEMENTED)
            .build();
    // @formatter:on

If we apply Intellij Google Java Format plugin, ignore this line (the expected behavior), but Spotless Gradle plugin ignore this comments. Result:

    // @formatter:off
    throw Problem.builder().withStatus(Status.NOT_IMPLEMENTED).build();
    // @formatter:on

How we can configure to ignore this sections?

question

Most helpful comment

Hmmm.... Firstly I think the pairing you want it // @formatter:off and // @formatter:on, not just on as in your example.

Secondly, these tags are supported by the eclipse formatter. However, I believe they are explicitly not supported by google-java-format: https://github.com/google/google-java-format/issues/137

Before the google-java-format library existed, Google used the eclipse formatter with their own style file: https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml

I don't know how the IntelliJ plugin works, but I bet you can get what you want like this:

spotless {
  java {
    eclipse().configFile('eclipse-java-google-style.xml`)

All 5 comments

Hmmm.... Firstly I think the pairing you want it // @formatter:off and // @formatter:on, not just on as in your example.

Secondly, these tags are supported by the eclipse formatter. However, I believe they are explicitly not supported by google-java-format: https://github.com/google/google-java-format/issues/137

Before the google-java-format library existed, Google used the eclipse formatter with their own style file: https://github.com/google/styleguide/blob/gh-pages/eclipse-java-google-style.xml

I don't know how the IntelliJ plugin works, but I bet you can get what you want like this:

spotless {
  java {
    eclipse().configFile('eclipse-java-google-style.xml`)

As an avid google-java-format user myself, I can confirm that google-java-format indeed doesn't support the @formatter:(off|on) tags.

@nedtwigg's workaround looks very sensible to me, so that's what I'd recommend if you want to use those tags.

As for the IntelliJ google-java-format plugin, I suspect the reason why it recognises the tags is because it partially delegates to IntelliJ's default formatter, which recognises the tags by default. But I don't have a computer with IntelliJ in front of me, so I'd have to confirm that some other time.

I hope our responses have answered your question, and if you still need help, please feel free to follow up. :)

Thanks everybody, I'm going to apply the @nedtwigg suggestion that seems powerful and pretty customizable.

@nedtwigg sorry for my english :)

Just an update that since plugin-gradle 5.5.0 and plugin-maven 2.3.0, it is now possible to use google-java-format with spotless:off tags.

Was this page helpful?
0 / 5 - 0 ratings