spotless icon indicating copy to clipboard operation
spotless copied to clipboard

IDEA-180965: Add support for IntelliJ formatter

Open fabriziocucci opened this issue 7 years ago • 28 comments

The line wrapping options that can be configured with IntelliJ seem to be a little bit more flexible than the one available in Eclipse (e.g. lambda-related preferences).

The closest thing I've found to format Java sources using an IntelliJ code style is its command line formatter. Although that still requires having the full binaries of IntelliJ (but I hope to be wrong!).

fabriziocucci avatar Feb 08 '18 13:02 fabriziocucci

Happy to merge a PR for this, but currently nobody is working on it.

You can use a custom step do this, there's an example of how to do this (and a description of its shortcomings) in #119.

nedtwigg avatar Feb 08 '18 15:02 nedtwigg

Commented interesting issue on the IntelliJ issue tracker related to this enhancement:

https://youtrack.jetbrains.com/issue/IDEA-180965

fabriziocucci avatar Feb 12 '18 10:02 fabriziocucci

Also: Provide standalone formatter or API https://youtrack.jetbrains.com/issue/IDEABKL-7685

leonard84 avatar Sep 14 '18 11:09 leonard84

If you don't care about using the IntelliJ formatter specifically, but rather just the IntelliJ IDE to autoformat in a way that agrees with Spotless, another path forward would be to make a plugin for IntelliJ that calls out to the spotless IDE hook. For example, here's a plugin for VSCode.

nedtwigg avatar Jun 03 '20 17:06 nedtwigg

It would be awesome if this come into reality!

anandbhaskaran avatar Jun 08 '20 18:06 anandbhaskaran

Long shot but, any news about it?

lfgcampos avatar Jan 06 '21 11:01 lfgcampos

If we want to use the intellij java code style with spotless. What's the recommended approach? https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml

sbilello avatar Sep 30 '21 21:09 sbilello

@sbilello Spotless doesn't support the IntelliJ formatter yet. The way to get Google-style Java formatting is to use this instruction:

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

It uses google-java-format, which is a successor to the IntelliJ XML file you linked, so I recommend google-java-format over it. :)

jbduncan avatar Oct 02 '21 11:10 jbduncan

Hey! I've actually been working on this as this is something that affects my day-to-day workflow and have a very basic working version of a Spotless IntelliJ plugin working.

Here is the code.

I'm waiting on JetBrains to approve the plugin for publishing, so it isn't available on the Marketplace yet, but you can install it in your own IDE via Settings/Preferences > Plugins > ⚙️ > Install plugin from disk... using this zip.

It currently only supports an action "Reformat Code with Spotless" which is found under the Code menu item, but am planning on adding format on save and possibly linting/contextual fixes soon. Feedback encouraged and welcome.

ragurney avatar Jan 02 '22 01:01 ragurney

Fantastic, thanks so much @ragurney! Looks like a great plugin, I will submit a PR momentarily to better advertise your plugin to Spotless users.

If your goal is to use existing Spotless rules with IntelliJ, then this issue is now solved (I'll be using your plugin!).

Some people want something different, which is to have an intellij() rule in Spotless which delegates to the formatter which ships in the default IntelliJ IDE. I'll keep this issue open for users who are waiting on that, but my advice to them would be to use a project which is specific to formatting (e.g googlejavaformat, or ktfmt) in conjunction with your plugin.

nedtwigg avatar Jan 04 '22 22:01 nedtwigg

Got it, thanks so much @nedtwigg! Thank you for the explanation about the other issue here, that makes sense, and it's very exciting that the plugin might be helpful to other Spotless users! I'm hoping to add further functionality to it soon, and looking forward to any feedback or feature requests you might have after using it.

ragurney avatar Jan 05 '22 15:01 ragurney

Great plugin @ragurney! 👍

What do you think about adding support for Maven? Would it make sense to have a single Spotless IntelliJ plugin that supports both Gradle and Maven?

lutovich avatar Jan 06 '22 11:01 lutovich

Thanks @lutovich!

I'm leaning towards it being better to have a separate plugin for Maven just for separation of concerns, but if there's a dependable way to tell if an IntelliJ project is using Maven or Gradle that could work too and would be more DRY. I'll do some sleuthing and see if that's a possibility.

Regardless, I think a prerequisite for a maven plugin would be adding an IDE hook similar to the one available for Spotless Gradle.

ragurney avatar Jan 06 '22 19:01 ragurney

I was able to use File Watchers to run ./gradlew spotlessApply on save. Need to install File Watchers plugin. You can run any command. Note - Needs to be configured for every repo.

image

melissachang avatar Mar 10 '22 00:03 melissachang

@melissachang similar to @ragurney's IntelliJ plugin, this helps to execute Spotless via IntelliJ – which is already great!

However, the issue here is basically about loading the IntelliJ formatter into Spotless. For example:

spotless {
  java {
    intellij('173').configFile('intellij-prefs.xml')
  }
}

beatngu13 avatar Mar 11 '22 11:03 beatngu13

Yeah sorry for going a bit off-topic. It took me a while to figure out how to run spotless-on-save (mentioned as a feature request in this above comment). Currently it's not possible with Intellij formatter. Just documenting a way to do it, in case people want spotless-on-save.

melissachang avatar Mar 11 '22 17:03 melissachang

Regardless, I think a prerequisite for a maven plugin would be adding an IDE hook similar to the one available for Spotless Gradle.

Is there any particular reason that Spotless Maven lacks parity with Spotless Gradle regarding the IDE hook?

Are there extra challenges unique to maven in adding such functionality?

(I'm new to the Java ecosystem so apologies for my ignorance)

tonjohn avatar Jun 08 '23 11:06 tonjohn

Is there any particular reason that Spotless Maven lacks parity with Spotless Gradle

Nope, we just got PR's from Gradle users, that's all.

nedtwigg avatar Jun 08 '23 18:06 nedtwigg

@ragurney @lutovich @tonjohn @nedtwigg Hi! I've been developed a plugin that supports maven based on @ragurney's plugin (for which special thanks to him) and added some new features. https://plugins.jetbrains.com/plugin/22455-spotless-applier https://github.com/lipiridi/spotless-applier

lipiridi avatar Aug 12 '23 08:08 lipiridi

Hello! Any updates on the issue?

I found docs: Format files from the command line | IntelliJ IDEA Documentation.

I use this API myself to format code using neovim:

vim.keymap.set("n", "<leader>oC", ":!idea format -s ~/idea.xml -r %<CR>edit<CR>")

I'd be glad to try to implement the feature if maintainers think it's possible.

PS. I tested this on Linux & MacOS. There some caveats:

  • Some IDEA builds have this formatter builtin AFAIU. The version from Jetbrains Tools Box didn't seem to work.
  • MacOS version did formatting asynchronously. This is a problem. Version installed in Manjaro works synchronously with concise logs .

Anyway considering all above, I'd gave a try to the feature. I'd add this as experimental due to unstable API of the IDE.

IlyasYOY avatar Nov 02 '23 07:11 IlyasYOY

A PR for this would be very welcome. ForeignExe might be the easiest way to implement it. It's okay if it only works on some platforms, so long as it works for the platform you use. Future PR's can always add support for missing platforms.

nedtwigg avatar Nov 09 '23 19:11 nedtwigg

Thanks for your hint @nedtwigg

I created the *Step class a few days ago. It works fine on my macos machine. I faced a problem testing it on CI, there is no IDEA binary & simple installation from brew might require tweaking.

I saw your PR https://github.com/diffplug/spotless/pull/672. Maybe I'm missing something but I don't have any good ideas on running IDEA in test on every machine (CI at least). I can come up with a solution but maybe the team has an appropriate solution for the problem.

Contribution guide helped a lot, thanks!

IlyasYOY avatar Jan 16 '24 08:01 IlyasYOY

Amazing! IdeaStep has been implemented, after half a decade in the desert!

Note the following:

  • in PR #2001
  • gradle/special-tests.gradle
  • testlib/src/main/java/com/diffplug/spotless/tag/GofmtTest.java
  • note that all tests have @GofmtTest from the tag above

In this way, the tests are always compiled, even though they aren't run.

Of all these "special" tests, the only ones which get run on CI right now are the npm tests. It would be better if they all ran, but it would be worse if the steps didn't exist at all.

The important thing is that if someone wants to develop the step further, they aren't starting from scratch - they have tests, they just need to setup their local machine to have foreign exe in question.

nedtwigg avatar Jan 23 '24 05:01 nedtwigg

Thanks for the advice! Really appreciate it!

I've just opened a Draft PR. https://github.com/diffplug/spotless/pull/2020

IlyasYOY avatar Jan 23 '24 20:01 IlyasYOY