OpenMRS Code Formatter Styles

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

2 Likes

We have been using the maven goal (using the maven-formatter-plugin) for code formatting and I really like that process. It is IDE independent, although it does cause some minor annoyances, I think its better than other ones that we’ve explored.

@sunbiz, thanks for the info. I too agree that it will be the most efficient way to use it. As I saw it’s using openmrs eclipse formatter inside to perform code formattings. But some rules such as wrap lines, enforce brackets for single line if statement and etc can easily achieve through the intellij IDEA formatter which are not perform when the maven build which I observed.

Also some modules are not configured with the maven formatter plugin. We can consider adding this as a GCI task? :).

But when I do coding, sometimes the formatted code not align as expected after maven builds. Also there are situation where we knew the exact fix and commit the code and then run the maven build which again reformat our code. Then I also need to do a git rebase. Sometimes my IDEA editor shows that all the code lines align correctly, but when I perform the pull request, it shows that there are alignment issues. I found the IDEA editor property which says show white spacs which shows the spaces and tabs in the code.

That’s why I though of having a Intellij IDEA formatter. :smile:

Thanks, Harsha

I think it would be great to have an IntelliJ formatter, even if the maven plugin duplicates its work.

I will admit that I often make small commits without doing a maven build, and having IntelliJ format in the OpenMRS way would be helpful. :smile:

Dear all,

I want to propose a change to the formatting of enum’s and would like to know if anybody would be in favor of it.

The current OpenMRS Formatter.xml puts all enum elements in one line which in case of the radiology module looks something like this for enum Modality:

public enum Modality {
	
	CR("Computed Radiography"), MR("Magnetic Resonance"), CT("Computed Tomography"), NM("Nuclear Medicine"), US(
	        "Ultrasound"), XA("X Ray");

I would love to change it as proposed here:

to get this result:

public enum Modality {
	
	CR("Computed Radiography"),
	MR("Magnetic Resonance"),
	CT("Computed Tomography"),
	NM("Nuclear Medicine"),
	US("Ultrasound"),
	XA("X Ray");

which in my opinion reads nicer and also leads to good diffs, if one would add or delete any of the elements you just see it right away.

What do you think?

Thank you all :smile:

1 Like

+1 for this change @teleivo

dear all,

I made the changes I proposed above to the OpenMRSFormatter.xml and created a PR

(CI build passed, since I didnt touch any hot stuff :wink:

Is it ok to request changes directly via a PR? Since its not an issue or feature I didnt think its supposed to go on JIRA.

Thank you guys! Ivo

2 Likes

It’s been a while since @harsha89 posted his code style settings for IntelliJ IDEA but I thought it worth mentioning that I decided to create a github project so as to make it easier to enhance the IDEA code style rules. I started with @harsha89’s settings and made some minor improvements. Feel free to use and contribute!

4 Likes

I always write enums like this. +1

I think it’s a good length, not very short as the outdated 80 chars but also not very long. I thinks it’s important that everyone uses the same formatting settings regardless of the IDE.

Personally, I absolutely hate settings around maximum line length. I’d rather see us use discretion here based on code readability. And I generally feel like if you have sufficient conventions around always having a newline after a semi-colon, then this mostly takes care of itself. But that’s me.

This is generally the one setting that keeps me from adopting code formatters in my own work more broadly.

2 Likes

@harsha89 Seems to be given Intellij IDEA code format style doesn’t work with IntellJIDEA 15 version.

@milan did you got any exceptions?

I haven’t tested with IDEA 15 but will give it a try at some point. Meanwhile, you also create an issue at GitHub - suniala/openmrs-code-style-idea: Code formatting rules for OpenMRS core and module developers who use IntelliJ IDEA or even create a pull request, if you are able to solve the problem. I guess I will create a maintenance branch for IDEA 14 once we start making changes specific to IDEA 15.

Just tested on IDEA 15 and everything seems to work.

1 Like

I just installed this in IDEA15 on OSX and it worked fine. Thanks @kosmik!

dear friends of good code style :slight_smile:

just wanted to share that I changed the radiology modules formatter plugin (see merge commit) to

which is an active fork of the google formatter plugin.

I did this because I couldnt get the following style to work with the former formatter plugin:

which you get when you set the qualified invocations to wrap all elements in the eclipse formatter:

so I get a more fluent style, which is nice for the builder patterns and also great in all hibernate DAO methods using criteria.

GitHub - revelc/formatter-maven-plugin: Formatter Maven Plugin also supports javascript formatting, not sure if the google plugin does.

let me know what you think, I am willing to make a PR for the core if you like me too :wink: