IntelliJ IDEA code format from checkstyle configuration
Asked Answered
H

6

60

I've decided to contribute to android maven plugin.

They use quite specific code style format. It will take some time for me to modify IDEA code format rules to satisfy their requirements.

Fortunately there is checkstyle configuration. I've already installed check-style plugin for IDEA and immediately see where my current formatting is wrong.

Unfortunately I didn't find way to import checkstyle configuration into IDEA formatting rules.

I'm interested if it's possible.

Hoberthobey answered 26/1, 2013 at 17:6 Comment(0)
P
23

UPDATE: supported since 4.24.0 plug-in version, see here for details.


Original answer from 2013:

Not possible yet, please star/vote to track the progress of the issue.

Plugin On GitHub

How to set it up

install pluging

Example of how it looks like in intellij

Pismire answered 26/1, 2013 at 17:31 Comment(2)
Sad :( What is common way to share code formatting across the team? I saw post about some file from ./idea folder. But I usually avoid to commit ide specific files to repositoriesHoberthobey
.idea/codeStyleSettings.xml, see the FAQ.Pismire
J
79

finally there is something:

checkstyle-IDEA since 4.24.0 features import of checkstyle config.

A solution is available now:

  1. Please install CheckStyle-IDEA plugin (http://plugins.jetbrains.com/plugin/1065?pr=idea), it can be found via plug-in repository (Settings|Plugins|Browse repositories).

  2. Go to Settings|Editor|Code Style, choose a code style you want to import CheckStyle configuration to.

  3. Click Manage...|Import.., choose "CheckStyle Configuration" and select a corresponding CheckStyle configuration file. Click OK.

At the end you will see a message "CheckStyle configuration settings were imported to ... scheme".

If you encounter any problems, you can create a new issue for the CheckStyle plug-in here: https://github.com/jshiell/checkstyle-idea/issues. This issue is actually a duplicate of https://github.com/jshiell/checkstyle-idea/issues/126.

CheckStyle plug-in version where the feature is available is 4.24.0

Copied from https://youtrack.jetbrains.com/issue/IDEA-61520#comment=27-1292600

Jape answered 8/2, 2016 at 15:57 Comment(6)
Yay! I will try! Thanks for sharing!Hoberthobey
Is it possible wise verse? Export it to checkstyle from idea settings?Hoberthobey
This appears to still not be a solution to the question to configure formatting using CheckStyle rules.Declaration
@SanderVerhagen it is, actually. Once you imported the config, make sure that is is being applied to the project, and then do a ctrl-shift-alt-L to format the current file...Jape
Is it possible to export the Idea code style as a checkstyle.xml?Buttery
@SanderVerhagen if you also install the Save Actions plugin and configure it to reformat the file on save then it'll for automatically on saveCracow
P
23

UPDATE: supported since 4.24.0 plug-in version, see here for details.


Original answer from 2013:

Not possible yet, please star/vote to track the progress of the issue.

Plugin On GitHub

How to set it up

install pluging

Example of how it looks like in intellij

Pismire answered 26/1, 2013 at 17:31 Comment(2)
Sad :( What is common way to share code formatting across the team? I saw post about some file from ./idea folder. But I usually avoid to commit ide specific files to repositoriesHoberthobey
.idea/codeStyleSettings.xml, see the FAQ.Pismire
P
6

There is an ugly workaround that I have been able to live with:

First import the Checkstyle format into Eclipse, as explained here: How to generate an Eclipse formatter configuration from a checkstyle configuration?

Then export the format from Eclipse (via Windows --> Preferences --> Java --> Code Style --> Formatter -> Edit -> Export and import the resulting *.xml file into IntelliJ IDEA

Of course this is kinda bonkers especially if you do not have someone around who has Eclipse running anyway, as you have to install it just to be able to "translate" the checkstyle format into something IntelliJ can read; but if it only has to be done once per project it might be somewhat better than manually adjust the format.

Protoplasm answered 4/11, 2013 at 11:46 Comment(0)
R
4

This worked for me.

  1. Go to Preferences > Editor > Code Style > Java
  2. In the Scheme dropdown, import your desired checkstyle.xmlimport-cecktyle-scheme
  3. Intellij calls the newly imported scheme as Default copy
  4. Use the Rename option to choose any other name. enter image description here
Reclamation answered 30/9, 2022 at 8:56 Comment(0)
D
1

Just ran mvn spotless:apply as suggested by Intellij. This helped me auto resolve the issue.

Drawstring answered 4/8, 2023 at 17:40 Comment(0)
K
0

The solution that worked for me is using Adapter for Eclipse Code Formatter . IntelliJ IDEA 2022.3.1

  1. Install it using Settings -> Plugins -> Adapter for Eclipse Code Formatter
  2. After restart, Settings -> Adapter for Eclipse Code Formatter
    • Use Eclipse's Code Formatter
    • Eclipse formatter config -> Eclipse workspace/project folder or config file
Karnes answered 8/4, 2023 at 18:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.