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

IDEA-180965: Add support for IntelliJ formatter #200

Open
fabriziocucci opened this issue Feb 8, 2018 · 24 comments
Open

IDEA-180965: Add support for IntelliJ formatter #200

fabriziocucci opened this issue Feb 8, 2018 · 24 comments

Comments

@fabriziocucci
Copy link

fabriziocucci commented Feb 8, 2018

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!).

@nedtwigg
Copy link
Member

nedtwigg commented Feb 8, 2018

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 nedtwigg changed the title [QUESTION] Is there any plan to support IntelliJ generated code style for Java? Add support for IntelliJ formatter Feb 8, 2018
@fabriziocucci
Copy link
Author

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

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

@JLLeitschuh JLLeitschuh changed the title Add support for IntelliJ formatter IDEA-180965: Add support for IntelliJ formatter Feb 13, 2018
@diffplug diffplug deleted a comment from fabriziocucci Jul 24, 2018
@diffplug diffplug deleted a comment from fabriziocucci Jul 24, 2018
@leonard84
Copy link

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

@nedtwigg
Copy link
Member

nedtwigg commented Jun 3, 2020

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.

@anandbhaskaran
Copy link

It would be awesome if this come into reality!

@lfgcampos
Copy link

Long shot but, any news about it?

@sbilello
Copy link

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

@jbduncan
Copy link
Member

jbduncan commented Oct 2, 2021

@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. :)

@ragurney
Copy link

ragurney commented Jan 2, 2022

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.

@nedtwigg
Copy link
Member

nedtwigg commented Jan 4, 2022

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.

@ragurney
Copy link

ragurney commented Jan 5, 2022

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.

@lutovich
Copy link
Contributor

lutovich commented Jan 6, 2022

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?

@ragurney
Copy link

ragurney commented Jan 6, 2022

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.

@melissachang
Copy link

melissachang commented Mar 10, 2022

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

@beatngu13
Copy link

@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')
  }
}

@melissachang
Copy link

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.

@tonjohn
Copy link

tonjohn commented Jun 8, 2023

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)

@nedtwigg
Copy link
Member

nedtwigg commented Jun 8, 2023

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.

@lipiridi
Copy link

@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

@IlyasYOY
Copy link

IlyasYOY commented Nov 2, 2023

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.

@nedtwigg
Copy link
Member

nedtwigg commented Nov 9, 2023

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.

@IlyasYOY
Copy link

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 #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!

@nedtwigg
Copy link
Member

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

Note the following:

  • in PR Add support for gofmt #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.

IlyasYOY added a commit to IlyasYOY/spotless that referenced this issue Jan 23, 2024
@IlyasYOY
Copy link

Thanks for the advice! Really appreciate it!

I've just opened a Draft PR.
#2020

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

No branches or pull requests