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

google-java-format doesn't respect formatter:off #446

Closed
atrujillofalcon opened this issue Aug 27, 2019 · 5 comments
Closed

google-java-format doesn't respect formatter:off #446

atrujillofalcon opened this issue Aug 27, 2019 · 5 comments
Labels

Comments

@atrujillofalcon
Copy link

atrujillofalcon commented Aug 27, 2019

  • [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?

@nedtwigg
Copy link
Member

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: google/google-java-format#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`)

@jbduncan
Copy link
Member

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. :)

@atrujillofalcon
Copy link
Author

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

@nedtwigg nedtwigg changed the title Spotless ignore not enabled formatter sections google-java-format doesn't respect formatter:off Aug 28, 2019
@atrujillofalcon
Copy link
Author

@nedtwigg sorry for my english :)

@nedtwigg
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants