20

I use the Spotless Gradle plugin to ensure consistent formatting of the Java code in my app. The relevant entries in build.gradle are

plugins {
  id "com.diffplug.gradle.spotless" version "3.27.0"
}

spotless {
  java {
    // The available versions of the Eclipse JDT formatter are defined here
    // https://github.com/diffplug/spotless/tree/master/lib-extra/src/main/resources/com/diffplug/spotless/extra/eclipse_jdt_formatter
    eclipse('4.13.0')
    indentWithSpaces()
    removeUnusedImports()
  }
}

Is there some way I can configure IntelliJ to use the same formatting settings?

Michael Piefel
  • 18,660
  • 9
  • 81
  • 112
Antonio Dragos
  • 1,973
  • 2
  • 29
  • 52

3 Answers3

8

A not 100% satisfying but working approach:

1. Configure the Spotless tasks

spotless {
    format 'misc', {
        target '*.properties','*.gradle', '*.md', '.gitignore' // etc.
        // code style rules
    }
    java {
        // code style rules
    }
}

2. Add task activation to *Apply tasks

enter image description here

3. Assign convenient keystroke to Build Project

Do this under Settings > Keymap.

Keep in mind that Build Project is using IntelliJ's internal builder. It's incremental (performant), but in case of custom/third-party gradle tasks, the build result might deviate from the $ gradle build result.

In case of a Spring application utilizing DevTools, the build will lead to the changes/updates being served automatically.


With the IDE Hook it should be rather trivial to implement a dedicated Spotless formatter plugin for IntelliJ: https://github.com/diffplug/spotless/blob/master/plugin-gradle/IDE_HOOK.md

So far I wasn't able to find one though.

Tob
  • 352
  • 2
  • 8
  • It's been a year and amazingly still no plugin for spotless in IDEA, amazing because it seems like it shouldn't be so hard to implement. Running the Gradle task doesn't sound like a bad idea anyway though, and you can hook other cleanup tasks in that way. One improvement could be to use the `idea-ext` plugin to set up the hook without having to use the UI. – Hakanai Sep 24 '21 at 01:10
  • @Hakanai, does the [following](https://plugins.jetbrains.com/plugin/18321-spotless-gradle) plugin fit your description? – Dmitrii Badretdinov Jan 11 '23 at 11:12
  • @DmitriiBadretdinov maybe. Would have to play with it a bit. I'd prefer it to replace the existing reformat action rather than being a new action, but it looks like it would at least do something. – Hakanai Jan 12 '23 at 05:17
7

Although Tob's approach works, I use a bit different way of configuring it.

1. Create an external tool in the Preferences

enter image description here

2. Assign a shortcut for the created external tool

I prefer using + + ;, because it's easy to remember (IDEA uses + + L by default). enter image description here

3. Use it

Press + + ; and wait until all the files are processed and the IDE reloaded changed files. Usually, it takes 2-5 seconds.

Warning: spotless may crash (usually it happens because of mistakes in your code, so be sure your code is compilable): enter image description here

void
  • 7,760
  • 3
  • 25
  • 43
DiPech
  • 71
  • 2
  • 3
1

You can install this plugin: https://plugins.jetbrains.com/plugin/22455-spotless-applier.

It allows running the spotless Gradle and Maven tasks from within the IDE, either on the current file selected in the editor or for the whole project.

Dimitrii
  • 23
  • 3