Hi All,
I was looking through the OpenMRS code formatter styles in wiki and saw that there is a code formatter for eclipse. But there is not a one for Intellij IDEA. But with the new edition of 13.x version of Intellij provide the capability of importing eclipse code formatter in to Intellij IDEA without any plugin. Even though I think it’s better to have a separate one. So I have created a Intellij Code Formatter using Eclipse Code Formatter. I have uploaded it to [1] and you can try it by putting it into the Intellij IDEA code formatting configuration holder location which is in your home directory where the location is “.IntelliJIdea13/config/codestyles”. The Intellij IDEA hold the configuration and related meta data and resources in “.IntelliJIdea13” directory which will created at the first installation.
To format the code with new formatter, you can use shortcut “Ctrl+Alt+L” which will format your class. In linux systems, this shortcut use to lock the machine. So you will need to change the shortcut either in the Intellij or System.
You can navigate to File > Settings> Code Style tab in the project view. As in below screenshot Intellij IDEA contains set of general settings for code formatters for languages. Since Intellij IDEA has the inbuilt support for several languages, you can see the list of languages where you can specify set of code formatter styles.
There I have set the maximum line length to 120 characters. Would that be Ok with current code style formattings?
I had a closer look at the java coding styles for OpenMRS. After applying the code formatter, you can select the openmrs_codestyle which would be the newly added code formatter to the Intellij. As shown in below, exploring the Java tab you will see set of tabs with different properties.
For more detailed descriptions for code formatting guidelines. resources in [2] will be pretty useful.
Tabs and Indent tab contains the java code formatting styles for specifying tabs. I have make tab size to 4 spaces and indent to 4 spaces while setting continuation indent to 8. Selecting tab character property will be beneficial when we use tabs for indentation. Otherwise Intellij will use spaces for indentation. This section will looks like below as with the code formatter.
Spaces tab contains the rules where we needs to have the spaces in the code. Below screenshots shows the current selected options.
Continue…
Continue…Wrapping and Braces tab contains the rules for where we needs to wrap lines. For example, since we strict our line length to 120, we would like to wrap line if it exceed 120 character limit. There are several important settings in this view. Below screenshots shows, current selected properties.
Continue…,
Continue…Blank Lines tab contains rules for where we needs to put a blank line. Current properties shown in below screenshot.
JavaDoc tab contains rules for format java docs. Current setting shown as below screenshot.
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. Current settings as below screenshot.
Arrangement This tab lets you define a set of rules that rearranges your code according to your preferences. Current settings as below screenshot.
If I can get a feedback on the settings with the attached code formatter, we can put Intellij IDEA code formatter in tthe wiki to have uniform code style in OpenMRS platform.
We can set formatting options for Javascript, Groovy and etc in single code formatter which will further helpful for our coding.
I appreciate any feedback on this to improve this.
Thanks, Harsha