22

I have an xml configuration file for maven checkstyle plugin. I have installed Intellij Idea plugin and see all checkstyle violation. Is there a way to fix most of them automatically? For example auto formatting setting? Is there a way to setup Intellij Idea to autoformat from checktyle config file?

botchniaque
  • 4,698
  • 3
  • 35
  • 63
Cherry
  • 31,309
  • 66
  • 224
  • 364

3 Answers3

8

There is an option now.

checkstyle-IDEA since 4.24.0 features import of checkstyle config.

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). Go to Settings|Editor|Code Style, choose a code style you want to import CheckStyle configuration to. Click on the gear, then 'import scheme', 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. Copied the answer from here

Edit: Quote has been changed for clarity and to meet the current UI design.

rcreswick
  • 16,483
  • 15
  • 59
  • 70
Ajay Yadav
  • 798
  • 1
  • 8
  • 18
  • 2
    Note - this wasn't working for me, and I was about ready to write up a bug report, and then I noticed that I have a local .editorconfig file. It turned out the .editorconfig file supersedes the other editor settings. Specifically my .editorconfig file specified an indent_size = 4, but google's checkstyle xml specifies 2. Once I commented out the line in my .editorconfig, the auto-format worked. – Adam Wise Mar 29 '19 at 17:47
  • 7
    How does importing an existing xml enable autofixing the issues? – daniu Apr 16 '19 at 10:25
  • 1
    @AdamWise so true.. I was troubled with this file for a while and the issue is that 4 spaces and 2 spaces indent. Thanks – Prabs Oct 07 '19 at 06:38
  • I am not able to see the Manage button – inquisitive Sep 03 '20 at 11:18
  • 2
    this doesn't answer the question - they were asking about autofixing which apparently Eclipse can do – Lucas Ross Aug 22 '22 at 20:13
5

I have the same problem. As far as I know, the rules used for reformatting cannot be imported from Checkstyle config. You can configure your Checkstyle rules in the Idea's Code Style config (File -> Settings -> Code Style) and then reformat the code (Code -> Reformat Code [Ctrl+Alt+L]) according to the rules (you can apply it to the whole code base at once).

botchniaque
  • 4,698
  • 3
  • 35
  • 63
4
  1. import your code style config first enter image description here

  2. reformat your code.

Yichang Wu
  • 61
  • 5