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

How to disable formatting for snippet? Java/Maven #275

Closed
mattcopas opened this issue Aug 3, 2018 · 11 comments
Closed

How to disable formatting for snippet? Java/Maven #275

mattcopas opened this issue Aug 3, 2018 · 11 comments

Comments

@mattcopas
Copy link

How do I turn off formatting for a certain bit of code?

I am trying to disable it for a method chain.

I've tried using // @Formatter syntax
e.g.

public void foo() {
  // @formatter:off
  here()
    .dont().format()
  .the()
       .code();
  // @formatter:off
  // format as normal from here...
}

but this approach doesn't seem to work.

Is there a way to do this in spotless?

Thanks

@jbduncan
Copy link
Member

jbduncan commented Aug 3, 2018

Hi @mattcopas, welcome to Spotless!

What formatter from Spotless are you using? Does your setup look more like this:

spotless {
  java {
    eclipse().configFile(...)
  }
}

or more like this:

spotless {
  java {
    googleJavaFormat(...)
  }
}

or more like this?

spotless {
  java {
    custom(...)
  }
}

If you're using eclipse(), then using // @formatter:off and then // @formatter:on should work fine.

If you're using googleJavaFormat(), then sadly google-java-format explicitly doesn't support those tags, since it's meant to be a very simple solution where everyone can agree on a common style without nitpicking (google/google-java-format#137).

If you're using a custom step or something else, I'd love it if you could provide us with more context e.g. an MVCE so we can understand better what you're trying to achieve. :)

@jbduncan
Copy link
Member

jbduncan commented Aug 3, 2018

Oh, I should have asked first: are you using the Gradle plugin or the Maven plugin?

@mattcopas
Copy link
Author

@jbduncan Thanks for the quick response. We're using the google format, so it actually looks like this isn't a spotless issue but a google one (thank you for the link).

Closing as out of scope.

@jbduncan
Copy link
Member

jbduncan commented Aug 3, 2018

@mattcopas You're very welcome!

I don't know if this will help, but with Spotless you can exclude certain files from being formatted, so if you have Java files that contain a lot of DSL-like code or unusual method chaining, you could exclude them from being formatted by google-java-format and manually format and maintain them yourself.

Something like the following (untested, and may only work with Gradle's Kotlin DSL!):

spotless {
  java {
    target(fileTree("src/**/*.java") {
        exclude(
            "**/com/company/package/FirstFileWeDoNotWantToFormat.java",
            "**/com/company/package/SecondFileWeDoNotWantToFormat.java")
    })
  }
}

@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 any formatter, in any language, and exclude specific snippets with spotless:off tags.

@hanoch
Copy link

hanoch commented May 7, 2021

@nedtwigg are there spotless:off and spotless:on tags or XML comments that can be used with the eclipseWtp XML format as well, when formatting XML files?

Adding <!-- spotless:off --> didn't help.

I am using the maven plugin.

@nedtwigg
Copy link
Member

nedtwigg commented May 7, 2021

@hanoch yes, but you'll need to enable toggleOffOn()

@hanoch
Copy link

hanoch commented May 7, 2021

How do I "enable toggleOffOn()" from the maven plugin?

I am assuming that once enabled, the <!-- spotless:off --> and <!-- spotless:on --> XML comments syntax will work?

@hanoch
Copy link

hanoch commented May 7, 2021

NVM, found it - https://github.com/diffplug/spotless/tree/main/plugin-maven#spotlessoff-and-spotlesson
<toggleOffOn />

Worked for the XML format as well, with the <!-- spotless:off --> and <!-- spotless:on --> XML comments syntax.
Nice!

@hanoch
Copy link

hanoch commented May 7, 2021

BTW - is there a similar syntax to temporarily override an eclipseWtp *.prefs properties, such as lineWidth=150, in a specific file segment?

@nedtwigg
Copy link
Member

nedtwigg commented May 7, 2021

#817 is the closest we've got.

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

No branches or pull requests

4 participants