Search code examples
intellij-ideaeditorconfig

Intellij IDEA - how to override code style config


We have a Java project in small team, all developers use Intellij IDEA.

I need to setup code style so that

  • some settings (for example use tabs instead of spaces, import order, threshold for asterisk import etc.) are common for all developers, these settings are obligatory and versioned in Git
  • tab size setting is up to each developer since it affects only appearance on his screen and not source code. It should not be versioned. Technically it should override common setting (actually its value should not be among obligatory values at all).

How to achieve it?

My attempts:

  • I am aware of "Stored in Project" vs "Stored in IDE" configuration where the first one is recommended to be in VCS. However, if I define common settings as "Project" configuration, user can define custom "IDE" setting and activate it instead of the "Project" configuration, not on top of it.
  • We also have .editorconfig file in project root with some common settings. Since the file is versioned in Git there would be great to have something like .editorconfig-gitignored file with custom settings (or put .editorconfig one level down into each subfolder, which is clumsy)
  • Since Intellij Idea can override project settings with .editorconfig settings, it seems to be a solution to define common settings in Idea and custom setting in (newly gitignored) .editorconfig file. Unfortunately some properties in .editorconfig are mandatory as well so I feel it would be downgrade at this moment.

Solution

  • Finally we decided to treat .editorconfig file as main source of truth. The file is versioned. The IDEA is set to prefer EditorConfig settings over IDEA XML settings. (Ctrl+Alt+S -> Editor -> Code Style -> General -> Enable EditorConfig support)

    The .editorconfig properties such as indent_style = tab are obligatory for all team members. On the other hand, properties such as tab_width intentionally are not in .editorconfig so IDEA consults its own settings (these are not versioned).

    This solution is facilitated by proprietary ij_... keys in .editorconfig. With such properties all IDEA config could be exported to .editorconfig file. When I asked a question 2 years ago, these keys weren't supported or I didn't notice them.