question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to disable formatting for snippet? Java/Maven

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jbduncancommented, 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")
    })
  }
}
1reaction
nedtwiggcommented, Sep 12, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

intellij idea - How to disable code formatting for some part of ...
You must go to Preferences -> Code Style -> General -> Formatter Control and check Enable formatter markers in comments for this to...
Read more >
Disable code formatting per region using comments
I'd like to selectively disable the IntelliJ IDEA code formatter for a portion of code just like I can in Eclipse. Does IntelliJ...
Read more >
Java formatting and linting - Visual Studio Code
You can open the editor with the command Java: Open Java Formatter Settings with Preview. In the editor, you can change the formatter...
Read more >
formatter-maven-plugin – Examples
Custom Configuration File · In Eclipse, go to Window > Preferences > Java > Code Style > Formatter · If you have a...
Read more >
Configuring a Project to Exclude Certain Sonar Violations
In this short tutorial, we'll look at how to disable Sonar checks. ... This snippet defines analysis only for java files from the ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found