Groovy Goodness: IntelliJ IDEA Formatting Of Closure Chains

When we write code with a lot of method chaining that involves closures and the use the Reformat code feature of IntelliJ IDEA things might get screwed up. Luckily we can changes some Groovy formatting rules in IntelliJ IDEA to prevent the reformatting.

In the following screenshot we see the original piece of code in the IntelliJ IDEA editor:

When we invoke Reformat code command we can have the following result where the cod his not aligned anymore:

Let’s set two options in the Preferences to keep the code formatted as is. We need to go to Editor | Code Style | Groovy. In the section Braces placement we enable the option Use flying geese braces:

In the section Chained method calls we enable the option Align when multiline:

Now when we reformat our original piece of code that alignment stays:

Written with IntelliJ IDEA 2016.1.

Original blog post

This entry was posted in Coding, Groovy, IntelliJ IDEA and tagged , by mrhaki. Bookmark the permalink.

About mrhaki

My name is Hubert A. Klein Ikkink also known as mrhaki. I work at the great IT company JDriven. Here I work on projects with Groovy & Grails, Gradle and Spring. At JDriven we focus on SpringSource technologies. All colleagues want to learn new technologies, support craftmanship and are very eager to learn. This is truly a great environment to work in. You can contact me via Google+ or @mrhaki.

Leave a Reply

Your email address will not be published. Required fields are marked *