Confgure IntelliJ to use the same Java formatter as Spotless
Asked Answered
B

3

27

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?

Bifurcate answered 15/1, 2020 at 15:29 Comment(1)
I'd ask that on the IntelliJ forum, as the developers who know it the best stalk those forums regularly.Pile
B
10

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

Broadbrim answered 19/12, 2021 at 9:52 Comment(0)
H
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.

Hoofbound answered 11/10, 2020 at 9:50 Comment(3)
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.Gusher
@Hakanai, does the following plugin fit your description?Central
@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.Gusher
M
5

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.

Malaise answered 12/8, 2023 at 8:5 Comment(1)
I cannot actually find this plugin via settings->plugins->marketplace in my intellij community edition. Im using IDEA 2023.3Backboard

© 2022 - 2024 — McMap. All rights reserved.