Saturday, November 8, 2014

How to make you own code formatting style with Intellij IDEA

With the newest version of Intellij IDEA, it's allow you to import existing code style settings from Eclipse code formatter xml. Below screenshot shows how you can import existing Eclipse Code formatter to Intellij. You can navigate to File > Settings>Code Style > Manage where you popup a window to import code style. There you can import eclipse code formatter style to Intellij. Here after you may needs to get the actual code style xml which create by Intellij IDEA, it will be reside in  IDEA's meta resource holding configuration directory in home directory where ".IntelliJIdea13/config/codestyles". In there you will notice that there will be a configuration file is create for newly added code style. You can use it to share with the others. For reuse the Intellij IDEA code style xml, you can drop configuration xml in  ".IntelliJIdea13/config/codestyles". Then newly added code style will show in the  File > Settings>Code Style > Manage  tab. Then you can select it to use in the project. In linux systems, shortcut "Ctrl+Alt+L" will do the code formatting for your class. But same shortcut is use to lock the account. So you may change either system or intellij IDEA shortcut to dealt with this.

Add, edit or select code style

But if you needs to create a new code formatting style, same way you can first go to File > Settings>Code Style > Manage and select existing code formatter style. Afterr words you can do "Save As"where it saves new copy from the original which you can do the changes.

So what are the available code formatting settings in Intellij IDEA. As the most popular Java IDE, it has several cool properties that you can select to make your own code style formatter.

I will give a brief idea on what each tab do. You can find more details in [1]. I will closely look at Java language specific code formatting.

There is a General tab which contains the general settings for the all the supported languages as in below image. Where you specify maximum length for the line and etc.

General Tab

General tab contains below properties. For more details look at  [1].
  • Right margin, Formatter on/off tags
  • Indentation
  • Indent size
  • Usage of ‘Tab’ character
  • Usage of Tab only for leading indentation (Smart Tabs)
  • Indent ‘case’ branches from ‘switch’
  • Indent class members
  • Keep comment at first column
Spaces tab contains the rules on where the code needs to have spaces on. For example people may prefer space after the class name. Below are the several properties. For more details look at  [1].
  • Before/after comma (as set for Eclipse method declaration parameters)
  • After comma in type arguments
  • Within array initializer braces
  • Within brackets (in array reference)
  • Within parentheses of: annotation, ‘for’, ‘if’, ‘catch’ ’while’, ’switch’, method, empty method, parenthesized expression, method call, type cast, ‘synchronized’
  • Before parentheses of: ‘try’, ‘for’, ’while’, ‘switch’, method, ’if’, ‘catch’, method, method call, ‘synchronized’.
  • After type cast
  • Around unary, assignment operators (if it’s set for ‘before’ and ‘after’ in Eclipse).
  • Before opening brace of: array initializer, ‘switch’
  • Before ‘?’ in conditional expression
  • Space before/after ‘:’ in conditional expression
  • Space around binary operators (a single Eclipse setting is mapped to multiple IntelliJ IDEA’s settings)
Spaces tab

Blank lines tab contains the rules for where to insert blank lines between different sections such as imports licence headers and etc. Below are the several properties. For more details look at  [1].
  • Around fields and methods
  • Before/after package
  • Before/after imports
  • Before method body
  • Keep blank lines in code (number of empty lines to preserve)
Blank Lines

Wrapping and Braces  tab contains the rules for where we needs to wrap lines for example, we may perform line wrap if single line exceed maximum line length. Below are the several properties. For more details look at  [1]
  • New line before: closing brace in array initializer, ‘else’ in ‘if’ statement, ‘finally’ and ‘catch’ in ‘try’ statement, binary operator (if wrapped)
  • New line after: opening brace in array initializer
  • Special ‘else if’ treatment (compact ‘else if’)
  • Keep simple blocks in one line
  • Keep control statements in one line
  • Alignment of: array initializer expressions, arguments in method declarations and calls, field declarations, extends list, assignments, binary expressions, ‘throws’ clause, resources in ‘try’.
  • Brace style for: code blocks, methods and classes
Wrapping and Braces
JavaDoc tab contains rules for format java docs. Below are the several properties. For more details look at  [1]
  • Enable JavaDoc formatting
  • Blank lines in JavaD
Java Doc

Imports tab contains rules on how we format imports in the class. Sometimes you will notice that, Intellij IDEA perform wild card imports. It can be overcome with this tab settings . For more details look at  [1].

Arrangement This tab lets you define a set of rules that rearranges your code according to your preferences. For more details look at  [1].


After you select the appropriate rules, click apply. Then you can apply the formatting rules and you can take out the current configuration from above mentioned configuration location.

[1] -
[2] -

1 comment: